Python Data type No 1 Kind of : character string (str), What is enclosed in quotation marks .
Python Data type No 2 Kind of : Integers (int).
Python Data type No 3 Kind of : Floating point numbers (float), A number with a decimal point .
Python Data type No 4 Kind of : list (list), In square brackets [ ]
Express .
Python Data type No 5 Kind of : Tuples (tuple), Use parentheses ( )
Express .
Python Data type No 6 Kind of : Dictionaries (dict), Use braces { }
Express .
list 、 Tuples store ordered data , Orderly means by 0、1、2、3 In the order of .
Dictionaries store data with mapping relationships , Mapping can be understood as a correspondence .
list 、 The data stored in tuples is called elements .
The data stored in the dictionary is called a key value pair , Is an unordered sequence .
Dictionary use { }
Express , Use English commas between key value pairs ,
Separate , Use English colon between key and value :
Separate .
Dictionaries are variable data types , You can add, delete or change key value pairs .
Be careful :dict As Python Keywords and built-in functions , Variable names are not recommended to be named dict.
dictionary[ˈdɪkʃənri]: Dictionaries
The dictionary syntax is shown in the following figure :
# Create a new empty dictionary :
empty_dict = {
}
Create a new dictionary to store personal information
# Create a new dictionary
info_dict = {
' full name ':' Bai Jingting ',' Date of birth ':'1993 year 10 month 15 Japan '}
# View dictionary
info_dict
【 Terminal output 】
{' full name ': ' Bai Jingting ', ' Date of birth ': '1993 year 10 month 15 Japan '}
There are two key value pairs in the above dictionary ,‘ full name ’: ' Bai Jingting ’ Is the first ,‘ Date of birth ’: '1993 year 10 month 15 Japan ’ It's the second one. .
' full name ’ It's a key ,' Bai Jingting ’ Is the value of the key .
grammar : Dictionaries [ key ]=[ value ]
# Create a new dictionary
info_dict = {
' full name ':' Bai Jingting ',' Date of birth ':'1993 year 10 month 15 Japan '}
# Add a key value pair : height 188.8
info_dict[' height '] = 188.8
# View dictionary
info_dict
【 Terminal output 】
{' full name ': ' Bai Jingting ', ' Date of birth ': '1993 year 10 month 15 Japan ', ' height ': 188.8}
grammar :del Dictionaries [ key ]
del After space .
delete [dɪˈliːt]: Delete .
# Create a new dictionary
info_dict = {
' full name ':' Bai Jingting ',' Date of birth ':'1993 year 10 month 15 Japan '}
# Delete a key value pair : Date of birth
del info_dict[' Date of birth ']
# View dictionary
info_dict
【 Terminal output 】
{' full name ': ' Bai Jingting '}
grammar : Dictionaries [ key ]=[ The new value ]
# Create a new dictionary
info_dict = {
' full name ': ' Bai Jingting ', ' Date of birth ': '1993 year 10 month 15 Japan ', ' height ': 188.8}
# Change the height to 180
info_dict[' height '] = 180
# View dictionary
info_dict
【 Terminal output 】
{' full name ': ' Bai Jingting ', ' Date of birth ': '1993 year 10 month 15 Japan ', ' height ': 180}
Method 1 : Dictionaries [ key ]
Method 2 : Dictionaries .get( key )
Method 3 : Dictionaries [ key 1][ key 2]
# Create a new dictionary
info_dict = {
' Basic information ':(' Bai Jingting ',' male ','1993 year '),
' Personal hobbies ':[' Bodybuilding ',' swimming '],
' Contact information ':{
' Wechat number ':'bjt77',' Phone number ':18087578757}
}
# Check the dictionary for personal hobbies
info_dict[' Personal hobbies ']
【 Terminal output 】
[' Bodybuilding ', ' swimming ']
use info_dict[‘ Personal hobbies ’] You can view the dictionary key personal hobby value Fitness 、 swimming .
# Create a new dictionary
info_dict = {
' Basic information ':(' Bai Jingting ',' male ','1993 year '),
' Personal hobbies ':[' Bodybuilding ',' swimming '],
' Contact information ':{
' Wechat number ':'bjt77',' Phone number ':18087578757}
}
# Check the dictionary for personal hobbies
info_dict.get(' Personal hobbies ')
【 Terminal output 】
[' Bodybuilding ', ' swimming ']
# Create a new dictionary
info_dict = {
' Basic information ':(' Bai Jingting ',' male ','1993 year '),
' Personal hobbies ':[' Bodybuilding ',' swimming '],
' Contact information ':{
' Wechat number ':'bjt77',' Phone number ':18087578757}
}
# Check wechat number
info_dict[' Contact information '][' Wechat number ']
【 Terminal output 】
'bjt77'
Dictionary values are also dictionaries , Using a dictionary [ key 1][ key 2] The way to take value .
# Create a new dictionary
info_dict = {
' Basic information ':(' Bai Jingting ',' male ','1993 year '),
' Personal hobbies ':[' Bodybuilding ',' swimming '],
' Contact information ':{
' Wechat number ':'bjt77',' Phone number ':18087578757}
}
# View dictionary
info_dict
【 Terminal output 】
{' Basic information ': (' Bai Jingting ', ' male ', '1993 year '),
' Personal hobbies ': [' Bodybuilding ', ' swimming '],
' Contact information ': {' Wechat number ': 'bjt77', ' Phone number ': 18087578757}}
In the above Dictionary 3 Key value pairs :
The first key Basic information
The value of is tuple , Parentheses denote tuples :
The second key Personal hobbies
The value of is list , Brackets indicate a list ;
The third key Contact information
The value of is dictionary , Curly braces mean dictionaries .
The keys in the dictionary are immutable data types , Like strings ;
The values in the dictionary can be of any data type .
You can write your own code test .
Create a dictionary that contains the following :
A grade has 3 A class , Class one 、 Class two 、 Class three ;
Class one 50 people , average 70 branch , The highest 88, Lowest score 66;
Class two 60 people , average 80 branch , The highest 99, Lowest score 77;
Class three 70 people , average 90 branch , The highest 99, Lowest score 88.