【python】元組

來源:互聯網
上載者:User

標籤: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】元組

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.