One, create a list
Just enclose the comma-delimited data items in square brackets.
1 list1=['physics','chemistry', 1997,2000] 2 list2=[1,2,3,4,5,6,7]
[Note]: 1. Data items in the list do not need to have the same data type
2. The index of the list starts at 0, and the list can be intercepted, combined
Second, access the value in the list
Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters
1list1=['Physics','Chemistry', 1997,2000]2list2=[1,2,3,4,5,6,7]3 4 Print('List1[0]:', List1[0])#output: list1[0]:p hysics5 Print('List2[1:5]:', List2[1:5])#output: [2,3,4,5]
Third, List script operators
|
P Ython-expression |
Results |
Describe |
| Len ([+]) |
3 |
Length |
| [1,2,3]+[4,5,6] |
[1,2,3,4,5,6] |
Combination |
| [' hi! '] * |
[' hi! ', ' hi! ', ' hi! ', ' hi! '] |
Repeat |
| 3 in [All in all] |
True |
Whether the element exists in the list |
| For x in [All-in-A-Z]: print (x) |
The |
Iteration |
Iv. List interception
The hypothetical list is as follows:
list=[1,2,3,4,5,6,7,8,9]
Operation:
| Python expressions |
Results |
Describe |
| LIST[2] |
3 |
Read the third element in the list |
| List[-2] |
8 |
Reads the second-to-last element in a list |
| List[1:4] |
[2,3,4] |
Intercepting an element from subscript 1 to subscript 4 (excluding elements of subscript 4), the result is a list |
| List[1:8:2] |
[2,4,6,8] |
Intercept elements starting from subscript 1 to subscript 8 (excluding elements of subscript 8), step 2, and result as a list |
| List[:3] |
[A] |
An element that is truncated from the first element of the list to the subscript 3 (excluding the element with subscript 3), resulting in a list |
| List[5:] |
[6,7,8,9] |
Intercepts an element from subscript 5 to the end of the list, and the result is a list |
| list[:] |
[1,2,3,4,5,6,7,8,9] |
Copy the entire list |
Python: List Operations Summary