Start learning Python seriously today.
1.list type
List is a Python data type, which is an ordered list of elements that can be added and removed at any time.
1.1 Features of the list type
- The type of members within a list type can be the same or different;
- Definition of list type: List1=[member1,member2,member3,...] ;
- You can use the Len () function to get the number of members of a list type, for example: Len (List1)
- You can use an index to access each member of the list type, with index subscripts starting with 0. Subscript out of bounds will make an error. Example: List1[2]
- If you want to access the last primitive of the list type, you can use 1 as the index in addition to the index location. Similarly, the second-to-last member can be indexed with 2, and so on. For example, in this example, List1[-1] is equivalent to list1[2]
- List type can be nested within list
1.2 Actions available for list type (method)
- Append member to list end: List1.append (MEMBER4)
- Append member to list specified location: List1.insert (N,MEMBER4)
- Delete End element: List1.pop ()
- Delete the element at the specified location: List1.pop (N)
2.tuple type
A tuple is a data type in Python, which is also an ordered list, unlike the list type, when a member of the tuple type is determined, it can no longer be modified, the definition of a tuple type: tuple1= (' AB ', ' CD ', ' EF '), Defines an empty tuple type, which can be written as: tuple= ()
Several examples of using a tuple type:
2.1 If you want to define a tuple type that contains only one member, the definition of the following form will be ambiguous and will not produce the expected result:
>>> tuple1= (1) >>> Tuple11
The definition is not a tuple, is 1 this number! This is because parentheses () can represent both tuples and parentheses in mathematical formulas, which creates ambiguity, so Python rules that, in this case, the parentheses are calculated and the result is naturally 1. Therefore, only 1 elements of a tuple definition must be added with a comma ', ' to disambiguate:
>>> tuple1= (1,) >>> Tuple1 (1,)
2.2 "mutable" tuple member, as previously stated in the definition of a tuple type, once a member of a tuple is determined, it cannot be modified, but in the following example:
>>> t= (1,2,[3,4]) >>> t (1, 2, [3, 4]) >>> t[2][0]=5>>> t[2][1]=6>>> t (1, 2, [ 5, 6])
On the surface, the elements of a tuple do change, but in fact it is not a tuple element, but a list element. The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of a tuple is directed to never change.
Data types for Python: list and tuple