current position:Home>Python from 0 to 1 (day 20) - basic concepts of Python dictionary

Python from 0 to 1 (day 20) - basic concepts of Python dictionary

2022-02-01 10:59:47 My family

This is my participation 11 The fourth of the yuegengwen challenge 25 God , Check out the activity details :2021 One last more challenge

The basic concepts of dictionaries

What is a dictionary

The dictionary is Python A common data type in , Used to store data with mapping relationships .

In our daily life , A lot of data " organize a team " appear : Such as book number and book 、 Commodity bar code and name 、 Student and subject scores .....

Each book has its own number , Each item will have its own bar code , Each student has a corresponding subject score ...…. The correspondence between these pairs of data belongs to the mapping relationship .

In short , You can understand the mapping relationship as a corresponding relationship between two data .

Using a dictionary to store these mapped data will be more concise , It's also easier to handle .

Next , I'll teach you to create a dictionary , Look at the grammar in the dictionary first :

We use a curly bracket {} Put these " Pairs of " Enclose the data , Such as : {' The small white ': 90, ' Little black ': 85}, You can define a dictionary .

In the dictionary , these " Pair " The data is called a key value pair , Such as :" The small white ":90 It's a key value pair .

The data in the dictionary is stored in the form of key value pairs , Use English commas between different key value pairs , separate .

The key value pair consists of the key (key) And the value (value) form , Keys and values use English colons : Connect , Key before colon , Value after colon .

In dictionary {' The small white ': 90, ' Little black ': 85} in , Such as name :' The small white ',' Little black ' It belongs to the key , Corresponding results :90,85 It belongs to value .

#  Create a dictionary as required , And assign to variable  info

info = {' full name ': ' Zhang San ', ' Age ': 22, ' height ': 175.6}

#  Print info

print(info)
 Copy code 

Next , Let's look at the dictionary info, Look at the data types of keys and values in the dictionary ?

info = {

    ' Personal profile ': (' The small white ', ' male '),

    ' Hobby ': [' game ', ' The movie '],

    ' Contact information ': {' Telephone ': 99996666, ' mailbox ': '[email protected]'}

}
 Copy code 

This dictionary " A little longer ", You can find the value in the dictionary , And say their data types ?

In order to make you observe the dictionary more clearly , I divided it into three lines to write ,:

Of course , Whether it's written in one line or three lines , The dictionary itself has not changed .

You can clearly see the dictionary info in 3 Key value pairs .

First key value pair : ' Personal profile ': (' The small white ', ' male ')

The key is ' Personal profile ', The corresponding value is (' The small white ',' male '), about (' The small white ',' male ') This data type , Do you have any impression ?

This is what we reviewed just now : Tuples .

The second key value pair : ' Hobby ': [' game ', ' The movie ']

The key is ' Hobby ', The corresponding value is [' game ',' The movie '], that [' game ',' The movie '] What data type does it belong to ?

you 're right , This is a list .

The third key value pair : ' Contact information ': {' Telephone ': 99996666, ' mailbox ': '[email protected]'}

The key is ' Contact information ', The corresponding value is ****{' Telephone ': 99996666, ' mailbox ': '[email protected]'}, It's a dictionary .

Tuples , list , A dictionary can also be used as a value in a dictionary .


Let's summarize the data types of keys and values in the dictionary we just explored :

copyright notice
author[My family],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011059457536.html

Random recommended