[Python Tutorial] Python Tuples

Source: Internet
Author: User
The Python Tuples are similar to the list, except that the elements of the tuples cannot be modified. 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.

Python Tuples

The Python Tuples are similar to the list, except that the elements of the tuples cannot be modified.

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:

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

Create null tuples

tup1 = ();

When only one element is contained in a tuple, you must add a comma after the element.

tup1 = (50,);

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

Access tuples

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

#!/usr/bin/pythontup1 = ('physics', 'chemistry', 1997, 2000);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]:  physicstup2[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:

#! /Usr/bin/pythontup1 = (12, 34.56); tup2 = ('ABC', 'XYZ'); # The operation to modify the elements of the tuples below is invalid. # Tup1 [0] = 100; # Create a new tup3 = tup1 + tup2; print tup3;

Output result of the above instance:

(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:

#!/usr/bin/pythontup = ('physics', 'chemistry', 1997, 2000);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, 2000)After deleting tup :Traceback (most recent call last):  File "test.py", line 9, in 
 
      print tup;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.

Python expressions

Result

Description

Len (1, 2, 3) 3 calculate the number of elements

(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) connection

['Hi! '] * 4 ('Hi! ', 'Hi! ', 'Hi! ', 'Hi! ') Copy

3 in (1, 2, 3) whether the True element exists

For x in (1, 2, 3): print x, 1 2 3 iteration

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:

L = ('spam', 'Spam', 'SPAM!')

Python expressions

Result

Description

L [2] 'Spam! 'Read the third element

L [-2] 'spam' reverse reading; reads the second to last element

L [1:] ['spam', 'Spam! '] Truncation element

No delimiter to close

Any unsigned objects are separated by commas (,). The default value is tuples, as shown in the following example:

#!/usr/bin/pythonprint 'abc', -4.24e93, 18+6.6j, 'xyz';x, y = 1, 2;print "Value of x , y : ", x,y;

Results allowed for the above instances:

abc -4.24e+93 (18+6.6j) xyzValue of x , y : 1 2

Tuples built-in functions

Python Tuples contain the following built-in functions

Serial Number

Method and description

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 value of the element in the tuples.

5 tuple (seq)
Converts a list to a tuples.

The above is the content of the python Tuples in the [Python Tutorial]. For more information, see The PHP Chinese website (www.php1.cn )!

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.