python学习(字典、用户输入和while循环)

前言:上次学习到了if语句,这次接着进行学习。

字典

字典能够准确地为各种真实物体建模,且能够将相关信息关联起来。

使用字典

字典是一系列的键——值对,一个建对应一个值,值可以为数字、字符串等
在Python中,字典用放在花括号{} 中的一系列键—值对表示。

访问字典中的值

score = {'shuxu':'80','yuwen':'90'}

print(score['shuxu'])
print(score['yuwen'])
#输出结果:
80
90

添加键——值对

字典是一种动态结构,可随时在其中添加键—值对
添加时值用方括号[]括起来

score = {'shuxu':'80','yuwen':'90'}

print(score)

score['wuli'] = 60
score['yingyu'] = 90
print(score)
#输出结果:
{'shuxu': '80', 'yuwen': '90'}
{'shuxu': '80', 'yuwen': '90', 'wuli': 60, 'yingyu': 90}

创建一个空字典

score = {}

score['wuli'] = 60
score['yingyu'] = 90
print(score)
#输出结果:
{'wuli': 60, 'yingyu': 90}

修改字典中的值

score = {'yuwen':'80'}
print(score)
score['yuwen'] = '90'
print(score)
#输出结果:
{'yuwen': '80'}
{'yuwen': '90'}

删除键——值对

使用del 语句可以将相应的键—值对彻底删除,使用del 语句时,必须指定字典名和要删除的键。

score = {'yuwen':80,'shuxu':90}
print(score)
del score['shuxu']
print(score)
#输出结果:
{'yuwen': 80, 'shuxu': 90}
{'yuwen': 80}

由类似对象组成的字典
如果用字典来存储众多对象的同一种信息,可以用这样的形式

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
print("he score is"+
    yuwen_score['he'])
#输出结果:
he score is70

遍历所有的键——值对

遍历键值——对时,可声明两个变量,用于存储键—值对中的键和值。对于这两个变量,可使用任何名称。

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
for key,value in yuwen_score.items():
    print("\nkey: " + key)
    print("value: " + value)
#items() 函数以列表返回可遍历的(键, 值) 元组数组。
#输出结果:
key: me
value: 90

key: you
value: 80

key: he
value: 70

key: she
value: 60

遍历字典中的所有键

keys() 函数返回一个列表包含所有键

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
for key in yuwen_score.keys():
    print("\nkey: " + key)
#输出结果:
key: me

key: you

key: he

key: she

按顺序遍历字典中的所有值

函数sorted() 来获得按特定顺序排列的键列表的副本

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
for key in sorted(yuwen_score.keys()):
    print("\nkey: " + key)
#输出结果:
key: he

key: me

key: she

key: you    

遍历字典中的所有值

方法values() ,返回一个值列表,不包含任何键。

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'60',
    }
for score in yuwen_score.values():
    print("\nscore: " + score)
#输出结果:
score: 90

score: 80

score: 70

score: 60

如果值中有重复的,可以用集合set,如:

yuwen_score = {
    'me': '90',
    'you':'80',
    'he':'70',
    'she':'90',
    }
for score in set(yuwen_score.values()):
    print("\nscore: " + score)
#输出结果:
score: 90

score: 80

score: 70

嵌套

将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套

字典列表

score_0 = {'subject':'yuwen','point':60}
score_1 = {'subject':'shuxu','point':70}
score_2 = {'subject':'yingyu','point':80}

scores = [score_0,score_1,score_2]

for score in scores:
    print(score)
#输出结果:
{'subject': 'yuwen', 'point': 60}
{'subject': 'shuxu', 'point': 70}
{'subject': 'yingyu', 'point': 80}

在字典中储存列表

每当需要在字典中将一个键关联到多个值时,都可以在字典中嵌套一个列表。

school = {
    'teacher': 'wang',
    'subjects': ['shuxu','yuwen'],
    }
print(school['teacher'])

for subject in school['subjects']:
    print("\t" + subject)
#输出结果:
wang
	shuxu
	yuwen

用户输入和while循环

函数input

函数input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中。
使用函数input() 时,输入解读为字符串

message = input("please input message:\n")
print("message is:"+message)
#输出结果:
please input message:
22222
message is:22222

函数int

使用函数int() 来获取数值输入 ,input()解读的为字符串,不能直接和整数比较

在这里插入图片描述
将数值输入用于计算和比较前,需将其转换为数值表示.

求模运算符

%,将两个数相除并返回余数

print(4 % 3)
print(5 % 3)
print(6 % 3)
#输出结果:
1
2
0

使用while 循环
通过一个例子来了解while的语法

number = 1
while number <= 5:
    print(number)
    number +=1
#输出结果:
1
2
3
4
5

在循环中使用continue

返回到循环开头,并根据条件测试结果决定是否继续执行循环

number = 0
while number <10:
    number += 1
    if number % 2 ==0:
        continue

    print(number)
 #输出结果:
 1
3
5
7
9

在列表之间移动元素

numbers = ['a','b','c']
confirmed_numbers = []

while numbers:
    middle_number = numbers.pop()#删除末尾赋给新的变量
    confirmed_numbers.append(middle_number)

for confirmed_number in confirmed_numbers:
    print(confirmed_number.title())
#输出结果:
C
B
A

删除包含特定值的所有列表元素

messages = ['a','b','c','d','a','a']
print(messages)

while 'a' in messages:
    messages.remove('a')

print(messages)
#输出结果:
['a', 'b', 'c', 'd', 'a', 'a']
['b', 'c', 'd']

使用用户输入来填充字典

#创建一个空字典
responses = {}
#设置一个标志
active = True

while active:
    name = input("\nWhat is your name?")
    like_food = input("your like food is ?")
    
    #将答案存储在字典中
    responses[name] = like_food

    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat =='no':
        active = False
for name,like_food in responses.items():
    print(name+":"+like_food)
#输出结果:
What is your name?222
your like food is ?222
Would you like to let another person respond? (yes/ no) no
222:222

这次就先学习到这,下次继续学习。

发布了71 篇原创文章 · 获赞 80 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43431158/article/details/97665838