>>>a=[1,2,3,4]
>>>a
[1,2,3,4]
Use the len() function to get the number of list elements
>>>len(a)
>>>4
Indices start at 0
>>>a[0]
>>>1
>>>a[1]
>>>2
If you take the last element, use -1 for the index
>>>a[-1]
>>>4
Of course, the 5th from the bottom crossed the line.
>>>a[-5]
list is a variable ordered list, so, you can append elements to the end of the list
>>>a.append(5)
>>>a
>>>[1,2,3,4,5]
>>>a.append(5) #If you don't enter it once, append it once. I have entered it many times
You can also insert elements at specified positions, such as inserting 0, at the first position
To delete the specified element, use pop(i), where i refers to the index position
Another type of ordered list is called a tuple: tuple. A tuple is very similar to a list, but once a tuple is initialized it cannot be modified
>>>a=(1,2,3,4,)
>>>a
>>>(1,2,3,4)
Now, the tuple of classmates cannot be changed, nor does it have methods such as append() and insert(). a[0]
Other methods of getting elements are the same as list, you can use a normally [-1]
, but you cannot assign it to another element
ist and tuple are Python's built-in ordered collections, one mutable and one immutable. Choose to use them as needed.