RT,小白学习py,个人理解,如有不对希望指正,不胜感激
参考书籍:《Python编程从入门到实践》 Eric Matthes
1.在字典中储存列表:
#在字典中存储列表
pizza={
'crust':'thick',
'toppings':['mushrooms','extra_cheese'],
}
#概述所点的披萨
print("You ordered a "+pizza['crust']+ "-crust pizza "+"with the following toppings: ")
for topping in pizza['toppings']:
print("\t"+topping)
#pizza是一个字典,里面的toppings键值对 是一个列表,也就是列表储存于字典了
2.字典嵌套列表
#字典嵌套列表,m每一个对 都是一个列表,类似于二维数组?
favorite_lang={
'jen':['python','ruby'],
'sarah':['c'],
'edward':['ruby','go'],
'phil':['python','haskell'],
}
#二层循环
for name,languages in favorite_lang.items():
print("\n"+name.title()+"'s favorite langs are: ")
for language in languages:
print("\t"+language.title())
3.字典中存字典:
#在字典中存储字典
users={
'aeinstein':{
'first':'albert',
'last':'einstein',
'location':'princeton',
},
'mcurie':{
'first':'marie',
'last':'mcurie',
'location':'paris',
},
'torres':{
'first':'fernando',
'last':'torres',
'location':'spain',
},
}
for username,user_info in users.items():
print("\nUsername: "+username)
fullname=user_info['first']+" "+user_info['last']
location=user_info['location']
print("\tFull name is "+fullname.title())
print("\tLocation is "+location.title())
4.关于input函数
message=input("输入点东西,我是复读机")
print(message)
height=input("How tall are you?\n")
height=int(height)
if height >= 150:
print("You're tall enough to ride a biycle")
else:
print("You'll be able to ride when you're tall enough")
5.Python没有a++?????
6.while循环:
prompt = "输入然后我复读:\n"
prompt += "输入'退出'结束:\n"
message=""
while message != '退出':
message=input(prompt)
if message!='退出':
print(message)
7.Bool 类似于 的使用
prompt = "输入然后我复读:\n"
prompt += "输入'退出'结束:\n"
active=True
while active:
message=input(prompt)
if message=='退出':
active=False
else:
print(message)
8.break 和 continue 的使用类似于CPP
9.发现可以用列表非空作为一个条件循环
#使用while来处理列表和字典
#创建一个待验证用户列表,和一个用于存储已验证用户的空列表
unconfirmed_users=['alice','brain','candace']
confirmed_users=[]
#验证每个用户,直到没有未验证用户为止,将每个经过验证的列表都移到已验证的拥护列表中
while unconfirmed_users:
current_user=unconfirmed_users.pop()
print("Verifying user: "+current_user.title())
confirmed_users.append(current_user)
#显示所有已经验证的拥护
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
10.#删除包含特定值的所有列表元素,注意循环的条件!!!!!!!!!!!!!!!!
#删除包含特定值的所有列表元素
#remove可以用来删除列表中的特定值
#但是如果存在多个这样包含这个值的元素,怎么删除?
pets=['dog','cat','dog','fish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
11.利用输入 动态扩充字典?
#用用户输入来填充字典
responses={}
#设置一个标志
polling_active=True
while polling_active:
#提示输入被调查者的名字喝回答
name=input("\nWhat's your name?\n")
response=input("Which mountain do you like ?\n")
#将答卷存储到字典中
responses[name]=response
#看看是否还有人要参与调查
repeat=input("Would you like to let another one respond?(yes/no)")
if repeat=='no':
polling_active=False
#调查结束,显示结果
print("\n---Poll Results---")
for name,response in responses.items():
print(name+" would like to climb "+response+".")
print(responses)