Python tuples (Tuple)

Source: Internet
Author: User

1. Create tuples
Copy codeThe Code is as follows: tup1 = ('physics ', 'chemistry', 1997,200 0 );
Tup2 = (1, 2, 3, 4, 5 );
Tup3 = "a", "B", "c", "d ";
Create null tuples
Copy codeThe Code is as follows: tup1 = ();
When only one element is contained in a tuple, you must add a comma after the element to eliminate ambiguity.
Copy codeThe Code is as follows: tup1 = (50 ,);
Tuples are similar to strings. subscript indexes start from 0 and can be truncated or combined.
Ii. Access tuples
You can use subscript indexes to access values in tuples, as shown in the following example:
Copy codeThe Code is as follows :#! /Usr/bin/python

Tup1 = ('physics ', 'chemistry', 1997,200 0 );
Tup2 = (1, 2, 3, 4, 5, 6, 7 );

Print "tup1 [0]:", tup1 [0]
Print "tup2 [1: 5]:", tup2 [1: 5]
# Output result of the above instance:
# Tup1 [0]: physics
# Tup2 [1: 5]: [2, 3, 4, 5]
3. Modify tuples
Element values in tuples cannot be modified, but we can concatenate and combine them, as shown in the following example:
Copy codeThe Code is as follows :#! /Usr/bin/python

Tup1 = (12, 34.56 );
Tup2 = ('abc', 'xyz ');

# The following operation to modify the element of a tuples is invalid.
# Tup1 [0] = 100;

# Create a new tuple
Tup3 = tup1 + tup2;
Print tup3;
# Output result of the above instance:
# (12, 34.56, 'abc', 'xyz ')
Iv. Delete tuples
The element values in the tuples cannot be deleted, but we can use the del statement to delete the entire tuples, as shown in the following example:
Copy codeThe Code is as follows :#! /Usr/bin/python

Tup = ('physics ', 'chemistry', 1997,200 0 );

Print tup;
Del tup;
Print "After deleting tup :"
Print tup;
# After the instance tuples are deleted, the output variable has an exception. The output is as follows:
# ('Physics ', 'chemistry', 1997,200 0)
# After deleting tup:
# Traceback (most recent call last ):
# File "test. py", line 9, in <module>
# Print tup;
# NameError: name 'tup' is not defined [/code]
5. tuples
Like a string, you can use the plus sign (+) and minus sign (*) to perform operations between tuples. This means they can combine and copy, and a new tuples will be generated after the operation.

Vi. tuples index and truncation
Because tuples are also a sequence, we can access the elements at the specified position in the tuples, or intercept some elements in the index, as shown below:
Tuples:
Copy codeThe Code is as follows: L = ('spam', 'spam', 'spam! ')


7. No Delimiter is closed
Any unsigned objects are separated by commas (,). The default value is tuples, as shown in the following example:
Copy codeThe Code is as follows :#! /Usr/bin/python

Print 'abc',-4.24e93, 18 + 6.4.7, 'xyz ';
X, y = 1, 2;
Print "Value of x, y:", x, y;
Results allowed for the above instances:
Copy codeThe Code is as follows: abc-4.24e + 93 (18 + 6.4.7) xyz
Value of x, y: 1 2
8. built-in functions of tuples
Python tuples contain the following built-in functions
1. cmp (tuple1, tuple2): Compares two tuples.
2. len (tuple): calculates the number of tuples.
3. max (tuple): returns the maximum value of elements in the tuples.
4. min (tuple): returns the minimum element value in the tuples.
5. tuple (seq): Convert the list into tuples.

9. Another explanation

Tuple and list are very similar, but tuple cannot be modified once initialized. For example, they are also used to list the names of students:
Copy codeThe Code is as follows: >>> classmates = ('Michael ', 'bob', 'tracy ')

Now, the tuple of classmates cannot be changed, and it does not have methods such as append () and insert. Other methods for retrieving elements are the same as those for listing. You can normally use classmates [0], classmates [-1], but cannot assign values to other elements.
What is the significance of an immutable tuple? Because tuple is immutable, the code is safer. If possible, use tuple instead of list.
Tuple trap: When you define a tuple, The tuple elements must be determined. For example:
Copy codeThe Code is as follows: >>> t = (1, 2)
>>> T
(1, 2)
To define an empty tuple, you can write it ():
Copy codeThe Code is as follows: >>> t = ()
>>> T
()
However, to define a tuple with only one element, if you define it as follows:
Copy codeThe Code is as follows:> t = (1)
>>> T
1
Tuple is not defined, it is the number of 1! This is because parentheses () can represent both tuple and parentheses in the mathematical formula, which leads to ambiguity. Therefore, Python stipulates that, in this case, parentheses are used for calculation, the calculation result is 1.
Therefore, when defining a tuple with only one element, you must add a comma to eliminate ambiguity:
Copy codeThe Code is as follows: >>> t = (1 ,)
>>> T
(1 ,)
When Python displays a tuple with only one element, it also adds a comma to avoid misunderstanding as a square brackets in the mathematical sense.

Let's look at a "variable" tuple:
Copy codeThe Code is as follows: >>> t = ('A', 'B', ['A', 'B'])
>>> T [2] [0] = 'X'
>>> T [2] [1] = 'y'
>>> T
('A', 'B', ['x', 'y'])
This tuple defines three elements: 'A', 'B', and a list. Doesn't tuple be immutable once defined? Why have they changed?

Don't worry. Let's take a look at the three elements that tuple contains when defining:

When we change the 'A' and 'B' elements of the list to 'X' and 'y', tuple is changed:

On the surface, the tuple elements have indeed changed, but in fact they are not the tuple elements, but the list elements. The list to which tuple points at the beginning is not changed to another list. Therefore, tuple's so-called "unchanged" means that every element of tuple will always point to it. That is, if you point to 'A', you cannot change it to 'B' or to a list. You cannot change it to another object, but the list itself is variable!
After "pointing to unchanged", how does one create a tuple with unchanged content? Therefore, each element of the tuple cannot be changed.

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.