(2)python变量类型

                                                                                                 声明: 仅为学习笔记,无任何意义 。

列表:
>>>> list1=['字符',"字符串",110,1.0125]
>>> list1;type(list1);len(type1)
['字符', '字符串', 110, 1.0125]
<class 'list'>
4
>>>> list1[0];list1[1];list1[2];list1[3]
'字符'
'字符串'
110
1.0125
>>>> list1[0]=999;list1[2]='字符'
>>> list1
[999, '字符串', '字符', 1.0125]
</span>
>>>> list1=list1*2
>>> len(list1)
8
>>>> list1=list1+[1,2,3,4,5]
>>> list1
['字符', '字符串', 110, 1.0125, 1, 2, 3, 4, 5]
>>> list1[:]
['字符', '字符串', 110, 1.0125, 1, 2, 3, 4, 5]
>>> list1[0:]
['字符', '字符串', 110, 1.0125, 1, 2, 3, 4, 5]
>>> list1[1:]
['字符串', 110, 1.0125, 1, 2, 3, 4, 5]
>>> list1[0:1]
['字符']
>>> list1[2:5]
[110, 1.0125, 1]
>>> list1[-1]
5
>>> list1[-3]
3
>>> list1[-1:3]
[]
>>> list1[-3:-5]
[]
</span>
元组:
>>>> tup1=()
>>> tup1
()
>>> tup1=(1,2,3,44,12)
>>> tup1;type(tup1);len(tup1)
(1, 2, 3, 44, 12)
5
注:无法自定义元素。
>>>> tup1[1]
2
>>> tup[1]="ooo"
Traceback (most recent call last):
File "<pyshell#471>", line 1, in <module>
tup[1]="ooo"
NameError: name 'tup' is not defined




字典:
<span style="font-family:Microsoft YaHei;">>>> dirr={}
>>> dirr[0]=list1
>>> dirr[2]=tup1
>>> dirr
{0: ['字符', '字符串', 110, 1.0125, 1, 2, 3, 4, 5], 2: (1, 2, 3, 44, 12)}</span>
>>>> len(dirr)
2
>>> dirr[1]=12
>>> dirr
{0: ['字符', '字符串', 110, 1.0125, 1, 2, 3, 4, 5], 1: 12, 2: (1, 2, 3, 44, 12)}
>>> dirr[0]="gai"
>>> dirr
{0: 'gai', 1: 12, 2: (1, 2, 3, 44, 12)}
>>> dirr["插入"]="任意"
>>> dirr[4]=""
>>> dirr[4]
>>>> dirr["A"]="BOB"
>>> dirr
{0: 'gai', 1: 12, 2: (1, 2, 3, 44, 12), 4: '', '插入': '任意', 'A': 'BOB'}
>>> dirr[10]=11000
</span>
>>>> dirr
{0: 'gai', 1: 12, 2: (1, 2, 3, 44, 12), 4: '', '插入': '任意', 10: 11000, 'A': 'BOB'}

你可能感兴趣的:((2)python变量类型)