Python 基础练习

时间:2021-04-28 18:13:56

今天接触了python,了解了一下 python 的基础语法,于是想着手训练一下,在本习题集中,参考代码为提供的参考答案,前面的代码为自己思考的代码,最后每道题给出练习的时间。

Python 基础练习题1

题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?


代码

def printNum():
sum = 0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if i==j or i == k or j == k:
continue
else:
sum += 1
print i,j,k
print "sum:",sum,"line" printNum()

运行结果

1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
sum: 24 line

参考代码

#!/usr/bin/python
# -*- coding: UTF-8 -*- for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if( i != k ) and (i != j) and (j != k):
print i,j,k

用时: 44min


Python 基础练习题2

题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?


代码

#!/usr/bin/python
# -*- coding: UTF-8 -*- def countBonus():
profit = int(raw_input('请输入利润(万):')) #这里要定义为int
if profit <= 10:
bonus = profit * 0.1
elif profit > 10 and profit <= 20:
bonus = 10 * 0.1 + (profit-10) * 0.075
elif profit > 20 and profit <= 40:
bonus = 10 * 0.1 + 10 * 0.075 + (profit-20)*0.05
elif profit > 40 and profit <= 60:
bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (profit-40) * 0.03
elif profit > 60 and profit <= 100:
bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (profit - 60) * 0.015
elif profit > 100:
bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (profit-100)*0.01
print "奖金(万)为:",bonus countBonus()

运行结果

请输入利润(万):12
奖金(万)为: 1.15

参考代码


#!/usr/bin/python
# -*- coding: UTF-8 -*- i = int(raw_input('净利润:'))
arr = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
r = 0
for idx in range(0,6):
if i>arr[idx]:
r+=(i-arr[idx])*rat[idx]
print (i-arr[idx])*rat[idx]
i=arr[idx]
print r

用时: 20min


Python 基础练习题3

题目:一个整数,它加上100和加上268后都是一个完全平方数,请问该数是多少?


代码

#!/usr/bin/python
# -*- coding: UTF-8 -*- import math
def calculate():
for i in range(1,10001):
getNum1 = math.sqrt(i+100)
getNum2 = math.sqrt(i+268)
if getNum1 % int(getNum1) == 0 and getNum2 % int(getNum2) == 0:
print i calculate()

运行结果

21
261
1581

参考代码


#!/usr/bin/python
# -*- coding: UTF-8 -*- import math
for i in range(10000):
#转化为整型值
x = int(math.sqrt(i + 100))
y = int(math.sqrt(i + 268))
if(x * x == i + 100) and (y * y == i + 268):
print i

用时: 16min


Python 基础练习题4

题目:输入某年某月某日,判断这一天是这一年的第几天


代码

