>>> 1+1
2
>>> print _
2
在交互式模式中语句可以直接执行
注意:变量名没有类型,对象才有
>>> type(name)
>>> name = 3.14
>>> type(name)
变量可以引用任意对象
python变量名只是内存引用的标示而已
列表:使用[]创建,如["This","is","a","pig"]
0 |
This |
1 | is |
2 | a |
3 | pig |
>>> l1 = ["This","is","a","pig"]
>>> l1[0]
'This'
>>> l1[0][0]
'T'
列表是可变对象,原处修改,id未变
>>> print l1
['This', 'is', 'a', 'pig']
>>> print l1[3]
pig
>>> l1[3] = "sheep" //修改值
>>> print l1
['This', 'is', 'a', 'sheep']
>>> id(l1)
58691848L
>>> l1[3] = "Tom"
>>> id(l1)
58691848L //id未变
>>> print l1
['This', 'is', 'a', 'Tom']
//3号元素的引用发生了改变,整个列表的引用没有改变
元组:使用()创建,如('one','two')-------不可变对象
>>> t1 = ("This","is")
>>> t1[0]
'This'
>>> t1[0][0]
'T'
字符串也属于序列类型
//切片
>>> name = 'jerry'
>>> name[0]
'j'
>>> name[0:1] //结束位是不显示的,0:1只打印j
'j'
>>> name[0:2] //0:2打印je
'je'
>>> name[:2] //从开头到2
'je'
>>> name[2:] //从2到最后
'rry'
>>> name[0:4:2] //切片可以指定“步进”,从0到4,步进2
'jr'
//切片本身会创建新的对象(字符串不可变)
集合
字典
>>> len(name)
5
>>> type(l1)
>>> print l1
['This', 'is', 'a', 'Tom']
>>> len(l1)
4
>>> l1.append('James') //列表对象的append()方法可用于为其补充新元素
>>> print l1
['This', 'is', 'a', 'Tom', 'James']
>>> len(l1)
5