Python 练习实例1
题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
我的代码:python 3+
#2017-7-20 list_h = [1,2,3,4] list_c = [] list_u = [] n = 0 for x in list_h: list_c = list_h[:] list_c.remove(x) for y in list_c: list_u = list_c[:] list_u.remove(y) for z in list_u: n += 1 result = x * 100 + y * 10 + z print("第%d种:" % n , result)
推荐代码:python 2+
#!/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
将for循环和if语句综合成一句,直接打印出结果 #!/usr/bin/env python # -*- coding: UTF-8 -*- list_num = [1,2,3,4] list = [i*100 + j*10 + k for i in list_num for j in list_num for k in list_num if (j != i and k != j and k != i)] print (list)
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,求应发放奖金总数?
#2017-7-20 # -*- coding:utf-8 -*- profit = int(input("请输入利润值:")) list_profit = [1000000,600000,400000,200000,100000,0] point = [0.01, 0.015, 0.03, 0.05, 0.075 ,0.1] bonus = 0 for x in range(len(point)): if profit > list_profit[x]: bonus += (profit - list_profit[x]) * point[x] profit = list_profit[x] else: continue print(bonus)
Python 练习实例3
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
#2017-07-20 # -*- coding:utf-8 -*- for x in range(2,85,2): y = 168 / x if x > y and (x + y) % 2 == 0 and (x - y) % 2 == 0: m = (x + y) / 2 n = (x - y) / 2 x = n * n - 100 print(x)
Python 练习实例4
题目:输入某年某月某日,判断这一天是这一年的第几天?
#2017-7-20 # -*- coding:utf-8 -*- year = int(input("请输入年份:")) month = int(input("请输入月份:")) day = int(input("请输入日:")) if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0) : days = [0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30] else: days = [0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30] which_day = 0 if month >= 1 and month <= 12: for x in range(month): which_day += days[x] which_day += day print("this day is the %dth day" % which_day) else: print(‘error month type‘)
时间: 2024-11-11 04:40:56