您的位置:首页 > 编程语言 > Python开发

Python 100练习题[1-10]

2016-12-28 15:22 399 查看
1.. 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

import itertools

def permutation12341():
result0= list(itertools.permutations('1234', 3))
result1 = [''.join(i) for i in result0]
result2 = [int(i) for i in result1]
result3 = len(result2)
result = {'result':result2, 'length': result3}
return result

result = permutation12341()
print result


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,求应发放奖金总数?

def bonusfun(x):
if (x > 0) and (x <= 100000):
return 0.1*x
elif (x > 100000) and (x <= 200000):
return 1 + 0.075*(x - 10)
elif (x > 200000) and (x <= 400000):
return 1 + 0.75 + 0.05*(x - 20)
elif (x > 400000) and (x <= 600000):
return 1 + 0.75 + 0.5 + 0.03*(x - 40)
elif (x > 600000) and (x <= 1000000):
return 1 + 0.75 + 0.5 + 0.3 + 0.015*(x - 60)
else:
return 1 + 0.75 + 0.5 + 0.3 + 0.15 + 0.01*(x - 100)

result = bonusfun(120000)
print result


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

result = []
for i in xrange(168):
for j in xrange(i, 168):
if j*j - i*i == 168:
result.append(i*i - 100)

print result


4.. 输入某年某月某日,判断这一天是这一年的第几天?

Year = input('Please enter a year: ')
Month = input('Please enter a month: ')
Day = input('Please enter a day: ')

months1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
months2 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

if int(Year)%4 == 0 or int(Year)%100 != 0 and int(Year)%400 == 0:
result = sum(months2[0:(Month - 1)]) + Day
else:
result = sum(months1[0:(Month - 1)]) + Day

print '%d th day of the year'%result


5.. 输入三个整数x,y,z,请把这三个数由小到大输出

inputData = []
X = input('X(an integer): ')
inputData.append(X)
Y = input('Y(an integer): ')
inputData.append(Y)
Z = input('Z(an integer): ')
inputData.append(Z)

inputData.sort()
print inputData


6.. 斐波那契数列

def fib(n):
result = [0, 1]
for i in xrange(2, n):
result.append(result[-2]+result[-1])
return result

print fib(10)


7.. 将一个列表的数据复制到另一个列表中

import copy

x = [1, 2, 3]
y = copy.deepcopy(x)
print y


8.. 输出9*9乘法口诀表

for i in xrange(1, 10):
for j in xrange(1, 10):
print '%d * %d = %d'%(i, j, i*j)


9.. 暂停一秒输出

import time

myDict = {1: 'a', 2: 'b'}
for key, value in dict.<
b829
span class="hljs-keyword">items(myDict):
print key, value
time.sleep(1)


10.. 暂停一秒输出

import datetime
import time

print datetime.datetime.now()
time.sleep(1)
print datetime.datetime.now()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python