Python 程序:打印元组项目

创新互联python教程:

创新互联公司专注于白城企业网站建设,成都响应式网站建设,成都做商城网站。白城网站建设公司,为白城等地区提供建站服务。全流程按需制作网站,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

编写一个 Python 程序来打印元组中所有项目的列表。我们可以使用 print 函数打印整个元组。在这个例子中,我们声明了字符串和 int 元组并打印它们。

# Print Tuple Items

numTuple = (10, 20, 30, 40, 50)

print("The Tuple Items are ")
print(numTuple )

strTuple = ('C#', 'Java', 'Python', 'C')

print("\nThe String Tuple Items are ")
print(strTuple )

在这个 Python 程序中,我们使用 for 循环范围来访问每个元组项。第一个 for 循环从第一个元组项迭代到最后一个,并打印每个元素。第二个循环打印字符串元组中的所有水果。

# Print Tuple Items

numTuple = (10, 20, 30, 40, 50)

print("The Tuple Items are ")
for i in range(len(numTuple)):
    print("Tuple Item at %d Position = %d" %(i, numTuple[i]))           

print("=========")
fruitsTuple = ('apple', 'orange', 'kiwi', 'grape')
for fruit in fruitsTuple:
    print(fruit)
The Tuple Items are 
Tuple Item at 0 Position = 10
Tuple Item at 1 Position = 20
Tuple Item at 2 Position = 30
Tuple Item at 3 Position = 40
Tuple Item at 4 Position = 50
=========
apple
orange
kiwi
grape

文章标题:Python 程序:打印元组项目
网页URL:http://www.zyruijie.cn/qtweb/news40/6590.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联