Operations on tuples in Python

Source: Internet
Author: User

Basic operations for tuples:
1. Create a tuple:

tuple= (1,26); Tuple1= ("" ","sy");

Create an empty tuple:

Tuple= ();

When you include only one element in a tuple, you need to add a comma after the element to eliminate ambiguity;

Tuple= (50,)

2. Access to tuples:

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]#The result of the above example output:#Tup1[0]: Physics#Tup2[1:5]: [2, 3, 4, 5]

3. Modify Tuples:

Tup1 = (34.56 = ('abc''xyz'); # The following modifications to tuple element operations are illegal.  #  tup1[0] = +; # Create a new tuple Tup3 = tup1 + tup2; Print Tup3; # The result of the above example output: # (34.56, ' abc ', ' XYZ ')

4. Delete the tuple:

Tup = ('physics'chemistry', 1997,); Print Tup; del Tup; Print "  "print tup;

5. As with strings, you can use the + sign and the * number to operate between tuples. This means that they can be combined and copied, and a new tuple is generated after the operation.

6.

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

Operations on tuples in Python

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.