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) stay Python Is a special variable . A list can hold multiple elements , And the elements can be of different data types .
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'>
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
Format :
list.append(elmnt)
Parameters :
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]
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]
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]
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
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 .
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]
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
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 (Dictionary) By key (Key) And the value (Value) form . Keys are unique .
Format :
dict = {key1:value1, key2, value2, key3, value3...}
Parameters :
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'>
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
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
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'}
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}
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 '
```
This article only has some cod
Catalog One 、 Actual combat s