Python tuple Type

Source: Internet
Author: User

Introduction of a tuple

(1) Tuples are defined in brackets (), such as tuple = (1, 2, 3, 4)
(2) Element index values in tuples starting at 0, tuples support indexing and slicing operations, and tuple elements support multiple types
(3) numbers, strings, and tuples are immutable types, which means that once an object is defined, its value can no longer be updated unless a new object is recreated

Two, the basic operation of the tuple

(1) Creating tuples: Tuple = (1, 2, 3, 4)
(2) Access tuples: You can use slice operators and index values to access tuple elements, such as tuple[2], and return a result of 3
(3) Updating tuples: Because tuples are immutable types, elements in tuples cannot be updated, and if they are to be updated, only one tuple can be recreated
(4) Delete a tuple: Del tuple means delete entire tuple

Three, the tuple's operator

(1) Standard type operators: =, = =, >, <, >=, <=,! =
(2) Sequence type operator: [], [:], [::] Detailed usage
(3) member relation operator: In, not in, determines whether the element is in a tuple, as defined by tuple = (1, 2, 3), executes a 3 in tuple returns a result of true
(4) connection operator: +, used to merge multiple tuple objects together, such as definition T1 = (1, 2, 3), t2 = (4, 5, 6), then T1 + T2 return result is (1, 2, 3, 4, 5, 6)
(5) Repeat operator: *, for repeating tuple elements, such as defining tuple = (1, 2, 3, 4), then tuple * 3 Returns the result as (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)

The built-in functions of the four tuples

(1) Standard type function: CMP ()
(2) Sequence type function: Len (), Max (), Min (), str (), CMP (), list () 、......
(3) Tuple type built-in function: Because tuples are immutable types, there is no dedicated built-in function

Five, the particularity of the tuple

(1) immutability: means that once a tuple is defined, its value cannot be updated again unless a new tuple is recreated
(2) "variability": in fact, the tuple is not so "immutable", such as the connection operator and the repetition operator are equivalent to append tuple elements, which in a sense is "variable"
(3) Default collection type: When a comma-delimited set of objects does not have a type defined, then they default to the tuple type, such as 1, 2, ' A ', ' B ' returns the result as (1, 2, ' A ', ' B ')
(4) Single element tuple: that is, a tuple of only one element, note that you create a single element tuple with commas, such as tuple (1,), if you define a tuple (1) then this type is int instead of tuple
(5) The Dictionary keyword: the value of the tuple is immutable, which means that the value they get through the hash algorithm is always a value, which is a prerequisite for the dictionary key value, so the value of the tuple can be used as a key in the dictionary

Python tuple Type

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.