Python基础数据类型
1.整数int与浮点数float
>>> type(1)
<class 'int'>
>>> type(1.2)
<class 'float'>
>>> type(1*1.0)
<class 'float'>
>>> type(2//2) // 2//2=1 整除
<class 'int'>
>>> type(2/2) // 2/2=1.0
<class 'float'>
2. 10进制 2进制 8进制 16进制
二进制:0b10 = 2
八进制:0o10 = 8
十六进制:0x10 = 16
*任意进制转换成二进制
>>> bin(10)
'0b1010'
>>> bin(10)
'0b1010'
>>> bin(0o7)
'0b111'
>>> bin(0xe)
'0b1110'
*任意进制转换成十进制
>>> int(0b111)
7
>>> int(0o77)
63
*任意进制转换成十六进制
>>> hex(0o7777)
'0xfff'
>>> hex(0b111)
'0x7'
>>> hex(888)
'0x378'
*任意进制转换成八进制
>>> oct(0b111)
'0o7'
>>> oct(0x777)
'0o3567'
3.布尔类型
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> int(True)
1
>>> int(False)
0
>>> bool(0)
False
>>> bool(1)
True
>>> bool(3)
True
>>> bool(-1)
True
>>> bool(1.1)
True
4.列表list
>>> type( [1,2,3,4,5,6] ) //列表是用 [] 来定义的
<class 'list'>
>>> type(["hello","world",1,9])
<class 'list'>
>>> type([[1,2],[3,4],[True,False]]) //嵌套列表or二维数组
<class 'list'>
>>> ["新月打击","苍白之瀑","月之降临","月神冲刺"][0]
'新月打击'
>>> ["新月打击","苍白之瀑","月之降临","月神冲刺"][3]
'月神冲刺'
>>> ["新月打击","苍白之瀑","月之降临","月神冲刺"][0:2]
['新月打击', '苍白之瀑']
>>> ["新月打击","苍白之瀑","月之降临","月神冲刺"][-1:]
['月神冲刺']
>>>["新月打击","苍白之瀑","月之降临","月神冲刺"]+['虚弱','点燃']
['新月打击', '苍白之瀑', '月之降临', '月神冲刺', '虚弱', '点燃']
>>> ['虚弱','点燃']*3
['虚弱', '点燃', '虚弱', '点燃', '虚弱', '点燃']
>>> ['虚弱','点燃']-['点燃']
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
['虚弱','点燃']-['点燃']
TypeError: unsupported operand type(s) for -: 'list' and 'list'
5.嵌套列表
>>> [['巴西','克罗地亚','墨西哥','柯麦隆'],['','','',''],['','','',''],['','','','']]
6.元组
>>> type( (1,2,3) ) //元组是用() 来定义的
<class 'tuple'>
>>> (1,2,3,4,5) //一个元组
(1, 2, 3, 4, 5)
>>> (1,'-1',True) //一个元组中的数据可以不同类型
(1, '-1', True)
>>> (1,2,3,4)[0] //数组访问方式
1
>>> (1,2,3,4)[0:2] //从0开始,两个元素组成一个新的元组
(1, 2)
>>> (1,2,3)+(4,5,6) //元组的加法
(1, 2, 3, 4, 5, 6)
>>> (1,2,3)*3 //元组的乘法
(1, 2, 3, 1, 2, 3, 1, 2, 3)
为什么不只用元组或者列表?
答:
元组一旦创建不可改变,不能追加元素,弹出元素等;
只能对元组中进行索引,不能对元组中进行赋值;
元组的操作更高效,适合存放一组常量;
而list列表就可以做上述很多的不可以。
//几个常用类型的英文简称
>>> type((1,2,3))
<class 'tuple'> //元组
>>> type(1)
<class 'int'> //整型
>>> type([1,2,3])
<class 'list'> //列表
>>> type('hello')
<class 'str'> //字符串
为啥下列两种type出来不是元组类型?
>>> type((1))
<class 'int'>
>>> type(('hello'))
<class 'str'>
答:因为python中,() 同样代表运算括号,例如:(1+1)*2
而type() 会默认认为 () 代表运算括号。
>>> type((1))
<class 'int'>
>>> type(1)
<class 'int'>
如何表示只有一个元素的元组,空元组呢?
>>> type((1,)) //只有一个元素的元组
<class 'tuple'>
>>> type(()) //空列表
<class 'tuple'>
>>> type([1])
<class 'list'>
7.序列 (有序)
包括list tuple str
>>> 'hello world'[2]
'l'
>>> [1,2,3][2]
3
7.1.切片
>>> [1,2,3,4,5][0:3]
[1, 2, 3]
>>> [1,2,3,4,5][-1:]
[5]
//??????????[0:8:2]???????????????
>>> "hello world"[0:8:2]
'hlow'
7.2.in 运算符
>>> 3 in [1,2,3,4,5] //判断3是否 在序列中
True
>>> 3 not in [1,2,3,4,5] //判断3是否 不在序列中
False
7.3 len函数:计算序列的长度
>>> len([1,2,3,4,5,6])
6
>>> len(["hello world"])
1
7.4 max,mix函数
>>> max([1,2,3,4,5,6])
6
>>> min([1,2,3,4,5,6])
1
//////////////////////////////////////////////////////////////////////
>>> ord('w')
119
>>> ord('d')
100
>>> ord(' ')
32
>>> max('hello world') //输出字符ascll编码最大的w
'w'
>>> min('hello world')
' '
>>> min('helloworld')
'd'
/////////////////////////////////////////////////////////////////////
下列输出的是首字母最大或者最小的那个元素
>>> max(["time","long","hello"])
'time'
>>> min(["time","long","hello"])
'hello'
8.集合 (无序)
>>> type({1,2,3,4,5,6})
<class 'set'>
//不支持访问序列下标
>>> {1,2,3,4,5,6}[0]
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
{1,2,3,4,5,6}[0]
TypeError: 'set' object is not subscriptable
//集合中不存在重复的元素
>>> {1,1,2,2,2,3,4,5,5,7}
{1, 2, 3, 4, 5, 7}
8.1 集合支持的操作
>>> type({1,2,3})
<class 'set'>
>>> len({1,2,3})
3
>>> 3 in {1,2,3}
True
>>> 3 not in {1,2,3}
False
8.2 集合的特殊操作
//求两个集合的差集
>>> {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}
//定义一个空的集合
>>> type(set())
<class 'set'>
>>> len( set() )
0
9.字典
由很多key和value组合,集合类型 (set)
{key1:value1, key2,value2}
value: str int float list set dict
key: 必须是不可变的类型
>>> type({1:1,2:2,3:3})
<class 'dict'>
>>> type({}) //空的字典
<class 'dict'>
//字典是无序的,所以报错
>>> {'Q':'新月打击','W':"苍白之瀑",'E':"月之降临",'R':"月神冲刺"}[0]
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
{'Q':'新月打击','W':"苍白之瀑",'E':"月之降临",'R':"月神冲刺"}[0]
KeyError: 0
//通过key访问value
>>> {'Q':'新月打击','W':"苍白之瀑",'E':"月之降临",'R':"月神冲刺"}['Q']
'新月打击'
>>> {'Q':'新月打击','W':"苍白之瀑",'E':"月之降临",'R':"月神冲刺"}['R']
'月神冲刺'
//字典不允许有重复的key
>>> {'Q':'新月打击','Q':"苍白之瀑",'E':"月之降临",'R':"月神冲刺"}
{'Q': '苍白之瀑', 'E': '月之降临', 'R': '月神冲刺'}
//数字1和字符串‘1’会被识别为两个不同的key
>>> {1:'新月打击','1':"苍白之瀑",'E':"月之降临",'R':"月神冲刺"}
{1: '新月打击', '1': '苍白之瀑', 'E': '月之降临', 'R': '月神冲刺'}
//key值不能是list类型,但可以是tuple类型
>>> {[1,2]:'新月打击','1':"苍白之瀑",'E':"月之降临",'R':"月神冲刺"}
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
{[1,2]:'新月打击','1':"苍白之瀑",'E':"月之降临",'R':"月神冲刺"}
TypeError: unhashable type: 'list'
总结: