Python---tuple ganso

Source: Internet
Author: User

Ython tuples are similar to lists, except that elements of tuples 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.

Tup1 = (' Physics ', ' Chemistry ', 1997, *) Tup2 = (1, 2, 3, 4, 5) Tup3 = "A", "B", "C", "D" #创建空元祖tup1 = () #元组中只包含一个元素时, need to be Add comma after element tup1 = (50,)
accessing tuples

Tuples can use subscript indexes to access values in tuples

Tup1 = (' Physics ', ' Chemistry ', 1997, $) tup2 = (1, 2, 3, 4, 5, 6, 7) print ("tup1[0]:", tup1[0]) print ("Tup2[1:5]:", tu P2[1:5]) output results tup1[0]:  Physicstup2[1:5]:  (2, 3, 4, 5)
modifying tuples

element values in tuples are not allowed to be modified, but they can be combined in groups of tuples

Tup1 = (34.56) tup2 = (' abc ', ' XYZ ') # The following modify tuple element operation is illegal # tup1[0] = 100# Create a new tuple Tup3 = Tup1 + tup2print (TUP3) #输出结果 (12, 34. , ' abc ', ' XYZ ')
Delete a tuple

element values in tuples are not allowed to be deleted, you can use the DEL statement to delete the entire tuple

Tup = (' Physics ', ' Chemistry ', 1997, #) print (tup) del tupprint ("After deleting Tup:") print (TUP) The output variable will have exception information after the instance tuple is deleted (' Physics ', ' Chemistry ', 1997, 2000) After deleting Tup:traceback (most recent call last):  File "test.py", line 9, in <module>    print tup; Nameerror:name ' tup ' is not defined
Tuple operators

As with strings, you can use the + sign and the * number to perform operations between tuples

#计算元素个数len ((1, 2, 3)) #输出结果 >>>3# Connection (1, 2, 3) + (4, 5, 6) #输出结果 >>> (1, 2, 3, 4, 5, 6) #复制 (' hi! ') * 4# output >& Gt;> (' hi! ', ' hi! ', ' hi! ', ' hi! ') #元素是否存在3 in (1, 2, 3) #输出结果 >>>true# iteration for x in (1, 2, 3):    print (x) #输出结果 >>>1 2 3
Tuple index, intercept

Tuples are also a sequence, so we can access elements in a tuple at a specified location, or we can intercept an element in an index

L = (' spam ', ' spam ', ' spam! ') #读取第三个元素L [2]>>>spam! #反向读取; Read the penultimate element l[-2]>>>spam# intercept element L[1:]>>>spam spam!

Arbitrarily unsigned objects, separated by commas, are tuples by default

Print (' abc ', -4.24E93, 18+6.6j, ' xyz ') x, y = 1, 2;print ("Value of X, y:", x, y) #输出结果abc -4.24e+93 (18+6.6j) Xyzvalue of X, Y:1 2
Tuple built-in functions
CMP (Tuple1, Tuple2)  #比较两个元组元素len (tuple)  #计算元组元素个数max (tuple)  #返回元组中元素最大值min (tuple)  # Returns the element minimum tuple (seq) in a tuple  #将列表转换为元组

  

Python---tuple ganso

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.