创新互联Python教程:
写一个 Python 程序来找到元组的长度。在这个例子中,我们声明了整数和字符串元组,并使用 Python len 函数来查找这些元组的长度。
# 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 嵌套元组长度,您必须使用该嵌套项的索引位置。例如,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://www.shufengxianlan.com/qtweb/news44/463594.html
成都网站建设公司_创新互联,为您提供动态网站、全网营销推广、品牌网站建设、网站收录、网站设计公司、python
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联