Python data type-tuples

Source: Internet
Author: User

A tuple is another data type, similar to a list.
The tuple is identified with a "()". The inner elements are separated by commas. However, tuples cannot be assigned two times, which is equivalent to a read-only list. Cannot be modified once initialized

Role:
1, for some data we do not want to be modified, you can use the tuple
2. Tuples can be used as keys in mappings (and members of collections)-but not in lists, tuples as return values for many built-in functions and methods

tuple = ('runoob'John', 70.2)  #  format: tuple name = ( Element 1, Element 2, Element 3)#  tuple = (  a) # an element that needs to add a comma after the element, representing the element, or a variable 'John  ')

1. Check
Print(tuple)#Output Full tuplePrint(Tuple[0])#the first element of an output tuplePrint(Tuple[1:3])#outputs the second to third elementPrint(tuple[2:])#outputs all elements from the third start to the end of the listPrint(Tinytuple * 2)#output tuple two timesPrint(Tuple + tinytuple)#Print a group of tuples#Output Result:" "(' Runoob ', 786, 2.23, ' John ', 70.2) Runoob (786, 2.23) (2.23, ' John ', 70.2) (123, ' John ', 123, ' John ') (' Runoob ', 786, 2.23, ' John ', 70.2, 123, ' John ')" "
2. By deleting
del tuple  #  delete entire tuple
3. Other
#The following are tuples that are not valid because tuples are not allowed to be updated. And the list is allowed to be updated:#!/usr/bin/python#-*-coding:utf-8-*-Tu = ('Runoob', 786, 2.23,'John', 70.2) Li= ['Runoob', 786, 2.23,'John', 70.2]#tu[2] = 1000 # tuples are illegal to apply#li[2] = 1000 # is a legitimate application in the list#Method:Print(Tu.index ('Runoob'))#Print the index value of an elementPrint(Tu.count ('Runoob'))#number of printed elements

Python data type-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.