Python3
实例一:
import datetime
#定义一个列表
mot = ["今天星期一:\n坚持下去不是因为我很坚强,而是因为我别无选择。",
"今天星期二:\n含泪播种的人一定能笑着收获。",
"今天星期三: \n做对的事情比把事情做对更加重要。",
"今天星期四: \n命运给予我们的不是失望之酒,而是机会之怀。",
"今天星期五: \n不要等到明天,明天太遥远,今天就行动。",
"今天星期六:\n求知若渴,虚心若愚。",
"今天星期天:\n成功将属于那些从来不说“不可能”的人"]
day = datetime.datetime.now().weekday()
print(mot[day])
实例二:
print("2017~2018赛季NBA西部联盟前八名\n")
team = ['火箭','勇士','开拓者','雷霆','爵士','鹈鹕','马刺','森林狼']
for index,item in enumerate(team):
if index % 2 == 0: #判断是否为偶数,为偶数则不换行
print(item +'\t\t', end=' ') #end=' ',python默认输出结尾为换行,end=,自己选择结尾
else: #非偶数换行
print(item +'\n')
实例三:
#NBA名人堂原有人员
oldlist = ["迈克尔.乔丹","卡里姆.阿布杜尔.贾巴尔","哈基姆.奥拉朱旺","查尔斯.巴克利","姚明"]
newlist = ["贾森.基德","史蒂夫.纳什","格兰特.希尔"]
oldlist.extend(newlist)
print(oldlist)
实例四:
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 = "")
实例五:
#定义元祖
coffeename = ('蓝山','卡布奇诺','曼特宁','摩卡','麝香猫','哥伦比亚')
#输出元祖
print(coffeename)
实例六:
coffeename = ('蓝山','卡布奇诺','曼特宁','摩卡','麝香猫','哥伦比亚')
print("您好,欢迎光临~伊米咖啡馆~\n\n我店有: \n")
for name in coffeename:
print(name + "咖啡",end = " ")
实例七:
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")
实例八:
#定义元组
coffeename = ('蓝山','卡布奇诺','曼特宁','摩卡','麝香猫','哥伦比亚')
#将“麝香猫”改为拿铁
coffeename = ('蓝山','卡布奇诺','曼特宁','摩卡','拿铁','哥伦比亚')
#元组不可改动故只能重新定义
print("新元组: ",coffeename)
实例九:
name = ["绮梦","冷凝一","香凝","黛香"] #作为键的列表
sign = ["水瓶座","射手座","双鱼座","双子座"] #作为值的列表
dictionary = {i:j+'座' for i,j in zip(name,sign)} #使用列表推导式生成字典
print(dictionary)
实例十:
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\t",sign_dict.get(person_dict.get("香凝"))) #输出性格特点
实例11:
name = ["绮梦","冷凝一","香凝","黛香"] #作为键的列表
sign = ["水瓶座","射手座","双鱼座","双子座"] #作为值的列表
dictionary = {i:j+'座' for i,j in zip(name,sign)} #使用列表推导式生成字典
print(dictionary)
实战1:
print("列表输出:\n")
print("“王者荣耀”游戏角色:")
position = ['坦克','战士','刺客','法师','射手','辅助']
name = [['苏烈','刘邦','张飞','牛魔'],['狂铁','吕布','哪吒','铠'],
['元歌','李白','阿珂','韩信'],['奕星','女娲','妲己','貂蝉'],
['后羿','黄忠','虞姬','艾琳'],['孙膑','瑶','桑启','钟馗']]
for i in range(0,6):
print("===",position[i],"===")
for j in range(0,4):
print(name[i][j],'\t',end='')
if j == 3:
print('')
######分割线##########
print('\n\n')
######分割线##########
print('字典形式:\n')
print("“王者荣耀”游戏角色:")
all_name = {i:j for i,j in zip(position,name)}
for i in position:
print('===',i,'===')
print(all_name[i])
实战二:
print('''
车次 出发站—到达站 出发时间 到达时间 历时
T40 长春—北京 00:12 12:00 12:08
T298 长春—北京 00:06 10:50 10:44
Z158 长春—北京 12:46 21:06 08:18
Z62 长春—北京 21:56 06:08 8:20
''')
key = input("请输入要购买的车次:")
name = input("请输入乘车人,用“,”隔开:")
system = {"T40":"长春—北京 00:12","T298":"长春—北京 00:06","Z158":"长春—北京 12:46",
"Z62":"长春—北京 21:56"}
print("您已购买 ",key," 次列车 ",system[key],"开,请",name,"准时乘车。【铁路客服】")
实战三:
#定义列表,成员为每个节目名和收视率组成的元组
name = [('0.93','《My father-in-law will do martiaiarts》'),
('1.343','《The private dishes of the husbands》'),
('1.4','《Give up,hold on to me》'),
('0.862','《North Canton still believe in love》'),
('0.553','《Impossible task》'),
('0.411','《Sparrow》'),
('0.164','《East of dream Avenue》'),
('0.259','《The prodigal son of the new frontier town》'),
('0.394','《Distant distance》'),
('0.562','《Music legend》')]
#使用匿名函数和sort函数排序
name.sort(reverse = True)
#输出排序后的列表
for item in name:
print(item[1],'收视率:',item[0],'%')
实战四:
i = 1 #设置循环条件
name = set() #创建空集合
while i:
key = input("请输入收到快递人员的名单(输入0退出): ")
if key == '0': #判断输入为0
i=0 #设置结束循环条件
print("需要通知取快递的人员名单:")
for j in name: #循环输出名单
print(j)
elif key in name: #判断人员是否已经在名单中
print("取快递人员已存在!")
else:
name.add(key) #加入名单中