Python 的 tuple(~)
構造函數根據給定的可迭代對象創建一個新元組。元組是不可變的對象,這意味著它們一旦創建就無法修改。
參數
1.iterable
| iterable
| optional
用於創建新元組的迭代。
返回值
由可迭代對象構造的元組對象。如果未提供可迭代對象,則返回空元組。
例子
基本用法
要創建一個新元組:
tup = (1, 2, 3)
要根據 favorite_languages
字典的值創建新元組:
favorite_languages = {
'bob': 'french',
'emma': 'spanish',
'david': 'english',
'silvia': 'french',
}
tuple(favorite_languages.values())
('french', 'spanish', 'english', 'french')
不可變的
如果我們嘗試修改一個元組,我們會得到 TypeError
:
tup = (1, 2, 3)
tup[1] = 10
TypeError: 'tuple' object does not support item assignment
相關用法
- Python tuple()用法及代碼示例
- Python turtle.write_docstringdict()用法及代碼示例
- Python turtle.reset用法及代碼示例
- Python turtle.delay()用法及代碼示例
- Python turtle.write()用法及代碼示例
- Python turtle.getpen()用法及代碼示例
- Python turtle.isvisible用法及代碼示例
- Python turtle.onkey用法及代碼示例
- Python turtle.tracer用法及代碼示例
- Python turtle.end_fill()用法及代碼示例
- Python turtle.undo用法及代碼示例
- Python turtle.onscreenclick()用法及代碼示例
- Python turtle.up()用法及代碼示例
- Python turtle.distance()用法及代碼示例
- Python turtle.tilt()用法及代碼示例
- Python turtle.fillcolor()用法及代碼示例
- Python turtle.clearstamp()用法及代碼示例
- Python turtle.done()用法及代碼示例
- Python turtle.isvisible()用法及代碼示例
- Python turtle.reset()用法及代碼示例
- Python turtle.degrees用法及代碼示例
- Python turtle.degrees()用法及代碼示例
- Python turtle.circle()用法及代碼示例
- Python turtle.tracer()用法及代碼示例
- Python turtle.left()用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Python | tuple constructor。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。