python exercise normal

版权声明:本文章为博主原创文章,未经博主允许不得转载,如有问题,欢迎留言交流指正 https://blog.csdn.net/finalkof1983/article/details/88729025
#判断年份对应的生肖
def chinese_constellation_to_year(year):
    chinese_constellation = ("鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪")
    print(chinese_constellation[(year-2020) % 12])


chinese_constellation_to_year(1999)

#给定出生日期判断对应的星座
def constellation(month,day):
    mount_day = int(month) + int(day)/100
    all_constellation = {1.19: "摩羯座", 2.18: "水瓶座", 3.20: "双鱼座", 4.19: "白羊座", 5.20: "金牛座", 6.21: "双子座", 7.22: "巨蟹座",8.22: "狮子座", 9.22: "处女座", 10.23: "天秤座", 11.22: "天蝎座", 12.21: "射手座"}
    if mount_day > 12.21:
        return all_constellation.get(1.19)
    for i in all_constellation.keys():
        if mount_day <= i:
            return all_constellation.get(i)


month = input("请输入月份:")
day = input("请输入日期:")
print(constellation(month,day))

#filter的测试,找出列表中小于n的值
def num_less_n(alist, n):
    return list(filter(lambda x : x < n,alist))

print(num_less_n([3, 7, 8, 5, 20],6))

#字符串格式操作符%的使用,在字符串中%不再去求余运算
#%s表示由字符串来替换,%d表示由整数来替换,%f表示由浮点数替换
print('%d是%s' % (12 - 7,"质数"))

#单行赋值和值交换
a, b = 1, 2
a, b = b, a
print(a, b)

#执行过程中加入延迟操作,每输出一个i,延迟2秒
import time

for i in range(5):
    print(i)
    time.sleep(2)

#python列表推导式
alist = []
for i in range(5):
    if i % 2 == 0:
        alist.append(i*i)
print(alist)

print([i*i for i in range(5) if i % 2 == 0])


adict = {}
for i in range(5):
    if i % 2 == 0:
        adict[i] = i*i
print(adict)

print({i : i*i for i in range(5) if i % 2 == 0})

猜你喜欢

转载自blog.csdn.net/finalkof1983/article/details/88729025