In actual development, it is often necessary to store a set (more than one) of data for later code use.
Having said that, some readers may have heard of Array, which can store multiple data one by one, and
each element in the array can be accessed through the array index
To be clear, there are no arrays in Python, but more powerful lists have been added. If you think
of an array as a container, then a Python list is a factory repository.
Formally, the list will enclose all elements in a pair of square brackets , with adjacent
elements separated by commas, as shown below:
[element1, element2, element3, ..., elementn]
In the format, element1 ~ elementn represent the elements in the list, there is no limit to the
number, as long as it is a data type supported by Python.
In terms of content, lists can store any type of data such as integers, decimals, strings,
lists, and tuples, and the types of elements in the same list can also be different. For example:
["www.freelearningpoints.com", [7, 8, 9], 8.8]
As you can see, the list contains data types such as string, integer, list, and floating point.
Note that when using lists, although different types of data can be put into the same list, usually it
is not advisable. Because only the same type of data is put into the same list, which can improve the
readability of the program.
In addition, in other Python tutorials, list is often used to refer to a list, because the data type of a
list is a list, which can be known through the type () function, for example:
A list is a type of Python sequence. We can use an index to access an element in the list (getting
the value of an element), or we can use a slice to access a set of elements in the list
(getting a new Sublist).
The format for accessing list elements using an index is:
Among them, listname represents the name of the list, and i represents the index value. The index of the
list can be positive or negative.
The format for accessing list elements using slices is:
listname [start: end: step]
Among them, listname represents the name of the list, start represents the starting index, end represents
the ending index, and step represents the step size.
The above two methods have been explained in the "Python sequence", so I won't repeat them here,
just for example demonstration, please see the following code:
url = list ("www.freelearningpoints.com")
#Access an element in a list using an index
print (url ) #Use positive index
print (url [-9]) #Use negative index
#Access a set of elements in a list using a slice
print (url [2: 18]) #Use positive slice
print (url [2: 18: 3]) #Specify the step size
print (url [-9: -1]) #Use negative slice
For the list that has been created, if it is no longer used, you can delete it using the del keyword.
In actual development, del is not often used to delete lists, because Python's built-in garbage collection
mechanism will automatically destroy useless lists. Even if the developer does not delete them manually,
Python will automatically recycle them.
The syntax of the del keyword is:
Where listname is the name of the list to delete.
[Example] Python delete list:
list_1 = ["www.freelearningpoints.com"]
The output is:
NameError Traceback (most recent call last)
[ipython-input-54-a17d8657c9d0] in [module]
3 del list_1
----> 4 print(list_1)
NameError: name 'list_1' is not defined