ITPub博客

首页 > IT职业 > IT生活 > python如何访问元组中的元素

python如何访问元组中的元素

IT生活 作者:wh7577 时间:2021-09-11 10:11:38 0 删除 编辑

python访问元组中的元素的方法:我们可以使用下标索引来访问元组中的值。具体使用方法如:【tup1 = ('physics', 'chemistry', 1997, 2000);print tup1[0]】。

可以使用下标索引来访问元组中的值。

(推荐教程:Python入门教程)

代码如下:

#!/usr/bin/python

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
 
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]

输出结果:

#tup1[0]:  physics
#tup2[1:5]:  [2, 3, 4, 5]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2480/viewspace-2832750/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    105
  • 访问量
    15386