#!usr/bin/python
# -*- coding: UTF-8 -*- #闰年
leapYear = [31,29,31,30,31,30,31,31,30,31,30,31]
# 普通年
commonYear = [31,28,31,30,31,30,31,31,30,31,30,31] def countDay():
getDate = raw_input("请输入年月日(XXXX.XX.XX 例如:2016.10.30):")
date = getDate.split(".")
# 分别获得年月日
year = int(date[0])
month = int(date[1])
day = int(date[2]) sum = 0
if (year % 100 == 0 and year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
# 闰年
for i in range(1,month):
sum += leapYear[i]
sum += day
else:
for i in range(1,month):
sum += commonYear[i]
sum += day
print "该日期为该年份的第:",sum,"天" while 1:
countDay()
ifExit = raw_input("是否退出?(Y/N):")
if ifExit == "Y" or ifExit == "y":
break

运行结果

请输入年月日(XXXX.XX.XX 例如:2016.10.30):2016.10.03
该日期为该年份的第: 277 天
是否退出?(Y/N):n
请输入年月日(XXXX.XX.XX 例如:2016.10.30):2000.8.7
该日期为该年份的第: 220 天
是否退出?(Y/N):n
请输入年月日(XXXX.XX.XX 例如:2016.10.30):1900.3.2
该日期为该年份的第: 61 天
是否退出?(Y/N):y

参考代码


#!/usr/bin/python
# -*- coding: UTF-8 -*- year = int(raw_input('year:\n'))
month = int(raw_input('month:\n'))
day = int(raw_input('day:\n')) months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month <= 12:
sum = months[month - 1]
else:
print 'data error'
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print 'it is the %dth day.' % sum

用时: 27min


Python 基础练习题5

题目:输入三个整数x,y,z,请把这三个数由小到大输出。


代码

#!usr/bin/python
# -*- coding: UTF-8 -*- def demoFunction():
getNum = raw_input("请输入三个数字(用英文符号 , 隔开):")
num = getNum.split(",")
num1 = int(num[0])
num2 = int(num[1])
num3 = int(num[2]) if num1>num2:
temp = num2
num2 = num1
num1 = temp
if num1>num3:
temp = num3
num3 = num1
num1 = temp
if num2 > num3:
temp = num3
num3 = num2
num2 = temp print num1,num2,num3 demoFunction()

运行结果

请输入三个数字(用英文符号 , 隔开):3,7,1
1 3 7

代码优化

#! usr/bin/python
# -*- coding: UTF-8 -*- def demoFunction():
getNum = raw_input("请输入三个数字(用英文符号 , 隔开):")
num = getNum.split(",")
num1 = int(num[0])
num2 = int(num[1])
num3 = int(num[2]) if num1>num2:
num1,num2 = num2,num1
if num1>num3:
num1,num3 = num3,num1
if num2 > num3:
num2,num3 = num3,num2 print num1,num2,num3 demoFunction()

参考代码

#!/usr/bin/python
# -*- coding: UTF-8 -*- l = []
for i in range(3):
x = int(raw_input('integer:\n'))
l.append(x)
l.sort()
print l

用时: 10min


Python 基础练习题6

题目:斐波那契数列。


代码

#! usr/bin/python
# -*- coding: UTF-8 -*- # 斐波那契数列
def Fibonacci(num):
if num == 1:
print "0"
elif num == 2:
print "1"
else:
fib = [0,1]
for i in range(2, num):
fib.append(int(fib[i-1]) + int(fib[i-2]))
print fib num = int(raw_input("请输入斐波那契数列的位数:"))
Fibonacci(num)

运行结果

请输入斐波那契数列的位数:10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

参考代码

方法一

#!/usr/bin/python
# -*- coding: UTF-8 -*- def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
return a # 输出了第10个斐波那契数列
print fib(10)

方法二

#!/usr/bin/python
# -*- coding: UTF-8 -*- # 使用递归
def fib(n):
if n==1 or n==2:
return 1
return fib(n-1)+fib(n-2) # 输出了第10个斐波那契数列
print fib(10)

方法三

#!/usr/bin/python
# -*- coding: UTF-8 -*- def fib(n):
if n == 1:
return [1]
if n == 2:
return [1, 1]
fibs = [1, 1]
for i in range(2, n):
fibs.append(fibs[-1] + fibs[-2])
return fibs # 输出前 10 个斐波那契数列
print fib(10)

用时: 12min


Python 基础练习题7

题目:将一个列表的数据复制到另一个列表中。


参考代码

#!/usr/bin/python
# -*- coding: UTF-8 -*- a = [1, 2, 3]
b = a[:]
print b

运行结果

[1,2,3]

用时: 0min


Python 基础练习题8

题目:输出9x9乘法口诀表


代码

#! usr/bin/python
# -*- coding: UTF-8 -*- for i in range(1,10):
for j in range(i,10):
print i,"*",j,"=",i*j
print ""

运行结果

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9 2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18 3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27 4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36 5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45 6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54 7 * 7 = 49
7 * 8 = 56
7 * 9 = 63 8 * 8 = 64
8 * 9 = 72 9 * 9 = 81

用时: 4min


Python 基础练习题9

题目:暂停一秒输出。


代码1

#! usr/bin/python
# -*- coding: UTF-8 -*- import time for i in range(5):
time.sleep(1)
print i

运行结果

0
1
2
3
4

代码2

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) # 暂停一秒
time.sleep(1) print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))

运行结果

2016-11-03 11:18:10
2016-11-03 11:18:11

用时: 2min