Python sequence (序列)

序列简介

sequence 是一组有序元素的组合

序列可以是多个元素,也可以一个元素都没有

序列有2种:tuple(定值表)、List(表)

D:\python\Python_Day>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=(1,'OLIVER',2.7,False)
>>> print(a,type(a))
(1, 'OLIVER', 2.7, False) <class 'tuple'>
>>> b=[1,'OLIVER',2.9]
>>> print(b,type(b))
[1, 'OLIVER', 2.9] <class 'list'>
>>> a[0]=2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> b[0]=2
>>> print(b,type(b))
[2, 'OLIVER', 2.9] <class 'list'>

通过以上代码可以发现:

1.tuple是通过“()”包含其元素,List是通过“[]”包含其元素。

2.List中的元素是可以进行修改的,但是tuple中的元素师不可以进行修改的。

3.type(对象名) 可以看出该对象的数据类型。

--------------------------------------------------------------------------------------------------------------------

元素的引用

序列元素下标是从0开始的

>>> print(a[1])
OLIVER

空序列

>>> c=[]
>>> print(c)
[]

一个序列作为另外一个序列的元素

>>> d=['QIN',[1,'YUE']]
>>> print([0][1])
>>> print(d[0][1])
I
上一篇:unigui web app之title buttons


下一篇:python学习15-序列化(转载)