Meta-group Tuple
Tuples and lists are very similar, but they cannot be modified once the list is initialized.
What is the meaning of immutable tuples? Code is more secure because tuples are immutable. If possible, you can use a tuple instead of a list as much as possible.
First, Defining tuples
>>> tup1= (' A ', ' B ', ' C ', ' 1 ') >>> tup2= (' A ', ' B ', ' C ', []) >>> tup3= (,)
The elements of a tuple are enclosed in parentheses, and the things to note are as follows:
1.tup2 are "mutable", for example:
>>> tup2[3][2]=4>>> tup2 (' A ', ' B ', ' C ', [1, 2, 4])
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 atuple is directed to never change. That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable!
2. A tuple of only 1 elements must be defined with a comma, and if no commas are treated as variables.
Second, Basic Operations
The count () and index () methods and slices are used the same way as lists.
Refer to: http://fengjicheng.blog.51cto.com/11891287/1927616
This article is from the "Network Technology" blog, please be sure to keep this source http://fengjicheng.blog.51cto.com/11891287/1927624
Python notes--tuples