A, list 1, definition:
List is an ordered set of elements that can be added and removed at any time
2. Declaration method:
subjects=['Math','中文版'Chinese ']
3. Some API (1) Gets the number of list elements
Len (subjects)
(2) using an index to access the elements of each position in the list, remember that the index is from
0Started with
Subjects[0]
PS: If you want to take the last element, in addition to the index location, you can also be -1 indexed, directly get the last element, -2 is the penultimate, and so on.
(3) Append the element to the list to the end:
Subjects.append ('Music')
(4) inserting the element into the specified position
' Music ')
(5) Delete the element at the end of the list
Subjects.pop ()
(6) To delete the element at the specified position
Subjects.pop (i)
(7) to replace an element with another element, you can assign a value directly to the corresponding index position
' Music '
(8) The data type of the elements in the list can be different
(9) The IST element can also be another list
p = ['asp''php'= ['python' 'java'scheme']s[2][1] # equivalent to a 2-D array
Second, tuple1, definition
Another ordered list is called a tuple: a tuple. Tuple and list are very similar, but a tuple cannot be modified once initialized
2. Statement
subjects= ('Math','中文版'Chinese ')
3, the advantages of the tuple
The tuple is immutable, so the code is more secure
4. Tuple traps
(1) When you define a tuple, the elements of the tuple must be determined at the time of definition.
(2) When you define a tuple of only one element, you must write to the following format, otherwise the operation is performed () by default.
Tuplee = (1,)
(3) A tuple refers to the data that is pointing to the same, that is, when a tuple contains a list
Python Learning Notes (v)--list and tuple