Python3 Tuple

Tuple

元组和列表很类似,不同之处在于元组不能修改
元组使用 ( ) 、列表使用 [ ]
元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当做运算符使用

注:如果你读过我的上一篇博客 Python3 List , 想必你对序列类型的基本用法有了一定了解,因此,我不想对元组进行过多讲解,直接上例子。

1. 索引
>>> my_tuple = ("hello", "python", 2020)
print(my_tuple[1])
>>> my_tuple = ("hello", "python", 2020)
>>> print(my_tuple[:-1])
>>> print(my_tuple[1:2])
>>> print(my_tuple[:])
('hello', 'python')
('python',)
('hello', 'python', 2020)
2. 元组脚本操作符
>>> my_tuple = ("hello", "python", 2020)
print(len(my_tuple))
>>> tuple1 = (1, 2, 3)
>>> tuple2 = (4,)
>>> print(tuple1 + tuple2)
(1, 2, 3, 4)
>>> tuple1 = (1, 2, 3)
>>> print(tuple1 * 2)
(1, 2, 3, 1, 2, 3)
>>> tuple1 = (1, 2, 3)
>>> print( 1 in tuple1)
True
>>> tuple1 = (1, 2, 3)
>>> for t in tuple1:
>>>     print(t, end=" ")
1 2 3 
3. 元组内置函数

(1)len 函数,上面已经给出例子,此处不再重复
(2)max、min 函数:

>>> tuple1 = (1, 2, 3)
>>> print(max(tuple1))
>>> print(min(tuple1))
3
1

(3)tuple 函数,将可迭代系列转换为元组。

>>> list1 = [1, 2, 3]
>>> tuple1 = tuple(list1)
>>> print(tuple1)
(1, 2, 3)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞翔的红猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值