来自python的【tuple元组总结】

本文详细介绍了Python元组的定义、获取数据的方法,包括索引、切片和循环,强调了元组的不可变性。同时探讨了元组的内置函数,如len(), max(), min(), tuple()以及count()和index(),并讨论了元组的嵌套和具名元组(namedtuple)的概念。" 104162469,8548833,解决Unity项目VS Code无法多开问题,"['游戏开发', 'Unity', 'VS Code', '编辑器配置']
摘要由CSDN通过智能技术生成

元组定义

  • 按顺序输出元组内容
  • 与列表类似,使用()以及, 定义,内部数据可以是任意类型。
tup1 = ('1',1,set('123'))
print(tup1) #('1', 1, {'1', '3', '2'})
  • 元组是不可变数据,内部数据不能修改。会报错
#tup1[0] =99 
#TypeError: 'tuple' object does not support item assignment
# 不支持修改哦
  • 创建空元组
tup2 = ()
  • 元组中如果只有一个元素的时候,需要在元素后面添加逗号,否则会将括号当作运算符作用。
tup3 =(99)
tup4 = (99.9)
print(type(tup3)) #<class int>
print(type(tup4)) #<class float>  
# 以实际的数据类型为准

tup5 = (88,)
print(type(tup5))#<class tuple>
  • tuple 与 list 是十分相近。
  • tuple(iterable) :iterable可以是序列、支持迭代的容器或迭代器对象。tuple就是内置函数,将可迭代的数据转换为元组。
  • tuple 详细定义,详细可以看下面的定义哦
class tuple([iterable])
# tuple是可迭代的
Tuples may be constructed in a number of ways:
# tuple可以通过多种方式来进行构建:
Using a pair of parentheses to denote the empty tuple: ()
# 使用一对括号表示空元组:()
Using a trailing comma for a singleton tuple: a, or (a,)
#使用逗号作为单例元组:a,或(a,) [需要加逗号哦,不然会被认为是别的数据]
Separating items with commas: a, b, c or (a, b, c)
# 用逗号分隔项目:a, b, c或(a, b, c)
Using the tuple() built-in: tuple() or tuple(iterable)
#使用内置的tuple(): tuple()或tuple(iterable) 进行构建
The constructor builds a tuple whose items are the same and in the same order as iterable’s items. 
#构造函数构建一个元组,其中的项与iterable的项相同,且顺序相同。
iterable may be either a sequence, a container that supports iteration, or an iterator object. 
#iterable可以是序列、支持迭代的容器或迭代器对象。
If iterable is already a tuple, it is returned unchanged. 
#如果iterable已经是一个元组,那么它将不加修改地返回。
For example, tuple('abc') returns ('a', 'b', 'c') and tuple( [1, 2, 3] ) returns (1, 2, 3). If no argument is given, the constructor creates a new empty tuple, ().
# tuple 就是将数据转换元组的内置函数 
#print(tuple())#()  没有数据 就返回一个空元组

Note that it is actually the comma which makes a tuple, not the parentheses. 
#注意,构成元组的实际上是逗号,而不是圆括号。
The parentheses are optional
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值