In the previous study, you can find that lists are everywhere, as if Python syntax revolves around lists. This section will introduce lists in detail. Lists are considered by most Python users to be the core part of Python, because Python The list is very powerful, and many of our operations are based on the list.
First of all, let’s introduce the list . A list is an object that can contain multiple data types. The content in the list can be changed. It is a dynamic data structure. We can add or delete it, so in the list The operation is inseparable from the use of indexes.
In fact, you can see our definition of list many times before. In Python, we cannot simply define a variable name. For example, if we want to use a list next, we cannot define a my_list first and then proceed. Assignment or something, so the way we define the list is:
my_liss=[]#Define an empty list my_list=[1,2,3,4,5,6]#Define a list of existing values my_lizz=['a','b','c','d']
We can think of a list as a kind of container. We use it to store things. We can store the same type of data or different types of data in it. However, in order to improve the readability of the program, it is recommended to store the same type of data in a list. data type.
Sometimes we need to create a list with a certain value, but we don’t want to enter it manually, so we can use the list() function to nest the range() function to create it directly.
The list() function can not only perform forced type conversion to convert strings or tuples into lists, but can also use the list method when defining.
For example:
We want to create a list containing numbers from 1 to 10, then we can use the following method:
my_list=list(range(1,11))print(my_list)
The output is:
[1,2,3,4,5,6,7,8,9,10]
This way we can quickly create a list of what we want.
The list is deleted using the del statement. The format is:
delmy_list
Look at the following example:
my_list=[]delmy_list#Delete this created list print(my_list)#Try to output it
The output is:
FileD:/python/p/test.py,line3,in<module>print(my_list)NameError:name'my_list'isnotdefined
Through the error message, we can know that the list we created has been deleted.
Sometimes we need to make a copy of a list. At this time we need to copy the elements in the list. We can think about it first. If we define a list first, and then define a list, let the second list be equal to the first list. , after we modify the values in the first list, will the values in the second list change?
Look at the code below:
first_list=[1,2,3,4]#First define a list second_list=first_list#Copy this list print('Before modification (first list and second list): ', first_list,',', second_list) #Look at the output first_list[0]=10print('After modification (first list and second list):', first_list,',',second_list) #Look at the output again print(id(first_list),id( second_list))#By accessing the id, you can find that the two list addresses are the same at this time
The output is:
Before modification (first list and second list): [1,2,3,4], [1,2,3,4] After modification (first list and second list): [10, 2,3,4],[10,2,3,4]26239519545042623951954504
It can be found that the copied second list is also modified after the element value in the first list is modified, indicating that they are using the same list.
Take a look at the diagram:
In other words, the two variables use the same list in memory. No matter which element in the list is modified, the corresponding list is the same.
If you want to use lists with the same content but independently, you can use the following method:
first_list=[1,2,3,4]#First define a list second_list=[]+first_list#Use the connector print(id(first_list),id(second_list))#By accessing the id, you can find that the two lists are independent of each other
The output is:
18998579287761900113448584
Regarding lists, the following is all about them. This tutorial may seem too verbose compared to other tutorials, but everyone must understand that the core of Python’s data structure is the list. After thoroughly studying all aspects of the list, no matter what Whether it is competition or project development, it is of great help, so learning the list is very important.