As you all know, lists are the most important data type in Python, so there's a lot of content in this section. I've read this before, but I haven't started programming, so it's uncomfortable and strongly recommended << Python programming: From Getting Started to Practice>, this book is very systematic and suitable as an introductory book.
1. Create lists
This is simple, use brackets []
names=['stark','alice','david','danny'] >>> print(names) ['stark', 'alice', 'david', 'danny']
Create an empty list
number=[] >>> print(number) []
2. Traverse Lists
Use for in list:
for name in names: print(name) stark alice david danny
for is followed by an identifier that you can name as you like, but that meets the requirements of an identifier
3. Create a list of values
Here we're going to touch on a new method, range(), which works like this. We'll print a square from 1 to 10
for num in range(1,11): print(num**2) 1 4 9 16 25 36 49 64 81 100
The range(1,11) represents numbers from 1 to 10, but does not include 11. Be careful
Create a list of values:
number=list(range(1,11)) >>> print(number) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Use the range() function to create almost any desired list of numbers
You can use the max(), min(), sum() functions to find the maximum, minimum, and list number sum in a list of numbers
>>> sum(number) 55 >>> max(number) 10 >>> min(number) 1
4. List Resolution
>>> square=[value**2 for value in range(1,11)] >>> print(square) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
This part is not really hard to understand, it's equivalent to defining a list from 1-10, traversing the list to find the square, and printing the results
5. List Slice
Some elements used to process lists
Prints the names of the first three players in the players list, [] The first number represents the start, the second number represents the end, but does not include the end element, and the output of the list slice remains the list>>> players = ['charles', 'martina', 'michael', 'florence', 'eli'] >>> players[0:3] ['charles', 'martina', 'michael']
If the first number is omitted, the default is the starting element, as follows:
>>> players[:3] ['charles', 'martina', 'michael']
To print the names of the last three team members, do the following:
>>> players[2:] ['michael', 'florence', 'eli']
>>> players[-3:] ['michael', 'florence', 'eli']
Copy list:
>>> team_players=players[:] >>> print(team_players) ['charles', 'martina', 'michael', 'florence', 'eli']
>>> players.append('alice') >>> print(players) ['charles', 'martina', 'michael', 'florence', 'eli', 'alice'] >>> print(team_players) ['charles', 'martina', 'michael', 'florence', 'eli']
We've created a list called team_players with the same elements as in players, so let's look at another operation
This creates a list of teamplayers equal to the players, and when the players change, the teamplayers change, whereas the list created with [:] above does not change one thing or the other that follows the change>>> team_players=players >>> players.append('alice') >>> print(players) ['charles', 'martina', 'michael', 'florence', 'eli', 'alice'] >>> print(team_players) ['charles', 'martina', 'michael', 'florence', 'eli', 'alice']
6. Tuples
Tuples are also lists, but they cannot be changed. Tuples are created using ()
We put a person's height and weight in the feature tuple, and when we try to change it, we get errors
>>> feature=(166,50) >>> print(feature) (166, 50) >>> print(feature[0]) 166 >>> feature[0]=170 Traceback (most recent call last): File "<pyshell#90>", line 1, in <module> feature[0]=170 TypeError: 'tuple' object does not support item assignment >>>
Although elements of tuples cannot be changed, tuples can be reassigned
>>> feature=(170,55) >>> print(feature[0]) 170
That's all for this part of the list. There should be a lot more to learn. Go on!