Middle Valley Education Python data type sequence tuple

Source: Internet
Author: User

06Python data type sequence sequence
列表、元组和字符串都是序列。序列的两个主要特点是索引操作符合切片操作符。索引操作符让我们从序列中抓取一个特定的项目切片操作符让我们能够获取序列的一个切片,即一部分序列。
    • Index can be negative
    • Slices and indexes are very similar. The sequence name is followed by a square bracket with a pair of optional digits in square brackets, separated by a colon. The number is optional, and the colon is required.
Basic operation of the sequence
    1. Len () to find the length of the sequence
    2. + Connect two sequences
    3. * Repeating sequence elements
    4. In determines whether the element is in sequence
    5. Max () returns the maximum value
    6. MIN () returns the minimum value
    7. CMP (TUPLE1,TUPLE2) compares the 2 sequences for the same.
>>>  str1 =  "123"  >>>  str1*5   ' 123123123123123 '  >>>   "#"  *40   ' ################# ####################### '  >>>   ' 2 '  in  str1true  >>>  " in  str1true  > >>  min (str1)  ' 1 '  >>>  Max ( STR1)  ' 3 '   
>>> "1">>> "2">>> ‘12‘>>> ‘a‘>>> cmp(str1,str2)-1>>> cmp(str1,str3)-1>>> cmp(str2,str3)1>>> cmp(str1,str4)-1
Tuple ()
元组和列表十分相似,只不过元组和字符串一样是不可变的无法修改的。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或者用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。
Attention
    • Define an empty tuple, just an empty parenthesis
    • Defines a tuple with only one element, which needs to be appended with a ","
    • The value of the tuple cannot be changed
>>> info = (' Chen ', -) >>> info (' Chen ', -) >>> info[0] ' chen ' >>> t1 = () >>> t2 = (2) >>> type(T2) < type ' int ' >>>> type(t1) < type ' tuple ' >>>> T3 = (2,) >>> type(T3) < type ' tuple ' >
这里产生了一种新的数据定义方式
>>> info(‘chen‘25)>>> name,age = info>>> name‘chen‘>>> age25>>> 1,2,3>>> print a,b,c123

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Middle Valley Education Python data type sequence tuple

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.