- 并且第四种的t4,t5,t6分别等于’T3’,90,‘数据库原理与应用’
print(type(t1))
print(type(t2))
print(type(t3))
<class ‘tuple’>
<class ‘tuple’>
<class ‘tuple’>
- 单元素元组的表示一定要记得添加逗号
t123=t1,t2,t3
print(type(t123))
t123
t124=((‘元组1’,100),(‘元组2’))
t124
print(type(‘经济与管理’))
print(type((‘经济与管理’,))) # 单元素元组的表示一定要记得添加逗号
- 元组的大小和内容不可更改,不可改变元素的顺序,不可对元素进行重新复制
index()
功能描述
Python 元组 index()
方法用于从元祖中找出某个指定对象第一个匹配到的索引值,如果这个对象不在元祖中会报一个异常。
- 语法
tuple.index(obj[,start=0[,end=len(tuple)]])
- 参数说明
obj
– 检索某个指定的对象
start
– 可选参数,开始索引,默认为0。(可单独指定)
end
– 可选参数,结束索引,默认为元祖的长度。(不能单独指定)
- 返回值
如果元组中包含 检索的对象 返回索引值,否则抛出异常。
count()
功能描述
-
Python中的
count()
方法用于统计某个元素在元组中出现的次数。 -
语法
tuple.count(obj)
- 参数
obj
— 元组中统计的对象
- 返回值
返回元素在元祖中出现的次数。
===========================================================================
使用dir(tuple)
或者help(tuple)
[‘add’,
‘class’,
‘class_getitem’,
‘contains’,
‘delattr’,
‘dir’,
‘doc’,
‘eq’,
‘format’,
‘ge’,
‘getattribute’,
‘getitem’,
‘getnewargs’,
‘gt’,
‘hash’,
‘init’,
‘init_subclass’,
‘iter’,
‘le’,
‘len’,
‘lt’,
‘mul’,
‘ne’,
‘new’,
‘reduce’,
‘reduce_ex’,
‘repr’,
‘rmul’,
‘setattr’,
‘sizeof’,
‘str’,
‘subclasshook’,
‘count’,
‘index’]
Help on class tuple in module builtins:
class tuple(object)
| tuple(iterable=(), /)
|
| Built-in immutable sequence.
|
| If no argument is given, the constructor returns an empty tuple.
| If iterable is specified the tuple is initialized from iterable’s items.
|
| If the argument is a tuple, the return value is the same object.
|
| Built-in subclasses:
| asyncgen_hooks
| UnraisableHookArgs
|
| Methods defined here:
|
| add(self, value, /)
| Return self+value.
|
| contains(self, key, /)
| Return key in self.
|
| eq(self, value, /)
| Return self==value.
|
| ge(self, value, /)
| Return self>=value.
|
| getattribute(self, name, /)
| Return getattr(self, name).
|
| getitem(self, key, /)
| Return self[key].
|
| getnewargs(self, /)
|
| gt(self, value, /)
| Return self>value.
|
| hash(self, /)
| Return hash(self).
|
| iter(self, /)
| Implement iter(self).
|
| le(self, value, /)
| Return self<=value.
|
| len(self, /)
| Return len(self).
|
| lt(self, value, /)
| Return self<value.
|
| mul(self, value, /)
| Return self*value.
|
| ne(self, value, /)
| Return self!=value.
|
| repr(self, /)
| Return repr(self).
|
| rmul(self, value, /)
| Return value*self.
|
| count(self, value, /)
| Return number of occurrences of value.
|
| index(self, value, start=0, stop=9223372036854775807, /)
| Return first index of value.
|
| Raises ValueError if the value is not present.
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| class_getitem(…) from builtins.type
| See PEP 585
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| new(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
========================================================================
- 将元组变成列表
List
tl01=list(t123)
[(‘T3’, 96, ‘大数据分析’), (‘T3’, 90, ‘数据库原理与应用’), (‘T3’, 96, ‘大数据分析’)]
========================================================================
immutable
:不可修改性
不能直接给元组的元素进行赋值
例外:如果元组的元素是可修改的对象,如list
,则其值是可修改的
- 对整个元组重新赋值是可以的,id内存也会改变,但如果只是单单对元组的一个元素进行赋值,则必须要保证该元素可修改
======================================================================
x,y=1,2
print(type(x)) # <class ‘int’>
x,_=1,2
print(_) # 2
比较以下1和2:特殊用法
====================================================================
加法
乘法
t1=(1,2,3)
t2=(3,4,5)
t3=(‘1’,‘2’,‘3’)
t4=(‘a’,‘b’,‘c’)
print(t1<t2) # True
print(t3<t4) # True
尽管两者的类型和内容一样,但 is
的结果还是False
========================================================================
二者对比的本质是可更改与不可更改的对比