Python3 tuples Tuple (12), python3 groups of tuple

Source: Internet
Author: User

Python3 tuples Tuple (12), python3 groups of tuple

Tuples: tuple. Tuple and list are very similar, but tuple cannot be modified once initialized.

The tuples use parentheses, And the list uses square brackets..

Creating tuples is simple. You only need to add elements in brackets and separate them with commas.

Example:

1 tup1 = ('Google', 'Runoob', 1997, 2000);2 tup2 = (1, 2, 3, 4, 5 );3 tup3 = "a", "b", "c", "d";

 

Create null tuples:

1 tup1 = ();

 

Note: When a tuple contains only one element, you must add a comma after the element. Otherwise, brackets are used as operators.

1 >>> tup1 = (50) 2 >>> type (tup1) # Without commas, type: integer 3 <class 'int'> 4 5 >>> tup1 = (50,) 6 >>> type (tup1) # Add a comma, type: tuples 7 <class 'tuple'>

 

Tuples are similar to strings. subscript indexes start from 0 and can be truncated or combined.

Let's look at a "variable" tuple:

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

Doesn't tuple be immutable once defined? Why have they 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. Point'a'And cannot be changed'b', Pointing to a list, you cannot point to other objects, but the list itself is variable!

Understood"Pointing to unchanged"How to Create a tuple with the same content? Therefore, each element of the tuple cannot be changed.

Access tuples

You can use subscript indexes to access values in tuples, as shown in the following example:

1 #!/usr/bin/python32 3 tup1 = ('Google', 'Runoob', 1997, 2000)4 tup2 = (1, 2, 3, 4, 5, 6, 7 )5 6 print ("tup1[0]: ", tup1[0])7 print ("tup2[1:5]: ", tup2[1:5])

Output result of the above instance:

1 tup1[0]:  Google2 tup2[1:5]:  (2, 3, 4, 5)
Modify tuples

Element values in tuples cannot be modified, but we can concatenate and combine them, as shown in the following example:

1 #! /Usr/bin/python3 2 3 tup1 = (12, 34.56); 4 tup2 = ('abc', 'xyz') 5 6 # the operation to modify the tuples below is invalid. 7 # tup1 [0] = 100 8 9 # create a new tuple 10 tup3 = tup1 + tup2; 11 print (tup3)

Output result of the above instance:

1 (12, 34.56, 'abc', 'xyz')
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:

1 #! /Usr/bin/python32 3 tup = ('Google ', 'runoob', 1997,200 0) 4 5 print (tup) 6 del tup; 7 print ("deleted tup:") 8 print (tup)

After the instance tuples are deleted, the output variable has an exception. The output is as follows:

1. tup: 2 Traceback (most recent call last): 3 File "test. py ", line 8, in <module> 4 print (tup) 5 NameError: name 'tup' is not defined
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.

Tuples index, 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:

1 L = ('Google', 'Taobao', 'Runoob')

The running instance is as follows:

1 >>> L = ('Google', 'Taobao', 'Runoob')2 >>> L[2]3 'Runoob'4 >>> L[-2]5 'Taobao'6 >>> L[1:]7 ('Taobao', 'Runoob')
Tuples built-in functions

Python tuples contain the following built-in functions

 

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.