Python tuple tuples

Source: Internet
Author: User

# One, create tuple # Tup1 = (' Physics ', ' Chemistry ', 1997, #) # tup2 = (1, 2, 3, 4, 5) # tup3 = "A", "B", "C", "D" # tuples containing only one element, need to be  Add a comma after the element to disambiguate # Tup1 = (50,) # Two, access tuple tup1 = (' Physics ', ' Chemistry ', 1997, 4) tup2 = (1, 2, 3, 5, 6, 7,) print ("tup1[0]: ", Tup1[0]) print (" Tup2[1:5]: ", Tup2[1:5]) # Tup1[0]: physics# Tup2[1:5]: (2, 3, 4, 5) # Third, modify the element values in the tuple # tuple are not allowed to be modified, but we can go to the tuple Row connection combinations, as follows: Tup1 = (34.56) tup2 = (' abc ', ' XYZ ') # The following modification of tuple element operations is illegal. # Tup1[0] = 100# Create a new tuple Tup3 = Tup1 + tup2print (TUP3) #以上实例输出结果: # (+, 34.56, ' abc ', ' XYZ ') # Four, delete the element value in the tuple # tuple is not allowed to be deleted, but we can make Use the DEL statement to delete the entire tuple # Five, tuple operators # 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. # Six, tuple index, intercept # because tuples are also a sequence, we can access elements in a tuple at a specified location, or you can intercept an element in an index, as follows: # Seven, no closing delimiter # Any unsigned object, separated by commas, default to tuple # Eight, tuple built-in function # 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. # tuple Traps: When you define a tuple, the elements of the tuple must be determined when you define it # if you want to define an empty tuple, you can write t= () # to define a tuple with only 1 elements, t = (1,) tupleSemantic must be added with a comma, to disambiguate # "variable" tuple:t = (' A ', ' B ', [' A ', ' B ']) t[2][0] = ' X ' t[2][1] = ' y ' # T becomes (' A ', ' B ', [' X ', ' y ']) This tuple is defined by 3 elements, which are ' a ', ' B ', and a list, respectively. Doesn't it mean that once a tuple is defined, it's immutable? Why did you change it later?

  

Python tuple tuples

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.