Python tuple (Tuple) operations detailed _python

Source: Internet
Author: User

First, create a tuple

Copy Code code as follows:
Tup1 = (' Physics ', ' Chemistry ', 1997, 2000);
Tup2 = (1, 2, 3, 4, 5);
Tup3 = "A", "B", "C", "D";

Create an empty tuple
Copy Code code as follows:
Tup1 = ();

When you include only one element in a tuple, you need to add a comma after the element to eliminate ambiguity
Copy Code code as follows:
Tup1 = (50,);

Tuples are similar to strings, the subscript index starts at 0 and can be intercepted, combined, and so on.
second, access to the META Group
Tuples can use the subscript index to access values in tuples, as follows:
Copy Code code 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]
#以上实例输出结果:
#tup1 [0]: Physics
#tup2 [1:5]: [2, 3, 4, 5]


Third, modify the meta Group
The element values in the tuple are not allowed to be modified, but we can combine the tuples into the following example:
Copy Code code as follows:
#!/usr/bin/python

Tup1 = (12, 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 ')


Four, delete tuples
The element values in the tuple are not allowed to be deleted, but we can use the DEL statement to delete the entire tuple, as follows:
Copy Code code as follows:
#!/usr/bin/python

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

Print tup;
Del Tup;
print "After deleting tup:"
Print tup;


#以上实例元组被删除后, the output variable has exception information, and the output looks like this:
# (' Physics ', ' Chemistry ', 1997, 2000)
#After deleting Tup:
#Traceback (most recent call last):
# File "test.py", line 9, in <module>
# print Tup;
#NameError: Name ' Tup ' is not Defined[/code]
Five, tuple operators
As with strings, the + and * numbers can be used between tuples. This means that they can be combined and copied, and a new tuple will be generated after the operation.

Six, meta-group index, interception
Because the tuple is also a sequence, we can access the element at the specified position in the tuple, or we can intercept a section of the index, as follows:
META Group:

Copy Code code as follows:
L = (' spam ', ' spam ', ' spam! ')


Seven, no close separator
Any unsigned object, separated by commas, defaults to tuples, as follows:

Copy Code code as follows:
#!/usr/bin/python

print ' abc ', -4.24E93, 18+6.6j, ' XYZ ';
X, y = 1, 2;
Print "Value of x, y:", x,y;


The above example allows the result:
Copy Code code as follows:
abc-4.24e+93 (18+6.6J) xyz
Value of X, Y:1 2

Eight, the tuple built-in functions
The Python tuple contains the following built-in functions
1, CMP (Tuple1, Tuple2): compare two tuple elements.
2, Len (tuple): Calculate 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 a tuple.
5, tuple (seq): Converts a list to a tuple.

Nine, another kind of interpretation

Tuple and list are very similar, but tuple can not be modified once initialized, for example, the name of the classmate is also listed:

Copy Code code as follows:
>>> classmates = (' Michael ', ' Bob ', ' Tracy ')

Now, classmates this tuple can not be changed, it also has no append (), insert () such a method. The other way to get the element is the same as the list, you can use classmates[0],classmates[-1 normally, but you can't assign a value to another element.
What's the point of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, use tuple instead of list as much as possible with tuple.
Tuple trap: When you define a tuple, when defined, the elements of tuple must be identified, such as:

Copy Code code as follows:
>>> T = (1, 2)
>>> T
(1, 2)

If you want to define an empty tuple, you can write ():
Copy Code code as follows:
>>> T = ()
>>> T
()

However, to define a tuple that has only 1 elements, if you define this:
Copy Code code as follows:
>>> t = (1)
>>> T
1

The definition is not tuple, it is 1 this number! This is because the parentheses () can represent both the tuple and the parentheses in the mathematical formula, which creates ambiguity, so Python stipulates that in this case, the calculation is done in parentheses, and the result is naturally 1.
Therefore, only 1 elements of the tuple definition must be added with a comma, to eliminate ambiguity:
Copy Code code as follows:
>>> T = (1,)
>>> T
(1,)

Python also adds a comma when displaying a tuple of only 1 elements, lest you misunderstand the parentheses in the meaning of the mathematical calculation.

Looking at a "variable" tuple:

Copy Code code as follows:
>>> t = (' A ', ' B ', [' A ', ' B '])
>>> t[2][0] = ' X '
>>> t[2][1] = ' Y '
>>> T
(' A ', ' B ', [' X ', ' Y '])

This tuple definition has 3 elements, namely ' a ', ' B ' and a list. It's not that tuple once defined, does it not change? How come it's changed then?

Don't worry, let's take a look at the definition. Tuple contains 3 elements:

When we modify the element ' A ' and ' B ' of the list to ' X ' and ' Y ', the tuple becomes:

on the surface, the Tuple element does change, but in fact it is not Tuple element, but the element of the list. Tuple first point to the list did not change to another list, so, tuple so-called "invariant" is that tuple each element, point to never change. Point to ' a ', can't change to point ' B ', point to a list, can not be changed to point to other objects, but the list itself is variable!
When you understand "point invariant", to create a content unchanged tuple how? It must be ensured that every element of the tuple itself cannot be changed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.