目录
Number:数字
数字类型可分为数值类和布尔类型。数值类又分为整型和浮点型两种。
python3中取消了short,long,统一用int表示整数。取消了double类型,统一用float表示,这里的float就是其他语言中的双精度double
使用方法type(a)
,可以获知参数a的类型
>>>type(-1)
<class 'int'>
>>>type(1.1)
<class 'float'>
>>>type(1*1)
<class 'int'>
>>>type(1+0.1)
<class 'float'>
>>>type(2//2)
<class 'int'>
>>>type(2/2)
<class 'float'>
注意上面的除法计算,python3中默认/
为浮点数除法,用//
表示整除
进制与进制转换
生活中常用的进制是10进制,计算机中常用的是2、8、16进制。
python3中默认进制为10进制。想要显示的表示2进制可以使用0bxxx
这样的方式,如0b11
表示10进制的3。同理,8进制和16进制可以使用0oxxx
,0xaaa
的方式表示,如0o10
,0x1F
,分别表示10进制的8和31
存在进制就有进制转换的问题。python3中提供了下列方法进行进制转换:
int(a)
将某进制数a转换成10进制数bin(a)
将某进制数a转换成2进制数oct(a)
将某进制数a转换成8进制数hex(a)
将某进制数a转换成16进制数
布尔类型与复数
布尔类型:bool,负数complex。
bool(a)
方法可以将a转化为真假值True
和False
,注意大小写
>>> int(True)
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(0)
False
>>> bool(2)
True
>>> bool(-1)
True
>>> bool(0)
False
>>> bool('abc')
True
>>> bool('')
False
>>> bool([])
False
>>> bool({})
False
>>> bool({1,2,3})
True
>>> bool(None)
False
所有非0值都为True
,前两行表示了布尔类型从属Number类的原因。所有非空值皆为True
复数表示方法很简单,在数值后加上标识符j
即可。如36j
str:字符串
字符串是使用单引号``
,双引号""
,和三引号""" """
/''' '''
包括的内容。
>>> 'hello world'
'hello world'
>>> 1
1
>>> '1'
'1'
>>> type(1)
<class 'int'>
>>> type('1')
<class 'str'>
>>> "hello"
'hello'
>>> 'let's go'
SyntaxError: invalid syntax
>>> "let's go"
"let's go"
>>> 'let"s go'
'let"s go'
>>> 'let\'s go'
"let's go"
如果是表示字符串的引号必须成对出现,而在表征字符串的引号中插入其他类型的引号不会有问题,即便不成对。如果是同类型的引号可以使用\
进行转义。
python建议每行字符数不超过79,当字符串内容超长,可以使用三引号来表示
>>> '''
hello world
hello world
'''
'\nhello world\nhello world\n'
>>> """
hello world
hello world
hello world
"""
'\nhello world\nhello world\nhello world\n'
>>> """ hello world \n hello world\nhello world\n"""
' hello world \n hello world\nhello world\n'
>>> print(""" hello world \n hello world\nhello world\n""")
hello world
hello world
hello world
>>> print('hello world \n hello world\nhello world\n')
hello world
hello world
hello world
>>> """hello world
hello world"""
'hello world\nhello world'
>>> 'hello\
world'
'helloworld'
IDLE中>>>
表示等待输入,不仅可见的abc这样的字符会被录入,回车符虽不可见带也会被录入,最后表现为\n
的形式。
如果想在一行字符串中自定换行,可以使用print()
搭配转义字符\n
。
单引号和双引号的换行可以采用最后一例的形式,字符串行尾加上\
就可换行了
转义字符
在输入文字中有下列情况需要特殊处理:
- 特殊字符
- “看不见的字符”,换行、制表符等
- 与语言标准相冲突的字符,比如python中的
'
所以有了转义字符,形式为\
+字母
,比如说\r
回车,\n
换行,\t
横向制表符,或者是\'
为了输出'
小知识:\r
与\n
的区别
\r
——移动光标至行首\n
——光标下移一格
不同系统对于换行
采取的方式不同
- Unix系统,行尾是
\n
; - Windows系统,每行结尾是
\n\r
; - Mac系统里,每行结尾是
\r
Python能根据系统自动判别换行符的形式。但对如下输入,不同系统结果是一致的。
print aaaa \r bbbb
print aaaa \n bbbb
输出
bbbb
aaaa
bbbb
原始字符串
当想要将\n
本身作为字符串内容输出时,有两种方式,一是对\
进行转义操作,即\\
,另一种是在字符串前加上标志符r
>>> print('c:\north\northwest')
c:
orth
orthwest
>>> print('c:\\north\\northwest')
c:\north\northwest
>>> print(r'c:\north\northwest')
c:\north\northwest
注意,转换字符串为原始字符串的前提是字符串本身合法
>>> r'let"s go'
'let"s go'
>>> r'let's go'
SyntaxError: invalid syntax
字符串运算
字符串同数字一样,存在一些基本的四则运算操作,+
用来拼接字符串,*
用来复制字符串,必须是一个字符串和一个数做乘积
>>> "hello"+" world"
'hello world'
>>> "hello "*3
'hello hello hello '
>>> "hello"*"world"
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
"hello"*"world"
TypeError: can't multiply sequence by non-int of type 'str'
为了获取字符串中特定位置的元素,可以在字符串后加上[num]
,表明希望取到num位置的元素。
num为正时,从左往右,从0开始计数。为负时,从右往左,从-1开始计数
>>> "hello world"[0]
'h'
>>> "hello world"[4]
'o'
>>> "hello world"[-1]
'd'
>>> "hello world"[-3]
'r'
为了获取字符串中的一段,使用[start:end]
的方式获取[start,end)
范围内的字符串内容,start
小于end
时为空
>>> "hello world"[0:4]
'hell'
>>> "hello world"[0:5]
'hello'
>>> "hello world"[0:-1]
'hello worl'
>>> "hello world"[0:-3]
'hello wo'
>>> "hello world"[-3,2]
''
为了取到全部的字符串,特别是末尾的元素,可以令end>=str.length
,当不写start
和end
时,默认为从头开始取,和一直取到尾
>>> "hello world"[6:11]
'world'
>>> "hello world"[6:20]
'world'
>>> "hello world"[6:]
'world'
>>> "hello world"[:-4]
'hello w'
>>> "hello world"[-5:]
'world'
组
英雄技能qwer是一组,世界杯小组赛会分组。基本数据类型都源自于生活中的抽象。 python中有多个表示“组”概念的类型,专业名词叫做 序列 。顾名思义,其含义是类型对象中的每个元素都有一个能标识自身的 序号 。序列下面有列表和元组两个。
列表:list
列表表示方式为[xxx]
,内置元素类型不用刻意一致,可以类型混合,当然也可以嵌套
>>> type([1,2,3,4,5,6])
<class 'list'>
>>> type(["hello","world",1,9])
<class 'list'>
>>> type(["hello","world",1,9,True,False])
<class 'list'>
>>> type([[1,2],[True,False]])
<class 'list'>
列表的基本操作常用的有取值,截取,列表拼接(加法),列表复制(乘法)
>>> ["新月打击","苍白之暴","月神冲刺","月之降临"][0]
'新月打击'
>>> ["新月打击","苍白之暴","月神冲刺","月之降临"][3]
'月之降临'
>>> ["新月打击","苍白之暴","月神冲刺","月之降临"][0:2]
['新月打击', '苍白之暴']
>>> ["新月打击","苍白之暴","月神冲刺","月之降临"][-1:]
['月之降临']
>>> ["新月打击","苍白之暴","月神冲刺","月之降临"]+['点燃','虚弱']
['新月打击', '苍白之暴', '月神冲刺', '月之降临', '点燃', '虚弱']
>>> ['点燃','虚弱']*['点燃','虚弱']
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
['点燃','虚弱']*['点燃','虚弱']
TypeError: can't multiply sequence by non-int of type 'list'
>>> ['点燃','虚弱']*3
['点燃', '虚弱', '点燃', '虚弱', '点燃', '虚弱']
>>> ['点燃','虚弱']-['点燃']
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
['点燃','虚弱']-['点燃']
TypeError: unsupported operand type(s) for -: 'list' and 'list'
注意例子中和字符串相似的地方,关注下乘法规则,留心列表无减法操作(字符串同理)
元组
元组是另一个表示“组”概念的数据类型。使用(xxx)
表示。和列表一样,数据类型可以不一致。基本操作也是基本一致的。
>>> (1,2,3,4,5)
(1, 2, 3, 4, 5)
>>> (1,'-1',True)
(1, '-1', True)
>>> (1,2,3)[0]
1
>>> (1,2,3,4)[0:2]
(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)
>>> type((1,2,3))
<class 'tuple'>
>>> type((1))
<class 'int'>
>>> type(('string'))
<class 'str'>
注意到最后两个例子中的特殊之处,原因在于()
本身即是元组标识,也是数学运算符。
>>> (1)*2
2
>>> (1)
1
为了避免歧义,python默认当括号内只有一个元素时,()
作数学运算符处理。为了表示只有一个元素的元组,可以参考下法,列表等则无此问题
>>> (1,)
(1,)
>>> type((1,))
<class 'tuple'>
>>> type([1])
<class 'list'>
空元组表示方式很简单,单纯的()
即可
>>> type(())
<class 'tuple'>
序列共性
[]
取值
>>> 'hello world'[2]
'l'
>>> [1,2,3][2]
3
>>> [1,2,3,4,5][0:3]
[1, 2, 3]
>>> "hello world"[0:8:2]
'hlow'
[0:8:2],最后一个参数表示 步长
- 存在性判断
>>> 3 in [1,2,3,4,5,6]
True
>>> 2 not in [1,2,3,4,5,6]
False
- 长度
>>> len([1,2,34,5])
4
>>> len('hello world')
11
- 最值
>>> max([1,2,3,4,5,6])
6
>>> min('abcde')
'a'
补充知识点
函数ord()
可以获取参数的ascll码
>>> ord('a')
97
>>> ord(' ')
32
集合:set
说完序列的有序,该说无序的了,即set,集合。用{}
表示。因为无序所以不能获取指定序号的元素
>>> 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 does not support indexing
>>> {1,2,3,4,5,6}[0:1]
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
{1,2,3,4,5,6}[0:1]
TypeError: 'set' object is not subscriptable
集合可以存放不同类型数据,但必须是能哈希化的数据
>>> {1,2,'3',[1,2,3]}
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
{1,2,'3',[1,2,3]}
TypeError: unhashable type: 'list'
>>> {1,2,'3',(1,2,3)}
{1, 2, (1, 2, 3), '3'}
python中的集合拥有同数学集合一样的特性,特异性,无序性
>>> {1,1,2,3,3,4}
{1, 2, 3, 4}
>>> {1,3,4,2,7}
{1, 2, 3, 4, 7}
集合支持和序列一样的存在性判断和长度获取操作。
>>> len({1,2,3})
3
>>> 1 in {1,2,3}
True
>>> 4 not in {4,5,6}
False
集合最重要的是三个逻辑判断,差交并。
>>> {1,2,3,4,5,6} - {1,3,5}
{2, 4, 6}
>>> {1,2,3,4,5,6} & {1,3,5}
{1, 3, 5}
>>> {1,2,3,4,5,6} | {3,5,7}
{1, 2, 3, 4, 5, 6, 7}
表示空集有些特殊
>>> type(set())
<class 'set'>
>>> len(set())
0
使用单纯的{}
获得的是空字典
字典:dict
字典类型是一种特殊的集合,采用键-值对形式构建。创建方式为{key:value,key:value,....}
。
>>> type({1:1,2:2,3:3})
<class 'dict'>
>>> {'Q':'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}[0]
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
{'Q':'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}[0]
KeyError: 0
>>> {'Q':'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}['Q']
'新月打击'
>>> {'Q':'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}['R']
'月神冲刺'
>>> {'Q':'新月打击','Q':'苍白之瀑','E':'月之降临','R':'月神冲刺'}['Q']
'苍白之瀑'
>>> {'Q':'新月打击','Q':'苍白之瀑','E':'月之降临','R':'月神冲刺'}
{'Q': '苍白之瀑', 'E': '月之降临', 'R': '月神冲刺'}
因为类似于集合,所以不可以用角标访问元素。虽然不会报错,但务必保证key唯一,否则丢数据算轻的
key必须是不可变类型,比如str、int、元组。value可以是任意类型,甚至可以嵌套字典
>>> {1:'新月打击','1':'苍白之瀑','E':'月之降临','R':'月神冲刺'}
{1: '新月打击', '1': '苍白之瀑', 'E': '月之降临', 'R': '月神冲刺'}
>>> type({'Q':'新月打击','W':'苍白之瀑','E':{1:1},'R':'月神冲刺'})
<class 'dict'>
>>> {[1,2]:'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
{[1,2]:'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}
TypeError: unhashable type: 'list'
>>> {(1,2):'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}
{(1, 2): '新月打击', 'W': '苍白之瀑', 'E': '月之降临', 'R': '月神冲刺'}