4.python中表示组-列表List,元祖tuple,集合set,dict字典

列表

现实世界中存在一组一组的事物,

>>> [1,2,3,4,5,6]
[1, 2, 3, 4, 5, 6]
>>> type([1,2,3,4,5,6])
<class 'list'>
>>> ['hello world',1,2]
['hello world', 1, 2]
>>> type(['hello world',1,2])
<class 'list'>
>>> [[2,2],[3,3]]
[[2, 2], [3, 3]]
>>> type([[2,2],[3,3]])
<class 'list'>//二维数组,嵌套列表

列表的基本操作

>>> ['新月打击','苍白','月亮','月神']
['新月打击', '苍白', '月亮', '月神']
>>> ['新月打击','苍白','月亮','月神'][0]
'新月打击'
>>> ['新月打击','苍白','月亮','月神'][3]
'月神'
>>> ['新月打击','苍白','月亮','月神'][0:2]
['新月打击', '苍白']
>>> ['新月打击','苍白','月亮','月神'][-1:]
['月神']
>>> 
>>> ['月之降临','苍白之瀑','月之降临','月神冲刺']+['点燃你','虚弱']
['月之降临', '苍白之瀑', '月之降临', '月神冲刺', '点燃你', '虚弱']
>>> ['点燃你','虚弱']*3
['点燃你', '虚弱', '点燃你', '虚弱', '点燃你', '虚弱']
>>> ['点燃你','虚弱']-['点燃你']
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    ['点燃你','虚弱']-['点燃你']
TypeError: unsupported operand type(s) for -: 'list' and 'list'
>>> 

元祖

[['巴西', '克罗地亚', '墨西哥', '喀麦隆', ''], [], [], [], [], [], [], []]
>>> 
>>> (1,2,3,4,5)
(1, 2, 3, 4, 5)
>>> (1,'-1',True)
(1, '-1', True)
>>> (1,2,3,4,5)[0]
1
>>> (1,2,3,4,5)[0:2]
(1, 2)
>>> (1,2,3,4,5)*3
(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
>>> 
>>> type((1,2,3))
<class 'tuple'>//元组
>>> type(1)
<class 'int'>
>>> type([1,2,3])
<class 'list'>
>>> type('hello world')
<class 'str'>
>>> 
>>> type((1))
<class 'int'>
>>> 

序列总结

>>> type(1)
<class 'int'>
>>> type((1))
<class 'int'>
>>> (1+1)*2
4
>>> type((1))
<class 'int'>
>>> type(1,)
<class 'int'>
>>> type((1,))
<class 'tuple'>
>>> 
.
>>> type([1])
<class 'list'>
>>> 'hello world'[2]
'l'
>>> [1,2,3][2]
3
>>> 

>>> [1,2,3,4,5][0:3]
[1, 2, 3]
>>> [1,2,3,4,5][-1:]
[5]
>>> 
>>> 3 in [1,2,3]
True
>>> 10 in [1,2,3]
False
>>> 3 not in [1,2,3]
False
>>> len([1,2,3,4,5,6])
6
>>> len('hello world')
11
>>> max([1,2,3,4,5,6])
6
>>> min([1,2,3,4,5,6])
1
>>> max('hello world')
'w'
>>> min('hello world')
' '
>>> min('helloworld')
'd'
>>> 
>>> ord('w')
119
>>> ord('d')
100
>>> ord('')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    ord('')
TypeError: ord() expected a character, but string of length 0 found
>>> ord(' ')
32
>>> 
在这里插入代码片

set集合

集合是无序的,无法定位,切片
集合是不重复的

>>> {1,2,3,4,5,6}
{1, 2, 3, 4, 5, 6}
>>> type({1,2,3,4,5,6})
<class 'set'>
>>> {1,1,2,2,3,3}
{1, 2, 3}
>>> 
>>> len({1,2,3})
3
>>> 1 in {1,2,3}
True
>>> 1 not in {1,2,3}
False
>>> {1,2,3,4,5,6}-{3,4}
{1, 2, 5, 6}   //求两个集合差值
>>> 
>>> {1,2,3,4,5,6}&{3,4}
{3, 4} //交集
>>> {1,2,3,4,5,6}| {3,4,7}
{1, 2, 3, 4, 5, 6, 7} //合集
>>> 
>>> set()
set()          //定义空集
>>> type(set())
<class 'set'>
>>> 

dict字典

{key1:value1,key2:value2,key3:value3}

>>> {1:1,2:2,3:3}
{1: 1, 2: 2, 3: 3}
>>> type({1:1,2:2,3:3})
<class 'dict'>
>>> {'Q':'新月打击','W':'苍白','E':'降临','R':'月神'}['Q']
'新月打击'
>>> {'Q':'新月打击','W':'苍白','E':'降临','R':'月神'}['W']
'苍白'
>>> {'Q':'新月打击','Q':'苍白','E':'降临','R':'月神'}['Q']
'苍白'
>>> {'Q':'新月打击','Q':'苍白','E':'降临','R':'月神'} //字典不可以有相同key
{'Q': '苍白', 'E': '降临', 'R': '月神'}
>>> 
>>> {1:'新月打击','1':'苍白','E':'降临','R':'月神'}['1']
'苍白'
>>> {1:'新月打击','1':'苍白','E':'降临','R':'月神'}[1]
'新月打击'
>>> 
>>> {1:'新月打击','1':'苍白','E':{1:1},'R':'月神'}
{1: '新月打击', '1': '苍白', 'E': {1: 1}, 'R': '月神'}
>>> type({1:'新月打击','1':'苍白','E':{1:1},'R':'月神'})
<class 'dict'>
>>> //value:str,float,list ,set, dict
>>> {}
{}  //空的字典
>>>//key必须是不可变的类型,int,str,

思维导图总结基本数据类型

在这里插入图片描述

2级标题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值