python exercise normal

#判断年份对应的生肖
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})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值