Python Study Notes (1) -- list and tuple, pythontuple
List: a variable sequence.
Usage:
1. Statement
S = ["aaaaaa", "bbbbbbbbbb"];
2. Access the first element
S [0]
3. The last element
S [-1]
4. Add an element after the last element
S. append ("cccccccccccc ")
5. Insert a value at Index 1.
S. insert (1, "aaaaaaaabbbb ")
6. Delete the last element.
S. pop ()
7. Delete the element whose index is 1
S. pop (1)
8. assign a new value to an index.
S [1] = "aaaaaaaaaaa"
9. view the number of elements in the list
Len (s)
Note:
1. The list element can be another list
S = ["aaaaaa", "bbbbbbbbb", ["cccccc", "eeeeeeee"], "ffffffff"]
You can access s [2] [0] to obtain the cccccc value.
2. The elements in the list can be of different types.
S = ["aaa", 343]
Tuple: ordered list. The difference between a group and a list is immutable.
1. Statement
S = ("aaaa", "bbb", "ccccccccc ")
2. Obtain the first element.
S [0]
3. Get the last element
S [-1]
Note:
1. Use the same method as list. You cannot add elements.
2. When only one element is defined
S = (1 ,)
3. The tuple can contain a list.
S = (1, 2, ["ad", "cccc"])