Python tuples (Tuple) Detailed Operation

 

Python tuple is similar to the list, except that the elements of the tuple can not be changed, tuples use parentheses, square brackets lists, tuples create very simple, only need to add elements in parentheses, and that is to use a comma can

First, create a tuple

code show as below:
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";

Empty tuples

code show as below:
tup1 = ();

Tuple contains only one element, the comma needs to be added after the element to disambiguate

code show as below:
tup1 = (50,);

Tuple string Similarly, the subscript index starts from 0, may be taken, and other combinations.
Second, access tuple
tuple subscripts may be used to access the index value tuples, the following examples:

Code is as follows: 
! # / Usr / bin / Python 
 

TUP1 = ( 'Physics', 'Chemistry', 1997, 2000); 
tup2 = (. 1, 2,. 3,. 4,. 5,. 6,. 7); 

Print "TUP1 [0 ]: ", TUP1 [0] 
Print" tup2 [. 1:. 5]: ", tup2 [. 1:. 5] 
# above example output: 
# TUP1 [0]: Physics 
# tup2 [. 1:. 5]: [2,. 3 , 4, 5]

Third, the modified tuple
element values tuple can not be modified, but we may be connected to a combination of tuples, the following examples:

code show as below:
! # / usr / bin / Python 
 

TUP1 = (12 is, 34.56); 
tup2 = ( 'ABC', 'XYZ'); 

# following modifications tuple element operation is illegal. 
TUP1 # [0] = 100; 

# Create a new tuple 
tup3 + = TUP1 tup2; 
Print tup3; 
# output above example: 
# (12 is, 34.56, 'ABC', 'XYZ')

Fourth, delete tuple
element values in the tuple can not be deleted, but we can use the del statement to delete an entire tuple, the following examples:

code show as below:
! # / usr / bin / Python 
 

TUP = ( 'Physics', 'Chemistry', 1997, 2000); 

Print TUP; 
del TUP; 
Print "the After Deleting TUP:" 
Print TUP; 


# above examples tuple is deleted, the output abnormality information will be variable, output is as follows: 
# ( 'Physics', 'Chemistry', 1997, 2000) 
#after Deleting TUP: 
#Traceback (MOST Recent Last Call): 
# File "the test.py", Line. 9, in <Module1> 
# Print TUP; 
#NameError: name 'TUP' IS Not defined [/ code]

V. operator tuple
string as + and * can be used for operation between the number of tuples. This means that they can be combined and after replication operation will generate a new tuple.

 

Six-tuple index, taken
as a sequence of tuples is, we can access the element at the location of a tuple, some elements may be taken in the index, as follows:
tuples:

code show as below:
L = ('spam', 'Spam', 'SPAM!')

Seven, no closing delimiter
any unsigned object, separated by a comma, the default tuples, the following examples:

code show as below:
#!/usr/bin/python
 

print 'abc', -4.24e93, 18+6.6j, 'xyz';
x, y = 1, 2;
print "Value of x , y : ", x,y;

Examples of the above results allow:

code show as below:
abc -4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2

Eight, built-in functions tuple
Python tuple contains the following built-in functions
1, cmp (tuple1, tuple2) : Compares two tuple element.
2, len (tuple): counting the number of tuples of elements.
3, max (tuple): Returns the maximum tuple element.
4, min (tuple): returns the minimum value tuple element.
5, tuple (seq): to convert the tuple list.

 

Nine, another interpretation

tuple and the list is very similar, but the tuple can not be changed once initialized, for example, also list the names of students:

code show as below:
>>> classmates = ('Michael', 'Bob', 'Tracy')

Now, classmates this tuple can not be changed, nor does it append (), this method insert (). Other methods of obtaining and list element are the same, you can use normal classmates [0], classmates [-1 ], but can not be assigned to another element.
Immutable tuple What is the significance? Because tuple immutable, so the code more secure. If possible, use a tuple instead of a list as much as possible tuple.
tuple trap: When you define a tuple, when defined, tuple elements must be determined, such as:

code show as below:
>>> t = (1, 2)
>>> t
(1, 2)

If you want to define an empty tuple, can be written as ():

code show as below:
>>> t = ()
>>> t
()

However, to define a tuple 1 elements only, so if you define:

code show as below:
>>> t = (1)
>>> t
1

Not defined tuple, is the number 1! This is because the brackets () indicate either tuple, and can represent mathematical formulas in parentheses, which creates ambiguity, therefore, Python provisions, in this case, by parentheses were calculated. The results are a natural.
Therefore, you must add a comma when only the definition of a tuple elements ,, to disambiguate:

code show as below:
>>> t = (1,)
>>> t
(1,)

Python tuple when displaying only one element, will add a comma ,, in case you misunderstood as brackets on the calculated mathematical sense.

 

A "variable" tuple in perspective:

code show as below:

>>> t = ('a', 'b', ['A', 'B'])
>>> t[2][0] = 'X'
>>> t[2][1] = 'Y'
>>> t
('a', 'b', ['X', 'Y'])

This tuple defines when there are three elements, namely, 'a', 'b' and a list. Not to say that once the tuple variable is not defined yet? How later changed?

 

But wait, let's look at the definition time tuple contains three elements:
tuple-0
When we list the elements of 'A' and the 'B' modify 'X' and 'Y', tuple becomes:
tuple-1
On the surface, tuple the element has indeed changed, but the change is not in fact an element tuple, but the list of elements. tuple a start point of the list is not changed to another list, therefore, tuple-called "change" is that each element of the tuple, point to never change. That point 'a', can not be changed to point 'b', a point list, pointing to other objects can not be changed, but this list points to itself is variable!
After understanding the "point change", to create a content is constant tuple how to do? We must ensure that each element of the tuple itself can not be changed.

Guess you like

Origin www.cnblogs.com/still-smile/p/11586452.html