07tuple

本文最后更新于 2022-05-26 11:25:30

Tuple 元组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 元组 不可变序列
# 在多任务环境下,同时操作对象不需要加锁
# 如果元组中对象本身是不可变对象,则不能再引用其他对象
# 如果元组中的对象是可变对象(比如列表),则可变对象的引用不允许改变,但是数据可以改变

# 创建元组
var1 = ("king","tom",1)
print(var1)
print(type(var1))

var2 = tuple(("hello","world",11))
print(var2)
print(type(var2))

# 只有一个元素时。必须加逗号,不然就不是元组是原始类型
var3 = ("king",)
print(type(var3))

# 空元组
var4 = ()
print(type(var4))
var5 = tuple()
print(type(var5))

# ('king', 'tom', 1)
# <class 'tuple'>
# ('hello', 'world', 11)
# <class 'tuple'>
# <class 'tuple'>
# <class 'tuple'>
# <class 'tuple'>

# 遍历
for item in var1:
print(item,end=" ")
print()
# king tom 1

07tuple
https://jiajun.xyz/2020/10/22/python/01base/07tuple/
作者
Lambda
发布于
2020年10月22日
更新于
2022年5月26日
许可协议