您的位置:首页 > 编程语言 > Python开发

Python学习笔记04-字典和用户输入和 while 循环

2017-06-04 10:47 716 查看

一个简单的字典

alien = {'color': 'green', 'points': 5}
print(alien['color'])
print(alien['points'])
green
5


增加字典

alien['animal'] = 'dog'


创建一个空字典

alien_0 = {}
alien_0['color'] = 'green'
alien_0['points'] = 5
print(alien_0)
{'color': 'green', 'points': 5}
修改
alien_0['color'] = 'yellow'


删除字典

del alien_0[‘color’]

取出字典

user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}

for key, value in user_0.items():
 print("\nKey: " + key)
 print("Value: " + value)


取出键

for key in user_0.keys():

 print("\nKey: " + key)



取出值

for  value in user_0.values():

 print("Value: " + value)


在字典中存储字典

users = {
'aeinstein': {
'first': 'albert',
'last': 'einstein',
'location': 'princeton',
},
'mcurie': {
'first': 'marie',
'last': 'curie',
'location': 'paris',
},
}

for username, user_info in users.items():
 print("\nUsername: " + username)
 full_name = user_info['first'] + " " + user_info['last']
location = user_info['location']
 print("\tFull name: " + full_name.title())
print("\tLocation: " + location.title())


函数 input() 的工作原理

message = input("Tell me something, and I will repeat it back to you: ")
prompt += "\nWhat is your first name? "
print(message)


使用 while 循环

current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
使用 break 退出循环
在循环中使用 continue
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: