iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 8
0
自我挑戰組

Python X 30系列 第 8

python 起手式:淺談tuples

tuples 也是python的資料型態之一,同屬於“列表”的一種。
宣告方式如下

>>> tuple =('a','b','c');
>>> tuple
('a', 'b', 'c')

tuple 同樣與list一樣可以放置不同的資料型態,唯一不同的是兩者的宣告方式一個是用小括弧;另一個則是使用中括弧。兩者最大不同之處就是tuple不能修改、新增,而list可以。

>>> list = ['a','b','c','d']
>>> list[0]='c'
>>> list
['c', 'b', 'c', 'd']
>>> tuple=('a',0,2,'c')
>>> tuple
('a', 0, 2, 'c')
>>> tuple[0]='b'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

參考資料:
https://www.w3schools.com/python/python_tuples.asp
http://itman2266.blogspot.com/2013/05/python-tuple.html
https://ithelp.ithome.com.tw/articles/10185010


上一篇
python起手式:探討lists
下一篇
python 起手式:dictionary
系列文
Python X 3030
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言