Python程序:向元组中添加一个项

创新互联Python教程:

创新互联专注于企业营销型网站建设、网站重做改版、卓尼网站定制设计、自适应品牌网站建设、html5商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为卓尼等各大城市提供网站开发制作服务。

编写一个 Python 程序向元组中添加一个条目。Python 元组不允许您向现有元组中添加项目。下面的 Python 示例将通过连接新项目和当前元组来创建一个新元组。

# Add an Item to Tuple

intTuple = (10, 20, 30, 40, 50)
print("Tuple Items = ", intTuple)

intTuple = intTuple + (70,)
print("Tuple Items = ", intTuple)

intTuple = intTuple + (80, 90)
print("Tuple Items = ", intTuple)

intTuple = intTuple[2:5] + (11, 22, 33, 44) + intTuple[7:]
print("Tuple Items = ", intTuple)

在这个 Python 示例中,我们允许用户输入 tuple 项,并将它们添加到一个空 Tuple 中。为了达到同样的目的,我们使用 for 循环范围来迭代元组项。

# Add an Item to Tuple

intTuple = ()

number = int(input("Enter the Total Number of Tuple Items = "))
for i in range(1, number + 1):
    value = int(input("Enter the %d Tuple value = " %i))
    intTuple = intTuple + (value,)

print("Tuple Items = ", intTuple)
Enter the Total Number of Tuple Items = 4
Enter the 1 Tuple value = 22
Enter the 2 Tuple value = 99
Enter the 3 Tuple value = 122
Enter the 4 Tuple value = 77
Tuple Items =  (22, 99, 122, 77)

网站标题:Python程序:向元组中添加一个项
本文链接:http://www.shufengxianlan.com/qtweb/news44/265994.html

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

广告

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