編寫一個 Python 程序,用數字或數字元組創建一個元組。第一個將創建一個值為 22 的元組。
# Create a Numeric Tuple
nTuple = 22,
print("Tuple Items = ", nTuple)
numTuple = (10, 20, 40, 60, 80, 100)
print("Tuple Items = ", numTuple)
Tuple Items = (22,)
Tuple Items = (10, 20, 40, 60, 80, 100)
在這個 Python 程序中,我們創建了一個空元組,它允許用戶輸入元組項。在循環中,我們將每個數字元組項連接到一個已經聲明的元組。
# Create a Numeric Tuple
intTuple = ()
number = int(input("Please enter the Total Tuple Items to store = "))
for i in range(1, number + 1):
value = int(input("Please enter %d Tuple Item = " %i))
intTuple += (value,)
print("Tuple Items = ", intTuple)
原創文章,作者:SDXGZ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/127044.html