python--tuple (tuple) basic operations

Source: Internet
Author: User

Tuples are called read-only lists, data can be queried, but cannot be modified, similar to a list of slice operations, tuples are written in parentheses () before the elements are separated by commas

For some data that you do not want to be modified, you can use tuples to save

# Create a tuple

1) Create an empty tuple

Create an empty tuple Tup = ()print  (tup)print#  Use the type function to view the type #  Output result ()<class'tuple'>

2) Create a tuple ( when there is only one element, add a comma after the element )

 #   Create tuples (when there is only one element, add a comma after the element)  Tup = (1,) Span style= "COLOR: #008000" >#   print   (tup)  print  (Type (tup)) #   Use the type function to view types  #   output  (1 <class   " tuple  ; 
#  Create a tuple (when there is only one element, add a comma after the element)Tup = (1)  #  do not add commas, will be treated as other data types print (TUP)
Print # use the type function to view types # Output 1<class'int'>

3) Create a tuple (multiple elements)

Tup = (1,2,["a","b","C"],"a")Print(TUP)#Output Results(1, 2, ['a','b','C'],'a')

4) # Convert list to tuple

List_name = ["Python Book","Mac","Bile","Kindle"]tup= Tuple (List_name)#converting a list to a tuplePrint(Type (list_name))#View the List_name type and print out the resultsPrint(Type (TUP))#View the Tup type and print out the resultsPrint(TUP)#Output Results<class 'List'><class 'tuple'>('Python Book','Mac','Bile','Kindle')

# query

Tup = (1, 2, ['a','b','C'],'D','e', ('gu','Tang'))Print("Tup[0] =", Tup[0])#print an element with an index of 0#Output ResultsTup[0] = 1Print("tup[1:] =", Tup[1:])#from index 1 to last element#Output ResultsTup[1:] = (2, ['a','b','C'],'D','e', ('gu','Tang'))Print("Tup[:-1] =", Tup[:-1])#to the second element but not the second one .#Output ResultsTUP[:-1] = (1, 2, ['a','b','C'],'D','e')Print("Tup[1::1] =", Tup[1::1])#equivalent to tup[1:] From left to right to fetch, step is 1#Output ResultsTUP[1::1] = (2, ['a','b','C'],'D','e', ('gu','Tang'))Print("Tup[1::2] =", Tup[1::2])#Take step 2 from left to right#Output ResultsTup[1::2] = (2,'D', ('gu','Tang'))Print("Tup[::-1]", Tup[::-1])#Reverse Output step is 1#Output ResultsTup[::-1] (('gu','Tang'),'e','D', ['a','b','C'], 2, 1)Print("Tup[::-2]", Tup[::-2])#Reverse Output step is 2 (one fetch))#Output ResultsTup[::-2] (('gu','Tang'),'D', 2)

# del Delete (element object does not support delete, but can delete entire tuple variable)

#del Delete element in tupleUp = ('Tang','Guo','Li','Xiu')delTUP[0]#element object does not support deletion#print (TUP)#Output ResultsTypeError:'tuple'Object doesn't support item deletion
Tup = ('Tang','Guo','Li','Xiu')delTup#DeletePrint(TUP)#del tup, memory deleted, will prompt not defined in print#Output ResultsNameerror:name'Tup'  is  notDefined

# Count Statistics element number

# count statistics elements tup = ('tang'Guo'li  ','guo'). Count ('guo')  Print  (tup)# output 2

# Index returns the indexed position of the element

# Index returns the indexed position of the element Tup = ('tang'Guo'li  ','Xiu')print (Tup.index (' Li ' # returns the index position of the element ' II ' # Output Results 2

# Len calculates the number of elements in a tuple

# Len   computes the number of elements in the tuple tup = ('tang'Guo ' Li ','Xiu')print (len (tup))    # calculate the element leader degree # Output Results 4

Precautions:

1, when an element in a tuple, be sure to add a comma after the element

2. Elements in tuples are not allowed to be deleted, but you can use the DEL statement to delete an entire tuple

3, the tuple does not list in the increase, delete, change the operation, only check the operation

python--tuples (tuple) basic operations

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.