python010 Python3 Tuple

Source: Internet
Author: User

Python3 Meta-Group
A Python tuple is similar to a list, except that the elements of a tuple cannot be modified.
Tuples use parentheses, and the list uses square brackets.
Tuple creation is simple, just add elements in parentheses and separate them with commas.
The following example:

Tup1 = (' Google ', ' Tencent ', 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

Tup1 = (50,);

Tuples are similar to strings, and subscript indexes start at 0 and can be intercepted, combined, and so on.

accessing tuples
Tuples can use the subscript index to access the values in the tuple, as in the following example:

#!/usr/bin/python3tup1 = (' Google ', ' Tencent ', 1997, $) tup2 = (1, 2, 3, 4, 5, 6, 7) print ("tup1[0]:", tup1[0]) print ( "Tup2[1:5]:", Tup2[1:5])

The result of the above example output:

TUP1[0]: Googletup2[1:5]: (2, 3, 4, 5)

modifying tuples
The element values in the tuple are not allowed to be modified, but we can combine the tuples with the following example:

#!/usr/bin/python3tup1 = (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)

The result of the above example output:

(34.56, ' abc ', ' XYZ ')

Delete a tuple
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/python3tup = (' Google ', ' Tencent ', 1997, +) print (tup) del tup;print ("Deleted tuple tup:") print (TUP)

When the above instance tuple is deleted, the output variable will have exception information, the output is as follows:

Deleted tuple Tup:traceback (most recent call last): File "test.py", line 8, in <module>print (tup) nameerror:name ' Tup ' I s not defined

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.

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 = (' Google ', ' Taobao ', ' Tencent ')

Run the following example:

>>> L = (' Google ', ' Taobao ', ' Tencent ') >>> l[2] ' Tencent ' >>> l[-2] ' Taobao ' >>> l[1:] (' Taobao ', ' Tencent ')

Tuple built-in functions

The Python tuple contains the following built-in functions

python010 Python3 Tuple

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.