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')

Python 练习实例5

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

#2017-7-20
# -*- coding:utf-8 -*-

li = []
for x in range(3):
    y = int(input('请输入数字:'))
    li.append(y)

print(sorted(li))

Python 练习实例6

题目:斐波那契数列。           0、1、1、2、3、5、8、13、21、34、……。

#2017-7-20
# -*- coding:utf-8 -*-

def fib(x):
    a,b = 1,1
    for t in range(x-2):
        if x >0:
            a,b = b,a + b
    print(b)
print(fib(int(input("give a number :"))))



#用递归
def fix(x):
    if x == 1 or x == 2:
        return 1
    else:
        return fix(x-1) + fix(x-2)
print(fix(int(input("give a number :"))))


#用yield函数
def fiy(x):
    a,b,c = 0,0,1
    while x > a:
        yield c
        b,c = c,b+c
        a+=1
for z in fiy(10):
  print(z)

 

posted on 2017-07-20 17:52  漏三刀  阅读(403)  评论(0编辑  收藏  举报