新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
Python程序:計算元組長度
創(chuàng)新互聯(lián)Python教程:

寫一個 Python 程序來找到元組的長度。在這個例子中,我們聲明了整數(shù)和字符串元組,并使用 Python len 函數(shù)來查找這些元組的長度。
# Tuple Length
intTuple = (10, 20, 30, 40, 50)
print("Tuple Items = ", intTuple)
inttupleLength = len(intTuple)
print("Tuple Length = ", inttupleLength)
strTuple = ('apple', 'Mango', 'kiwi')
print("String Tuple Items = ", strTuple)
strtupleLength = len(strTuple)
print("String Tuple Length = ", strtupleLength)
Tuple Items = (10, 20, 30, 40, 50)
Tuple Length = 5
String Tuple Items = ('apple', 'Mango', 'kiwi')
String Tuple Length = 3
尋找元組長度的 Python 程序
在這個例子中,我們聲明了嵌套和混合元組。如果您想要 Python 嵌套元組長度,您必須使用該嵌套項的索引位置。例如,len(mTuple[4])返回嵌套元組(1,2,3,4) 的長度
mTuple = ('Apple', 22, 'Kiwi', 45.6, (1, 2, 3, 4), 16, [10, 30, 70])
print("Mixed Tuple Items = ", mTuple)
mtupleLength = len(mTuple)
print("Mixed Tuple Length = ", mtupleLength)
nestedtupleLength = len(mTuple[4])
print("Nested Tuple Length = ", nestedtupleLength)
nestedlistLength = len(mTuple[6])
print("List Nested inside a Tuple Length = ", nestedlistLength)
在這個 Python 程序中,我們聲明了一個空元組,并將用戶給定的值添加到該元組中,并計算長度。
# Tuple Length
intTuple = ()
number = int(input("Enter the Total Tuple Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d value = " %i))
intTuple += (value,)
print("Tuple Items = ", intTuple)
inttupleLength = len(intTuple)
print("Tuple Length = ", inttupleLength)
Enter the Total Tuple Items = 4
Enter the 1 value = 22
Enter the 2 value = 99
Enter the 3 value = 128
Enter the 4 value = 65
Tuple Items = (22, 99, 128, 65)
Tuple Length = 4 當前題目:Python程序:計算元組長度
轉載源于:http://m.fisionsoft.com.cn/article/djpdcgs.html


咨詢
建站咨詢
