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
- Len () to find the length of the sequence
- + Connect two sequences
- * Repeating sequence elements
- In determines whether the element is in sequence
- Max () returns the maximum value
- MIN () returns the minimum value
- 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