Python Basic Learning 4----tuples

Source: Internet
Author: User

Tuples are also called read-only lists and cannot be modified by their contents

1. Create a tuple

tuple1= (1,3,2,4,5) tuple2= () #空元组tuple3 = (' A ',) #元组中只有一个元素时要在元素后面加上逗号, otherwise it will be treated as a different data type tuple4= (1, ' a ', [], (3,4)) # An element in a tuple can be any data type

2. You can convert a list to a tuple

List1 = [1,2,3,4]tup = tuple (list1) print (tup) #输出 (1,2,3,4)

3. Enquiry

Tuple queries and slices are basically the same as lists

tuple1= (1,2,3,4,5) print (tuple1[1]) #输出2 # Other query slices are the same as the list, followed by the tuple name in parentheses

Number of 4.count statistics elements

tuple1= (1,3,3,4,5) print (Tuple1.count (3)) #输出为2

5.index returns the index position of the element

Tup = (' A ', ' B ', ' C ', ' d ') print (Tup.index (' C ')) # returns the index position of the element ' C ', Output 2

6.len counts the number of elements in a tuple

Tup = (' A ', ' B ', ' C ', ' d ') print (len (tup)) #计算元组长度, Output is 4

  

Python Basic Learning 4----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.