Python tuple ()

Source: Internet
Author: User

Tuple definition: Use parentheses to enclose elements separated by commas; empty parentheses denote empty tuples; when there is only one element, you must end with a comma.

Basic operation of the sequence:

Len ()--to find the length of the sequence
such as a string "a= ' Qweqerq '", its length can be thought

    • --Connection of two sequences
      For example, the string "a" plus a sequence, as shown below
      In [4]: a+‘abc‘Out[4]: ‘qweqerqabc‘

*--Repeating sequence elements
For example, repeat printing 20 "#" can be expressed as

In [5]: "#" * 20Out[5]: ‘####################‘In [7]: a * 5Out[7]: ‘qweqerqqweqerqqweqerqqweqerqqweqerq‘

in--determines whether the element is in sequence
For example, determine if a is in sequence a, if present, return "True", not present, return "False"

In [2]: a=‘qweqerq‘In [8]: "a" in aOut[8]: FalseIn [9]: "q" in aOut[9]: TrueIn [10]: "a" not in aOut[10]: True                         #a不在序列a里面,为真

Max ()--Returns the maximum value

In [11]: max(a)Out[11]: ‘w‘

Min ()--Returns the minimum value

In [12]: min(a)Out[12]: ‘e‘

CMP (x, y)--compare two sequences for equality

In [13]: a=12345In [14]: cmp(a,1234)Out[14]: 1                                #表示前者比后者多In [15]: cmp(a,12345)Out[15]: 0                                #表示两者相等In [16]: cmp(a,123456)Out[16]: -1                               #表示前者比后者少

Tuples and lists are very similar, tuples and strings are immutable, and you can store a range of values, usually when a user-defined function can safely take a set of values, that is, the value of the tuple being used does not change.

In [17]: t1=1In [18]: type(t1)Out[18]: int                     #表示的是字符串In [19]: t2=1,In [20]: type(t2)Out[20]: tuple                #表示的是元组

Sequences and tuples are similar in operation

In [21]: t=(a,"b","c")                #a未加引号,表示变量In [22]: tOut[22]: (12345, ‘b‘, ‘c‘)           #显示a的实际代表的值In [24]: b="qwe"In [25]: t=("a",b,"c")In [26]: tOut[26]: (‘a‘, ‘qwe‘, ‘c‘)

In Python, everything can be called an object.

Python tuple ()

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.