新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythontuple()
內(nèi)置函數(shù)tuple()有助于在 python 中創(chuàng)建元組。元組是具有多個(gè)元素的單個(gè)變量。元組中的元素是不可變的,這意味著不能修改它。

創(chuàng)新互聯(lián)專注于眉山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供眉山營(yíng)銷型網(wǎng)站建設(shè),眉山網(wǎng)站制作、眉山網(wǎng)頁(yè)設(shè)計(jì)、眉山網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造眉山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供眉山網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
**tuple(iterable)** #where iterable may be list, range, dict etc
元組()參數(shù):
接受單個(gè)參數(shù)。元組元素是有序的,不可更改的,并且允許重復(fù)的值。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 可迭代的 | 可列舉的(列表、范圍等)。)或迭代器對(duì)象 | 可選擇的 |
元組()返回值
如果 iterable 沒有傳遞給tuple(),該函數(shù)將創(chuàng)建一個(gè)空的 tuple 并返回一個(gè) TypeError。
| 投入 | 返回值 | | 如果可迭代 | 元組 |
Python 中tuple()方法的示例
示例 1:如何使用tuple()創(chuàng)建元組
tuple1 = tuple()
print('tuple1 =', tuple1)
# creating a tuple from a list
tuple2 = tuple([2, 3, 5])
print('tuple2 =', tuple2)
# creating a tuple from a string
tuple1 = tuple('Python')
print('tuple1 =',tuple1)
# creating a tuple from a dictionary
tuple1 = tuple({2: 'one', 4: 'two'})
print('tuple1 =',tuple1)
輸出:
tuple1 = ()
tuple2 = (2, 3, 5)
tuple1 = ('P', 'y', 't', 'h', 'o', 'n')
tuple1 = (2, 4)示例 2:用元組()演示類型錯(cuò)誤的程序
# Error when a non-iterable is passed
tuple1 = tuple(1)
print(tuple1)
輸出:
Traceback (most recent call last):
File "/home/eaf759787ade3942e8b9b436d6c60ab3.py", line 5, in
tuple1=tuple(1)
TypeError: 'int' object is not iterable 分享標(biāo)題:創(chuàng)新互聯(lián)Python教程:Pythontuple()
URL分享:http://m.fisionsoft.com.cn/article/dphheje.html


咨詢
建站咨詢
