程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python basics 2022 latest] lesson 3 list & Dictionary

編輯:Python

【Python Basics 2022 newest 】 The third class list & Dictionaries

  • summary
  • list
    • Create a list of
  • Add, delete, change and check the list
    • Indexes
    • increase
    • Delete
    • Change
    • check
  • Other operating
    • Slicing operation
    • Merge list
    • Look for the element
    • Common mistakes
  • Dictionaries
    • Create a dictionary
    • Check the value
  • Dictionary operation
    • ACCESS Dictionary values
    • Add key value pair
    • Revise the dictionary
    • Key repeat

summary

Starting today , Xiaobai, I will lead you to learn Python Introduction to zero foundation . This column will explain + Practice mode , Let's get familiar with it Python The grammar of , application , And the basic logic of the code .

list

list (List) stay Python Is a special variable . A list can hold multiple elements , And the elements can be of different data types .

Create a list of

Example :

list1 = [1, 2, 3] # Create by integer type (int) Make an array of
list2 = [" I'm Xiaobai ", True] # Create an array containing different data types
print(list1, list2) # Debug output
print(type(list1), type(list2)) # Debug output type

Output results :

[1, 2, 3] [' I'm Xiaobai ', True]
<class 'list'> <class 'list'>

Add, delete, change and check the list

Indexes

Indexes (Index) It's a special data structure , A catalogue similar to a book . The list assigns an index to each element (index), from 0 Start pushing back . Through the index, we can update the list , Remove elements , Section operation .

Example :

list1 = [" I'm Xiaobai ", " Wang Ergou ", " Li Tiezhu "] # Create a list of
print(" Indexes 0:", list1[0]) # Debug output elements by indexing 1
print(" Indexes 1:", list1[1]) # Debug output elements by indexing 2
print(" Indexes 2:", list1[2]) # Debug output elements by indexing 3

Output results :

 Indexes 0: I'm Xiaobai
Indexes 1: Wang Ergou
Indexes 2: Li Tiezhu

increase

Format :

list.append(elmnt)

Parameters :

  • elmnt: Elements added at the end of the array

Example :

# Create a list of
list1 = [12, 23, 34, 45]
print(" Original list :", list1) # Debug output
# utilize append Add... At the end
list1.append(56)
print("append List after :", list1) # Debug output
# utilize insert Additive elements
list1.insert(0, 99) # Insert an element into the column header , 99
print("insert List after :", list1) # Debug output

Output results :

 Original list : [12, 23, 34, 45]
append List after : [12, 23, 34, 45, 56]
insert List after : [99, 12, 23, 34, 45, 56]

Delete

Example :

# Create a list of
list1 = [12, 23, 34, 45]
print(" Original list :", list1) # Debug output
# Delete index as 0 The elements of , namely 12
del list1[0]
print(" List after deleting elements :", list1) # Debug output

Output results :

 Original list : [12, 23, 34, 45]
List after deleting elements : [23, 34, 45]

Change

Example :

# Create a list of
list1 = [12, 23, 34, 45]
print(" Original list :", list1) # Debug output
# Modify the index 0 The value of the corresponding element
list1[0] = 0
print(" The list after modifying the element :", list1) # Debug output

Output results :

 Original list : [12, 23, 34, 45]
The list after modifying the element : [0, 23, 34, 45]

check

Example :

list1 = [" I'm Xiaobai ", " Wang Ergou ", " Li Tiezhu "] # Create a list of
print(" Indexes 0:", list1[0]) # Debug output elements by indexing 1
print(" Indexes 1:", list1[1]) # Debug output elements by indexing 2
print(" Indexes 2:", list1[2]) # Debug output elements by indexing 3

Output results :

 Indexes 0: I'm Xiaobai
Indexes 1: Wang Ergou
Indexes 2: Li Tiezhu

Other operating

Slicing operation

Format :

list[ Starting index ( contain ) : End index ( It doesn't contain )]
list[ Starting index ( contain ) : End index ( It doesn't contain ): interval ]

example 1:

# Create a list of
list1 = [" I'm Xiaobai ", " Wang Ergou ", " Zhang San ", " Li Si ", " Li Tiezhu "]
print(list1[0:2]) # Slicing operation
print(list1[:2]) # Same as the previous line , Omit the starting index

Output results :

[' I'm Xiaobai ', ' Wang Ergou ']
[' I'm Xiaobai ', ' Wang Ergou ']


example 2:

# Create a list of
list1 = [" I'm Xiaobai ", " Wang Ergou ", " Zhang San ", " Li Si ", " Li Tiezhu "]
print(list1[::2]) # Slicing operation , Every time 2 Extract an element
print(list1[::3]) # Slicing operation , Every time 3 Extract an element

Output results :

[' I'm Xiaobai ', ' Zhang San ', ' Li Tiezhu ']
[' I'm Xiaobai ', ' Li Si ']

a key : The sliced list contains elements at the starting index position , But it doesn't contain the element that ends the index position .

Merge list

Example :

# Create a list of
list1 = [1, 2, 3, 4] # Create a list of 1
list2 = [5, 6, 7, 8] # Create a list of 2
list_combine = list1 + list2 # Merge list
print(list_combine) # Debug output

Output results :

[1, 2, 3, 4, 5, 6, 7, 8]

Look for the element

Through the command in Determine whether there are elements in the list .

Format :

 Elements in list

Example :

# Create a list of
list = [1, 2, 3, 4]
# Determine whether the list contains elements
print(1 in list)
print(2 in list)
print(3 in list)
print(4 in list)
print(5 in list)
print(6 in list)

Output results :

True
True
True
True
False
False

Common mistakes

The array subscript is out of the defined range .

Example :

list1 = [1, 2, 3, 4]
print(list1[0])
print(list1[1])
print(list1[2])
print(list1[3])
print(list1[4]) # list1 There is no index in the list 4

Output results :

1
2
3
4
Traceback (most recent call last):
File "C:/Users/Windows/Desktop/ lecture / The second lesson / The second lesson Common mistakes 1.py", line 6, in <module>
print(list1[4])
IndexError: list index out of range

Dictionaries

Dictionaries (Dictionary) By key (Key) And the value (Value) form . Keys are unique .

Create a dictionary

Format :

dict = {key1:value1, key2, value2, key3, value3...}

Parameters :

  • key: key
  • value: value

Example :

# Create a dictionary
dict = {" name ": " I'm Xiaobai, alas ", " Age ": 20}
print(dict) # Debug output
print(type(dict)) # Debug output type

Output results :

{' name ': ' I'm Xiaobai, alas ', ' Age ': 20}
<class 'dict'>

Check the value

Example :

# Create a dictionary
dict = {" name ": " I'm Xiaobai, alas ", " Age ": 20}
print(dict[" name "]) # Get the corresponding value through the key
print(dict[" Age "]) # Get the corresponding value through the key

Output results :

 I'm Xiaobai, alas
20

Dictionary operation

ACCESS Dictionary values

Method 1, adopt items() Command to get dictionary key value pairs :

# Create a dictionary
dict = {" name ": " I'm Xiaobai, alas ", " Age ": 20}
# Method 1
for key, value in dict.items():
# Output each key value pair
print(" key :", key, " value :", value)

Output results :

{' name ': ' I'm Xiaobai, alas ', ' Age ': 20}
key : name value : I'm Xiaobai, alas
key : Age value : 20

Method 2, Get the corresponding value through the key :

# Create a dictionary
dict = {" name ": " I'm Xiaobai, alas ", " Age ": 20}
# Method 2
for key in dict:
# Output each key value pair
value = dict[key]
print(" key :", key, " value :", value)

Output results :

 key : name value : I'm Xiaobai, alas
key : Age value : 20

Add key value pair

Example :

# Create a dictionary
dict = {" name ": " I'm Xiaobai, alas ", " Age ": 20}
print(dict) # Debug output
# Add key value pair
dict[" Position "] = " New York Benail CEO"
print(dict) # Debug output '

Output results :

{' name ': ' I'm Xiaobai, alas ', ' Age ': 20}
{' name ': ' I'm Xiaobai, alas ', ' Age ': 20, ' Position ': ' New York Benail CEO'}

Revise the dictionary

Example :

# Create a dictionary
dict = {" name ": " I'm Xiaobai, alas ", " Age ": 20}
print(dict) # Debug output
# Modify the value with the key
dict[" Age "] = 18 # Xiaobai forever 18, Do not accept rebuttal
print(dict) # Debug output

Debug output :

{' name ': ' I'm Xiaobai, alas ', ' Age ': 20}
{' name ': ' I'm Xiaobai, alas ', ' Age ': 18}

Key repeat

Example :

# Create a dictionary
dict = {" name ": " I'm Xiaobai, alas ", " Age ": 20, " Age ": 18}
print(dict) # Debug output

Output results :

{' name ': ' I'm Xiaobai, alas ', ' Age ': 18}
```
notes : When the key repeats , Keep the latest value , As a key value pair .
## Common mistakes
The key doesn't exist :
```
# Create a dictionary
dict = {" name ": " I'm Xiaobai, alas ", " Age ": 20}
# Debug the value corresponding to the output key
print(dict[" name "])
print(dict[" Age "])
print(dict[" Position "]) # The key does not exist
```
Output results :
```
I'm Xiaobai, alas
20
Traceback (most recent call last):
File "C:/Users/Windows/Desktop/ lecture / The third class / The third class Common mistakes in dictionaries .py", line 7, in <module>
print(dict[" Position "])
KeyError: ' Position '
```

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved