Python的资料储存容器,
可以分为tuple、串列(list)、字典(dict)与集合(set)四种,
以下表格简单介绍一下 :
Python的资料储存容器 | 说明 |
---|---|
tuple | 用於依序储存资料,可以依照顺序取出,但不可更改,是不可变的物件。 |
串列(list) | 用於依序储存资料,可以依照顺序取出,可以更改。 |
字典(dict) | 储存的资料为**「键(key)」与「值(value)」对应的资料,使用「键」查询「值」。取出字典所有资料後,发现与建构字典时输入资料的顺序不同,字典储存资料是没有顺序性的**,字典也可以视为关联性阵列(associative array)。 |
集合(set) | 储存没有顺序性的资料,要找出资料是否存在,储存不需要键与值对应的资料,就很适合使用集合。 |
使用「()」建立tuple,
tuple在Python中表示连续资料元素串接在一起,
tuple是不可以更改的,
tuple是不可以更改的,
tuple是不可以更改的,
因为很重要所以要说三次 !!
#input
tuple_1 = ()
print(tuple_1)
#output
()
#input
tuple_1 = 1,2,3
tuple_2 = (1,2,3) #一般而言会加上()表示是tuple
print(tuple_1)
print(tuple_2)
#output
(1,2,3)
(1,2,3)
#input
tuple_1 = 1,2,3
print(tuple_1[2])
#output
3
#input
tuple_1 = 1,2,3
a,b,c = tuple_1
print('a=',a,',b=',b,',c=',c)
#output
a= 1 ,b= 2 ,c= 3
#input
a = 100
b = 200
print('交换前','a=',a,',b=',b)
a,b = b,a
print('交後前','a=',a,',b=',b)
#output
交换前 a= 100 ,b= 200
交後前 a= 200 ,b= 100
#input
list_1 = [1,2,3,4]
tuple_1 = tuple(list_1)
print(tuple_1)
#output
(1, 2, 3, 4)
#input
tuple_1 = (1,2,3,4)
tuple_2 = (tuple_1,5,6)
print(tuple_1)
print(len(tuple_2))
print(tuple_2[0][1])
#output
(1, 2, 3, 4)
3
2
#input
tuple_1 = ('a',)
tuple_2 = ('b')
print(tuple_1)
print(type(tuple_1))
print(type(tuple_2))
#output
('a',)
<class 'tuple'>
<class 'str'>
以上是tuple的基本观念,
请大家务必要弄清楚喔!!
大家加油~~~
<<: [Day6] THM Basic Pentesting
这是在找linked list资料看到harry xie大神文章提到的 里面是常见演算法的时间复杂度...
此系列文章会同步发文到个人部落格,有兴趣的读者可以前往观看喔。 在 E2E 测试中,不仅有选取元素...
前言 快要结束了,今明两天会讲点拿下Server Initial Access(foothold)之...
今年的疫情蛮严重的,希望大家都过得安好,希望疫情快点过去,能回到一些线下技术聚会的时光~ 今天目标:...
子曰:『工欲善其事,必先利其器。 前言 昨天提到依照 Ground truth 改变的速度会让不同...