寫一個 Python 程序,用一個例子創建一個 Tuple。在 Python 編程語言中,有兩種方法可以創建元組。第一個選項是使用()括號,另一個選項是 tuple()函數。
下面的 Python 示例使用這兩個選項創建了一個空元組。
x = ()
print(x)
y = tuple()
print(y)
空元組的輸出
()
()
在這個示例程序中,我們正在創建一個項目元組。我們都知道元組函數接受任何迭代器,並將其轉換為元組。因此,我們聲明了一個水果列表,並使用 Python 元組函數將其轉換為元組。
x = (10, 20, 30, 40, 50)
print(x)
print("Datatype of y = ", type(x))
fruits = ['Kiwi', 'Banana', 'Apple', 'Orange']
y = tuple(fruits)
print(y)
print("Datatype of Fruits = ", type(fruits))
print("Datatype of y = ", type(y))
原創文章,作者:RCWU5,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/127754.html