標籤:shel 標籤 module last ast round define mod 刪除
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> tuple_1 = (1,‘a‘,2,"b",3,‘c‘)
>>> tuple_1
(1, ‘a‘, 2, ‘b‘, 3, ‘c‘)
>>> tuple_1 = (1,‘a‘,(2,"b"),3,‘c‘)
>>> tuple_1
(1, ‘a‘, (2, ‘b‘), 3, ‘c‘)
>>> tuple_1[0]
1
>>> tuple_1[2][0]
2
>>> tuple_1[2:]
((2, ‘b‘), 3, ‘c‘)
>>> tuple_1[:2]
(1, ‘a‘)
>>> tuple_1[:]
(1, ‘a‘, (2, ‘b‘), 3, ‘c‘)
>>> tuple_2=tuple_1[:]
>>> tuple_2[0]=4
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
tuple_2[0]=4
TypeError: ‘tuple‘ object does not support item assignment
>>> tmp=(23)
>>> type(tmp)
<class ‘int‘>
>>> tmp2=1,2
>>> type(tmp2)
<class ‘tuple‘>
>>> #是不是tuple類型關鍵是逗號而不是小括弧
>>> tmp3=[]
>>> type(tmp3)
<class ‘list‘>
>>> #是不是list類型只要看有沒有中括弧
>>> tmp4=()
>>> type(tmp4)
<class ‘tuple‘>
>>> #如果只有小括弧而無逗號就是空元組
>>> tmp5=(1,)
>>> type(tmp5)
<class ‘tuple‘>
>>> tmp5
(1,)
>>> tmp6=1,
>>> type(tmp6)
<class ‘tuple‘>
>>> 6*(6,)
(6, 6, 6, 6, 6, 6)
>>> type(6,)
<class ‘int‘>
>>> tmp=(‘掃地僧‘,‘張三丰‘,"風清揚","石破天")
>>> tmp=tmp[:2]+(‘郭靖‘,)+tmp[2:]
>>> tmp
(‘掃地僧‘, ‘張三丰‘, ‘郭靖‘, ‘風清揚‘, ‘石破天‘)
>>> #原來的tmp還存在,但無名稱標籤貼上,會被python自動回收
>>> del tmp
>>> tmp
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
tmp
NameError: name ‘tmp‘ is not defined
>>> #python有回收機制,無需用del去主動刪除,無標籤貼上時會自動回收
【python】元組