Python Learning Note 3: Lists and tuples
A list of lists
Summary: The list is a mutable variable, and you can modify the value of the element by subscript
1. Definition list --brackets []
name=[' Lily ', ' Lucy ', ' Meimei '
2, subscript value --plus or minus number can be
3. Add a value to the list
1)Append () method, add a value at the end of the list
2)Insert () method, add a value at the specified position in the list
3)Extend () method to append a list to the end of a list
4. Remove values from the list
1) Thepop () method, which deletes the last element by default, and returns its value; the subscript of the parameter list element, you can delete the element at the specified position
2) Remove() method, Delete the specified value, the argument is the value in the list
3) Clear () method, clear list
4) del keyword deletes the specified element
5. Modify the values in the list
Format: listname[n]=xxx
6, Slice, take sub-list
format: listname[n:m], remove the value between the mark N to M, also Gu Tou disregard the tail
7. Sorting
1) Sort () method, ascending
2) Reverse () method, descending
8, the length of the list
Len () method to calculate the length of the list
9, the traversal of the list
The For loop iterates through the list, iterating through every element in the list
10. Other methods
1) index () returns the subscript of the element
2) count () Number of occurrences of the statistic element
Second, tuple tuple
Summary: tuples are immutable variables
1, definition, parentheses ()
name = (' Hello ', ' world ', ' python ')
2. Methods
Tuples have only two methods: count () Number of occurrences of the statistic element
Index () returns the subscript of an element
3. Note: Define tuples, if there is only one element, must end with a comma
Python Learning Note 3: Lists and tuples