《Python编程 从入门到实践》day8

昨日知识回顾:

        字典键-值对的赋值、修改、输出

今日知识点学习:

6.3 遍历字典

        6.3.1 遍历所有的键值对

                for k,v in 字典名.items():

languages = {
    'Bob': 'C', #遗忘逗号会报错
    'Sunny': 'java',
    'Sarah': 'Python',
    'Phil': 'ruby',
}
for k,v in languages.items():
    print('name:' + k)
    print('language:' + v)

# 运行结果:
# name:Bob
# language:C
# name:Sunny
# language:java
# name:Sarah
# language:Python
# name:Phil
# language:ruby

                6.3.2 遍历字典中的所有键

                        for k in 字典名.keys():

languages = {
    'Bob': 'C', #遗忘逗号会报错
    'Sunny': 'java',
    'Sarah': 'Python',
    'Phil': 'ruby',
}
for k in languages.keys():
    print('name_lower:' + k.lower())


# 运行结果:
# name_lower:bob
# name_lower:sunny
# name_lower:sarah
# name_lower:phil

                6.3.3 按顺序遍历字典中的所有键

languages = {
    'Bob': 'C', #遗忘逗号会报错
    'Sunny': 'java',
    'Sarah': 'Python',
    'Phil': 'ruby',
}
for k in sorted(languages.keys()):
    print('name_lower:' + k.lower())


# 运行结果:
# name_lower:bob
# name_lower:phil
# name_lower:sarah
# name_lower:sunny

                6.3.4 遍历字典中的所有值

languages = {
    'Bob': 'C', #遗忘逗号会报错
    'Sunny': 'java',
    'Sarah': 'Python',
    'Phil': 'ruby',
}
for v in sorted(languages.values()):
    print('language:' + v.title())


# 运行结果:
# language:C
# language:Python
# language:Java
# language:Ruby

6.4 嵌套

        6.4.1 字典列表                

alien_0 = {'color':'red','point':10,'x_position':0,'y_position':0}
alien_1= {'color':'yellow','point':5,'x_position':0,'y_position':5}
alien_2= {'color':'blue','point':15,'x_position':5,'y_position':5}
aliens = [alien_0,alien_1,alien_2]

for alien_num in range(3,10):
    new_alien = {'color':'yellow','point':10,'x_position':0,'y_position':0}
    aliens.append(new_alien)
for alien in aliens[2:8]:
    if alien['color'] == 'yellow':
        alien['color'] = 'blue'
        print(alien)

# 运行结果:
# {'color': 'blue', 'point': 10, 'x_position': 0, 'y_position': 0}
# {'color': 'blue', 'point': 10, 'x_position': 0, 'y_position': 0}
# {'color': 'blue', 'point': 10, 'x_position': 0, 'y_position': 0}
# {'color': 'blue', 'point': 10, 'x_position': 0, 'y_position': 0}
# {'color': 'blue', 'point': 10, 'x_position': 0, 'y_position': 0}

        6.4.2 在字典中存储列表                

persons = {
   'name':'bob',
   'wealth':['car','house']
}
for k,v in persons.items():
    print(k,v)
persons['wealth'].append('deposit')
print('\n增加后:')
for w in persons['wealth']:
    print(w)

# 运行结果:
# name bob
# wealth ['car', 'house']
# 
# 增加后:
# car
# house
# deposit

        6.4.3 在字典中存储字典

persons = {
    'person1':{
        'name': 'bob',
        'wealth': ['car', 'house']
    },
    'person2': {
        'name': 'Alice',
        'wealth': ['deposit', 'house']
    }
}
for k,v in persons.items():
    print('per_inf:')
    print(k,v)


# 运行结果:
# per_inf:
# person1 {'name': 'bob', 'wealth': ['car', 'house']}
# per_inf:
# person2 {'name': 'Alice', 'wealth': ['deposit', 'house']}

第七章 用户输入和while循环

7.1 函数input()的工作原理

message = input('请输入信息:')
print(message)

# 运行结果:
# 请输入信息:2131
# 2131

        7.1.1 编写清晰的程序

                提示词应尽量清晰且易于明白

        7.1.2 使用int()来获取数值输入

                input()函数默认输入为字符串,如果想让输入转换为数字,可使用int()进行转换                

# num = input('请输入数字:')
# print(num > 19)

# 运行结果:
# TypeError: '>' not supported between instances of 'str' and 'int'

num = input('请输入数字:')
print(int(num) > 19)

# 运行结果:False

        7.1.3 求模运算符

num = int(input('请输入数字,我将告知你偶数还是奇数:'))
if num % 2 ==0:
    print('偶数')
else:
    print('奇数')

# 运行结果:
# 请输入数字,我将告知你偶数还是奇数:16
# 偶数

        7.1.4 在Python2.7中获取输入

                使用raw_input()提示用户输入

7.2 while 循环简介

        7.2.1 使用while循环                

current_num = 1
while current_num <= 5:
    print(current_num)
    current_num += 1

# 运行结果:
# 1
# 2
# 3
# 4
# 5

        7.2.2 让用户选择何时退出                

prompt = '告诉我你需要什么:'
prompt += "\n Enter 'Q' to end \n"

m = input(prompt)
if m != 'Q':
    print(m)

# 运行结果:# 告诉我你需要什么:
#  Enter 'Q' to end 
# Q

# 进程已结束,退出代码0

        7.2.3 使用标志

prompt = '告诉我你需要什么:'
prompt += "\n Enter 'Q' to end \n"
active = True #标志
while active:
    m = input(prompt)
    if m != 'Q':
        print(m)
    else:
        active = False
        print(active)

# 运行结果:告诉我你需要什么:
#  Enter 'Q' to end 
# Q
# False

# 进程已结束,退出代码0

        7.2.4 使用break 退出循环

prompt = '告诉我你去过的城市:'
prompt += "\nEnter 'Q' to end \n"

while True:
    City = input(prompt)
    if City == 'Q':
        break
    else:
       print('我去过' + City +'\n')

# 运行结果:告诉我你去过的城市:
# Enter 'Q' to end 
# 上海
# 我去过上海

# 告诉我你去过的城市:
# Enter 'Q' to end 
# Q

# 进程已结束,退出代码0

        7.2.5 在循环中使用continue

print('continue()函数继续执行余下代码并返回循坏开头')
Current_num = 0
while Current_num <= 10:
    Current_num += 1
    if Current_num % 2 == 0:
        continue

    print(Current_num)

print('\nbreak()函数直接退出循环,不再执行余下代码')
Current_num = 0
while Current_num <= 10:
    Current_num += 1
    if Current_num % 2 == 0:
        break

    print(Current_num)

# 运行结果:
#continue()函数继续执行余下代码并返回循坏开头
# 1
# 3
# 5
# 7
# 9
# 11
# 
# break()函数直接退出循环,不再执行余下代码
# 1

        7.2.6 避免出现无限循环

                确保循环条件有一处为False或break()能执行

  • 34
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值