Python 全栈开发:python元组tuple

元组

什么是元组:“元组就是一个不可变的列表” (并不是列表,只是为了便于记忆)

用途:用于存放多个值,只能读不能二次赋值

表现形式:小括号()中间以逗号分割各个元素

eg:

tuples = ('a','b','c','d')

 

元组常用的操作

生成一个简单的元组

tuples = tuple(range(5))
print(tuples)    #结果(0, 1, 2, 3, 4)

 

len(tuple)

#计算元组长度
tuples = (0, 1, 2, 3, 4)
print(len(tuples)) #结果: 5

 

in 和not in

# in 和not in
tuples = ('a','b','c','d')
print('a' in tuples)        #结果:True
print('z' in tuples)        #结果:False
print('x' not in tuples)    #结果:True
print('d' not in tuples)    #结果:False

 

元组的取值

#元组的取值
tuples = ('a','b','c','d')

item = tuples[0]
print(item)                 #结果:a
item = tuples[2]
print(item)                 #结果:c

#while循环
c = 0
while c < len(tuples):
    print(tuples[c])
    c+=1
#for循环
for item in tuples:
    print(item)

 

 

元组的切片

关于元组的切片操作,在这里不在赘述,请参考字符串的切片。

Python 全栈开发:python字符串切片

 

转载于:https://www.cnblogs.com/fixdq/p/8619282.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值