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

一个简单的字典

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值