Introduction: The last study to if语句
, followed by the study.
dictionary
Dictionary for a variety of real objects can be modeled accurately and to associate related information together.
Use a dictionary
Is a series of dictionaries
键——值对
, a value corresponding to a building, may be a numeric value, a string, etc.
in Python, with dictionaries in花括号{}
a bunch of keys - representing value.
Value access dictionary
score = {'shuxu':'80','yuwen':'90'}
print(score['shuxu'])
print(score['yuwen'])
#输出结果:
80
90
Add key - value pairs
Dictionary is a dynamic structure, where at any time to add the key - the value of
adding a time when方括号[]
enclosed
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}
Create an empty dictionary
score = {}
score['wuli'] = 60
score['yingyu'] = 90
print(score)
#输出结果:
{'wuli': 60, 'yingyu': 90}
Modify the value of the dictionary
score = {'yuwen':'80'}
print(score)
score['yuwen'] = '90'
print(score)
#输出结果:
{'yuwen': '80'}
{'yuwen': '90'}
Delete key - value pairs
Use
del 语句
may be appropriate key - the value to completely remove, use the del statement, you must specify the name of the dictionary and you want to remove the key.
score = {'yuwen':80,'shuxu':90}
print(score)
del score['shuxu']
print(score)
#输出结果:
{'yuwen': 80, 'shuxu': 90}
{'yuwen': 80}
Dictionary of similar objects in
If object dictionary to store a number of the same information, this may be in the form of
yuwen_score = {
'me': '90',
'you':'80',
'he':'70',
'she':'60',
}
print("he score is"+
yuwen_score['he'])
#输出结果:
he score is70
Loop over the keys - value pairs
Traversal key - when, two variables may declare, for storing the key values and the key pair. For these two variables, you can use any name.
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
All keys to scroll through the dictionary
keys () function returns a list of all the 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
All values in order to traverse the dictionary
Function
sorted()
to obtain按特定顺序排列
a copy of the key list
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
All values traversal dictionary
The method values (), returns a list of values, does not contain any keys.
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
If there are duplicate values can be used 集合set
, such as:
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
Nesting
The series of the dictionary stored in a list, or a list of values stored in a dictionary, which is called a nested
Dictionary list
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}
List stored in the dictionary
Whenever required to associate a plurality of key values in the dictionary, a list can be nested in the dictionary.
school = {
'teacher': 'wang',
'subjects': ['shuxu','yuwen'],
}
print(school['teacher'])
for subject in school['subjects']:
print("\t" + subject)
#输出结果:
wang
shuxu
yuwen
And a user input while loop
Function input
Function input () let the program pauses, waiting for the user to enter some text. After obtaining user input, Python stores it in a variable.
When using the function input (), read as input字符串
.
message = input("please input message:\n")
print("message is:"+message)
#输出结果:
please input message:
22222
message is:22222
Int function
Using the function int () to obtain the input value, INPUT () interpretation as a string, an integer, and can not be compared directly
Before the input values for calculating and comparing, which need to be converted to a numeric representation.
Modulus operator
%
Will divide two numbers and returns the remainder
print(4 % 3)
print(5 % 3)
print(6 % 3)
#输出结果:
1
2
0
Use while cycling
through an example to understand while syntax
number = 1
while number <= 5:
print(number)
number +=1
#输出结果:
1
2
3
4
5
Use continue in a loop
Return to the beginning of the cycle, and decide whether to continue cycling conditions according to the test results
number = 0
while number <10:
number += 1
if number % 2 ==0:
continue
print(number)
#输出结果:
1
3
5
7
9
In the mobile element between lists
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
Delete all list elements contain a specific value
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']
Use user input to populate dictionary
#创建一个空字典
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
This time it is the first study to this, the next time to continue learning.