When using the Python list, we need to learn many key points. Next we will look at how to use the Python list. The Python list is a set of values of any type, which are combined in a certain order. The value of the List is called Elements ).
Each element is identified as an index, and the first index is 0. The elements in the list can be of any type or even list type. That is to say, the list can be nested drops. The elements in the list are enclosed in brackets and separated by commas. For example:
- 1 a = [1,2,3,4]2 b = [1.0,2.0,3,4,"5"]3 c = [1,2.0,"3",[1,2,3,4]]
The element in the Python list can also be a variable, but changing the value of the variable does not affect the value of the element in the list. For example:
- # Coding: UTF-8
- E = 1
- F = 2
- G = 3
- H = [e, f, g, 4]
- Print h # result = [1, 2, 4]
- Print type (h) # <type 'LIST'>
- E = 5
- F = 6
- G = 7
- Print h # result = [1, 2, 4] instead of [5, 6, 7, 4]
For a list containing only integers, Python also provides several other methods to create a list:
(1) range (n, m) function range returns an integer list. The list starts from the first n parameter of the function and ends with the last m parameter, but does not contain the last m parameter, the difference between two adjacent numbers is 1.
Example:
- a = range(1,4)
- print a #result = [1,2,3]
(2) The range (n) function range generates a list starting from 0. The most given parameter n ends, but does not contain the given parameter n.
Example:
- a=range(8)
- print a #result = [0,1,2,3,4,5,6,7]
(3) The range (n, m, k) function generates a list of mathematical ascending arithmetic numbers. The start value is n, the end value is m, and the step size is k.
Example:
- a = range(1,20,4)
- print a #result = [1,5,9,13,17]
There is also a special list that does not contain any elements, which is called an empty list. The empty list is expressed as []. The Python list can be assigned a value or passed to the function as a parameter.