实例01 输出每日一贴
使用 datetime.datatime.now() 用来获取当前的日期
点击查看代码
import datetime #导入日期时间类
#定义一个列表
mot = ["今天星期一:\n坚持下去不是因为我很坚强,而是因为我别无选择。",
"今天星期二:\n含泪播种的人一定能笑着收获。",
"今天星期三:\n做对的事情比把事情做对重要。",
"今天星期四:\n命运给予我们的不是失望之酒,而是机会之杯。",
"今天星期五:\n不要等到明天,明天太遥远,今天就行动。",
"今天星期六:\n求知若渴,虚心若愚。",
"今天星期日:\n成功属于那些从不说“不可能”的人。"]
day=datetime.datetime.now().weekday() #获取当前星期
print(mot[day]) #输出每日一帖
运行结果
实例02 分两列显示2017-2018赛季NBA西部联盟前八名的球队
点击查看代码
print("2017-2018赛季NBA西部联盟前八名\n")
team = ["火箭","勇士","开拓者","雷霆","爵士","鹈鹕","马刺","森林狼"]
for index,item in enumerate(team):
if index%2 == 0:
print(item +"\t\t",end='')
else:
print(item +"\n")
运行结果
实例03 向NBA名人堂列表中追加2018年新进入的球星
点击查看代码
#NBA名人堂原有人员
oldlist = ["迈克尔·乔丹","卡里姆·阿布杜尔·贾巴尔","哈基姆·奥拉朱旺","查尔斯·巴克利",
"姚明"]
newlist = ["贾森·基德","史蒂夫·纳什","格兰特·希尔"]
oldlist.extend(newlist) #追加新球员
print(oldlist) #显示新NBA名人堂人员名单
运行结果
实例04 使用二维码输出不同版式的古诗
点击查看代码
str1 = "千山鸟飞绝"
str2 = "万径人中灭"
str3 = "孤舟蓑笠翁"
str4 = "独钓寒江雪"
verse = [list(str1),list(str2),list(str3),list(str4)]
print("\n----横版----\n")
for i in range(4):
for j in range(5):
if j ==4:
print(verse[i][j])
else:
print(verse[i][j],end='')
verse.reverse()
print("\n----竖版----\n")
for i in range(5):
for j in range(4):
if j ==3:
print(verse[j][i])
else:
print(verse[j][i],end='')
运行结果
实例05 使用元组保存咖啡馆里提供的咖啡名称
点击查看代码
#定义元组
coffeename = ("蓝山","卡布奇诺","曼特宁","摩卡","麝香猫","哥伦比亚")
#输出元组
print(coffeename)
运行结果
实例06 使用for循环出咖啡馆里的咖啡名称
点击查看代码
coffeename = ("蓝山","卡布奇诺","曼特宁","摩卡","麝香猫","哥伦比亚")
print("您好,欢迎光临~伊米咖啡馆~\n\n我店有:\n")
for name in coffeename:
print(name +"咖啡 ", end =" ")
运行结果
实例07 分两列显示2017~2018赛季NBA西部联盟前八名球队
点击查看代码
print("2017-2018赛季NBA西部联盟前八名\n")
team = ("火箭","勇士","开拓者","雷霆","爵士","鹈鹕","马刺","森林狼")
for index,item in enumerate(team):
if index%2 == 0:
print(item +"\t\t",end='')
else:
print(item +"\n")
运行结果
实例08 将麝香猫咖啡替换成拿铁咖啡
点击查看代码
#定义元组
coffeename = ("蓝山","卡布奇诺","曼特宁","摩卡","麝香猫","哥伦比亚")
#输出元组
coffeename=("蓝山","卡布奇诺","曼特宁","摩卡","拿铁","哥伦比亚")
print(coffeename)
运行结果
实例09 创建一个保存女神星座的字典
点击查看代码
name =["绮梦","冷伊一","香凝","黛兰"]
sign =["水瓶座","射手座","双鱼座","双子座"]
dictionary = dict(zip(name,sign))
print(dictionary)
运行结果
实例10 根据星座测试性格特点
点击查看代码
name = ['绮梦','冷伊一','香凝','黛兰']
sign_person = ['水瓶座','射手座','双鱼座','双子座']
person_dict = dict(zip(name,sign_person))
sign_all =['白羊座','金牛座','双子座','巨蟹座',
'狮子座','处女座','天秤座','天蝎座','射手座','摩羯座',
'水瓶座','双鱼座']
nature = ['有一种让人看见就觉得开心的感觉,阳光、乐观、 坚强, 性格直来直去,就是有点小脾气。',
'很保守, 喜欢稳定, 一旦有什么变动就会觉得心里不踏实,性格比较慢热,是个理财高手',
'喜欢追求新鲜感,有点小聪明, 耐心不够, 因你的可爱性格会让很多人喜欢和你做朋友。',
'情绪容易敏感, 缺乏安全感, 做事情有坚持到底的毅力,为人重情重义,对朋友和家人特别忠实。',
'有着远大的理想, 总想靠自己的努力成为人上人,总是期待被仰慕被崇拜的感觉。',
'坚持追求自己的完美主义者。',
'追求平等、和谐, 交际能力强, 因此朋友较多。 最大的缺点就是面对选择总是犹豫不决。',
'精力旺盛, 占有欲强,对于生活很有目标, 不达目的哲不罢休,复仇心重。',
'崇尚自由,勇敢、 果断、 独立,身上有一股勇往直前的劲儿, 只要想做,就能做。',
'是最有耐心的, 做事最小心。 做事脚踏实地, 比较固执,不达目的不罢休,而且非常勤奋。',
'人很聪明,最大的特点是创新, 追求独一无二的生活,个人主义色彩很浓重的星座。',
'集所有星座的优缺点于一身。最大的优点是有一颗善良的心, 愿意帮助别人。']
sign_dict =dict(zip(sign_all,nature))
print("[香凝]的星座是",person_dict.get("香凝"))
print("\n她的性格特点是:\n\n",sign_dict.get(person_dict.get("香凝")))
运行结果
实例11 应用字典推导式实现根据名字和星座创建一个字典
点击查看代码
name = ['绮梦','冷伊一','香凝','黛兰']
sign = ['水瓶','射手','双鱼','双子']
dictionary = {i:j+'座' for i,j in zip(name,sign)}
print(dictionary)
运行结果
实例12 创建保存学生选课信息的集合
点击查看代码
python = {'绮梦','冷伊一','香凝','梓轩'}
c = {'冷伊一','零语','梓轩','圣博'}
print("选择python的同学有:",python,'\n')
print("选择C语言的学生有:",c)
运行结果
实例13 学生更改选学课程
点击查看代码
python = set(['绮梦','冷伊一','香凝','梓轩'])
python.add('零语')
c = set(['冷伊一','零语','梓轩','圣博'])
c.remove('零语')
print('选择python的同学有:',python,'\n')
print('选择c语言的同学有:',c)
运行结果
实例14 对选课集合进行交集、并集和差集运算
点击查看代码
python = set (['绮梦','冷伊一','香凝','梓轩'])
c = set(['冷伊一','零语','梓轩','圣博'])
print('选择python语言的学生有:',python)
print('选择c语言的学生有:',c)
print('交集运算:',python & c)
print('交集运算:',python | c)
print('差集运算:',python - c)
运行结果
实战01 输出“王者荣耀”的游戏角色
点击查看代码
print("====坦克:====")
tank = ["苏烈","刘邦","钟馗","张飞","牛魔","程咬金","白起","刘禅","庄周","项羽","廉颇","猪八戒"]
for i in tank:
print(i,end=" ")
print("\n"+"====战士:====")
zhanshi = ["狂铁","裴擒虎","铠","孙悟空","哪吒","杨戬","橘右京","亚瑟","雅典娜","夏侯惇","关羽","吕布","韩信","老夫子","典韦","曹操","钟无艳","墨子","赵云"]
for i in zhanshi:
print(i,end=" ")
print("\n"+"====刺客: ====")
cike =["百里玄策","庞统","花木兰","阿珂","不知火舞","李白","娜可露露","兰陵王","韩信","宫本武藏"]
for i in cike:
print(i,end=" ")
print("\n"+"====法师: ====")
fashi =["杨玉环","奕星","女娲","周瑜","鬼谷子","芈月","东皇太一","大乔","诸葛亮","貂蝉","张良","安琪拉"]
for i in fashi:
print(i,end="")
print("\n"+"====射手:====")
sheshou =["公孙离","百里守约","后羿","刘备","黄忠","马可波罗","成吉思汗","李元芳","艾琳","狄仁杰","鲁班七号","孙尚香"]
for i in sheshou:
print(i,end=" ")
print("\n"+"====辅助:====")
fuzhu = ["明世隐","梦奇","孙膑","太乙真人","蔡文姬"]
for i in fuzhu:
print(i,end=" ")
运行结果
实战02 模拟火车订票系统
点击查看代码
print("车次"+"\t"+"出发站-到达站"+"\t"+"出发时间"+"\t"+"到达时间"+"\t"+"历时")
checi = ['T40','T298','Z158','Z62']
zhan = ['长春-北京','长春-北京','长春-北京','长春-北京']
stime = ['00:12','00:06','12:48','21:58']
etime = ['12:20','10:50','21:06','06:08']
lishi = ['12:08','10:44','08:18','8:20']
print(checi[0]+'\t'+zhan[0]+'\t'+stime[0]+'\t'+etime[0]+'\t'+lishi[0])
print(checi[1]+'\t'+zhan[1]+'\t'+stime[1]+'\t'+etime[1]+'\t'+lishi[1])
print(checi[2]+'\t'+zhan[2]+'\t'+stime[2]+'\t'+etime[2]+'\t'+lishi[2])
print(checi[3]+'\t'+zhan[3]+'\t'+stime[3]+'\t'+etime[3]+'\t'+lishi[3])
number = str(input("请输入要购买的车次:"))
name = str(input("请输入乘车人(用逗号分隔):"))
checi_dict = dict(zip(checi,zhan))
stime_dict = dict(zip(checi,stime))
a = checi_dict[number]
b = stime_dict[number]
print("你已购"+str(number)+"次列车" +str(a)+" "+str(b)+ "开","请"+str(name)+"准时乘车【铁路客服】")
运行结果
实战03 电视剧的收视率排行榜
点击查看代码
print("电视剧的收视排行榜:")
program = {('《Give up,hold on to me》收视率:','1.4%'),
('《The private dishes of the husbands》收视率:','1.343%'),
('《My father-in-law will do martiaiarts》收视率:','0.92%'),
('《North Canton still believe in love》收视率 :','0.862%'),
('《Impossible task》收视率:','0.553%'),
('《Sparrow》收视率:','0.411%'),
('《East of dream Avenue》收视率;','0.164%'),
('《The prodigal son of the frotier town》收视率:','0.259%'),
('《Distant distance》收视率:','0.394%'),
('《Music legend》收视率:','0.562%')}
program_rate = sorted(program,key = lambda x:x[1],reverse = True)
for item in program_rate:
print(item[0]+str(item[1]))
运行结果
实战04 统计需要取快递的人员名单
点击查看代码
set1 = set()
none = True
while none :
put =input("请输入收到快递人员的名单(输入0退出):")
if put == "0":
break
if put in set1 :
print("快递人员已存在!")
else :
set1.add(put)
print("需要通知取快递的人员名单:")
for item in set1:
print(item)
运行结果
标签:end,item,python,print,点击,dict,应用,序列,第四章
From: https://www.cnblogs.com/m7hhhhhh/p/16739664.html