Python程序:计算元组长度

创新互联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 程序

在这个例子中,我们声明了嵌套和混合元组。如果您想要 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://hxwzsj.com/article/djpdcgs.html

其他资讯

Copyright © 2025 青羊区翔捷宏鑫字牌设计制作工作室(个体工商户) All Rights Reserved 蜀ICP备2025123194号-14
友情链接: 自适应网站建设 营销网站建设 成都网站制作 重庆网站制作 专业网站设计 成都响应式网站建设 成都网站设计 重庆电商网站建设 网站设计制作 营销型网站建设 成都网站建设公司 专业网站设计 重庆网站建设 成都网站设计 网站制作公司 手机网站设计 古蔺网站建设 宜宾网站设计 成都网站制作 网站建设改版 成都网站建设 成都网站设计