First, create a tuple
Tup1 = (' Physics ', ' Chemistry ', 1997, n); tup2 = (1, 2, 3, 4, 5); tup3 = "A", "B", "C", "D";
Create an empty tuple
Tup1 = ();
When you include only one element in a tuple, you need to add a comma after the element to disambiguate
Tup1 = (50,);
Tuples are similar to strings, and subscript indexes start at 0 and can be intercepted, combined, and so on.
Second, access to tuples
Tuples can use the subscript index to access the values in the tuple, as in the following example:
#!/usr/bin/pythontup1 = (' Physics ', ' Chemistry ', 1997, 3); tup2 = (1, 2,, 4, 5, 6, 7);p rint "tup1[0]:", Tup1[0]print "Tup2[1:5]:", Tup2[1:5] #以上实例输出结果: #tup1 [0]: physics#tup2[1:5]: [2, 3, 4, 5]
Third, modify the tuple
The element values in the tuple are not allowed to be modified, but we can combine the tuples with the following example:
#!/usr/bin/pythontup1 = (34.56); tup2 = (' abc ', ' xyz '); # The following modification of tuple element operations is illegal. # Tup1[0] = 100;# Create a new tuple Tup3 = Tup1 + tup2;print tup3; #以上实例输出结果: # (34.56, ' abc ', ' XYZ ')
Iv. Deleting tuples
element values in tuples are not allowed to be deleted, but we can use the DEL statement to delete an entire tuple, as in the following example:
#!/usr/bin/pythontup = (' Physics ', ' Chemistry ', 1997, $);p rint Tup;del tup;print "after deleting tup:" Print tup;
#以上实例元组被删除后, the output variable will have exception information, as shown in the output:
# (' Physics ', ' Chemistry ', 1997, 2000)
#After deleting Tup:
#Traceback (most recent):
# File "test.py", line 9, <module>
# print Tup;
#NameError: Name ' Tup ' is not Defined[/code]
Five, tuple operators
As with strings, you can use the + and * numbers to perform operations between tuples. This means that they can be combined and copied, and a new tuple is generated after the operation.
Six, tuple index, intercept
Because tuples are also a sequence, we can access the elements at the specified location in the tuple, or we can intercept an element in the index as follows:
Meta-group:
L = (' spam ', ' spam ', ' spam! ')
Seven, no closing separators
Arbitrarily unsigned objects, separated by commas, are tuples by default, as in the following example:
#!/usr/bin/pythonprint ' abc ', -4.24E93, 18+6.6j, ' xyz '; x, y = 1, 2;print "Value of X, y:", x, y;
The above example allows the result:
abc-4.24e+93 (18+6.6j) Xyzvalue of X, Y:1 2
Eight, tuple built-in functions
The Python tuple contains the following built-in functions
1. CMP (Tuple1, Tuple2): Compares two tuple elements.
2. Len (tuple): Calculates the number of tuple elements.
3, Max (tuple): Returns the maximum value of an element in a tuple.
4, min (tuple): Returns the element minimum value in the tuple.
5, tuple (seq): Converts a list to a tuple.
Nine, another interpretation
Tuple and list are very similar, but once the tuple is initialized, it cannot be modified, for example, the name of the classmate is also listed:
>>> classmates = (' Michael ', ' Bob ', ' Tracy ')
Now, classmates this tuple cannot be changed, and it does not have a append (), insert () method. Other methods of acquiring elements are the same as lists, and you can use classmates[0],classmates[-1] normally, but you cannot assign them to another element.
What is the meaning of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, you can use a tuple instead of a list as much as possible.
The trap of a tuple: when you define a tuple, the elements of a tuple must be determined when defined, such as:
>>> T = (1, 2) >>> T (1, 2)
If you want to define an empty tuple, you can write ():
>>> t = () >>> t ()
However, to define a tuple with only 1 elements, if you define this:
>>> t = (1) >>> t1
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 eliminate ambiguity:
>>> T = (1,) >>> T (1,)
Python will also add a comma when displaying a tuple of only 1 elements, lest you misunderstand the parentheses in the mathematical sense.
In view of a "mutable" tuple:
>>> t = (' A ', ' B ', [' A ', ' B ']) >>> t[2][0] = ' X ' >>> t[2][1] = ' Y ' >>> t (' A ', ' B ', [' X ', ' Y '])
This tuple is defined by 3 elements, namely ' a ', ' B ', and a list. Doesn't it mean that once a tuple is defined, it's immutable? Why did you change it later?
Don't worry, let's take a look at the definition when the tuple contains 3 elements:
When we modify the list's elements ' A ' and ' B ' to ' X ' and ' Y ', the tuple becomes:
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. 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!
After understanding "point to invariant", how do you create a tuple that does not change the content? It is important to ensure that each element of a tuple cannot be changed.