基本数据类型
python3中 Number 只有 int 和 float(默认就是双精度)
/ 除法 2/2 float = 1.0
// 整除 2//2 int = 1
bool 一切非0都是真
一切空值都是False
None是False
各进制的表示与转换
二进制:0b10 0b11…
八进制:0o
十六进制:0x
任意数字转换
二进制 bin()
十进制 int()
十六进制 hex()
八进制 oct()
python中的单引号和双引号以及三引号
” “” ”’ ”’ “”” “””
三引号可以换行
python里的转义字符
特殊的字符
无法看见的字符
与语言本身语法有冲突的字符
\n 换行
\’ 单引号
\t 横向制表符
\r 回车
原始字符串
print(r’sdsdd\n’) >>> sdsdd\n
当一个字符串前面加上r 那么这个字符串就是原始字符串 不会转义
字符串运算
+号 连接
“hello”+”world” >>>’helloworld’
“hello”*3 >>>’hellohellohello’
“hello world”[0] >>> ‘h’ 很牛掰
“hello world”[3] >>> ‘l’ 很牛掰
“hello world”[-3] >>>’r’
>>>"hello world"[6] //[正数]代表字符串的序号 从0开始
'w'
>>>"hello world"[-5] //[负数] 从字符串的末尾往回数5位
'w'
>>>"hello world"[0:4] //左包右不包
'hell'
>>> "hello world"[0:-1] //负数代表步长 从字符串的末尾往回数1位
'hello worl'
>>> "hello world"[0:-3]
'hello wo'
>>> "hello world"[6:11]
'world'
>>> "hello world"[6:] //截取从第六个开始到末尾
'world'
>>> "hello python java c# javascript ruby"[6:]
'python java c# javascript ruby'
>>> "hello python java c# javascript ruby"[:-4]
'hello python java c# javascript '
>>> "hello python java c# javascript ruby"[-4:]
'ruby'
列表的定义
列表里什么都能放
list
>>> [1,2,3,4,5,6]
[1, 2, 3, 4, 5, 6]
>>> type([1,2,3,4,5,6])
<class 'list'>
>>> ["hello","world",1,9]
['hello', 'world', 1, 9]
>>> ["hello",1,9,True,False]
['hello', 1, 9, True, False]
>>> [[1,2],[3,4],[True,False]]
[[1, 2], [3, 4], [True, False]] //嵌套列表
>>> type([[1,2],[3,4],[True,False]])
<class 'list'>
>>> ["心悦打击","苍白智跑","精练志活"][2]
'精练志活'
>>> ["心悦打击","苍白智跑","精练志活"][0][2]
'打'
>>> ["心悦打击","苍白智跑","精练志活"][0:2]
['心悦打击', '苍白智跑']
>>> ["心悦打击","苍白智跑","精练志活"][-1:]
['精练志活']
>>> ["心悦打击","苍白智跑","精练志活"]+['点燃','虚弱']
['心悦打击', '苍白智跑', '精练志活', '点燃', '虚弱']
>>> ["心悦打击","苍白智跑","精练志活"]*2
['心悦打击', '苍白智跑', '精练志活', '心悦打击', '苍白智跑', '精练志活']
元组
tuple
>>> (1,2,3)
(1, 2, 3)
>>> (1,'-1',True)
(1, '-1', True)
>>> (1,2,3,4)[0]
1
>>> (1,2,3,4)[0:2]
(1, 2)
>>> (1,2,3)*3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> type((1,2,3))
<class 'tuple'>
>>> type((1)) // ()里只有一个元素 就当做()运算了
<class 'int'>
>>> type((1,))
<class 'tuple'>
list str tulpe 都是序列
序列里的每一个元素都有顺序和序号
切片 [0:] [:-5] 类似这种
+ *
>>> 3 in [1,2,3,4,5] //判断元素是否在列表中
True
>>> 3 not in[1,2,3,4,5] //判断元素是否不在这个列表中
False
>>> len([1,2,6,4,5,6])
6
>>> len("hellworld")
9
>>> max([1,2,3,4,5,6])
6
>>> min([1,2,3,4,5,6])
1
>>> max('hello world')
'w'
>>> ord('w') 阿斯克码~
119
集合 set
集合是无序的,没有下标索引
集合的特色就是 - 差集 &交集 | 并集
>>> type({1,2,3,4})
<class 'set'>
>>> {1,1,2,2,3,3,4,4} //集合不重复
{1, 2, 3, 4}
>>> len({1,2,3})
3
>>> 1 in {1,2,3}
True
>>> 1 not in {1,2,3,4}
False
>>> {1,2,3,4,5,6}-{4,5} //求两个集合的差集
{1, 2, 3, 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}
dict字典
空的{}表示字典 不表示集合
key:必须是不可变的类型 int str
字典由很多个key value 组合而成 字典不是序列 字典也是集合
>>> {'Q':'心悦打击','W':'苍白质朴','E':'也只降临','R':'月神冲刺'}['Q']
'心悦打击'
>>> {'Q':'心悦打击','W':'苍白质朴','E':'也只降临','R':'月神冲刺'}['W']
'苍白质朴'
>>> {'Q':'心悦打击','Q':'心悦打击','W':'苍白质朴','E':'也只降临','R':'月神冲刺'} //不会重复
{'Q': '心悦打击', 'W': '苍白质朴', 'E': '也只降临', 'R': '月神冲刺'}
>>> {1: '心悦打击','1': '心悦打击', 'W': '苍白质朴', 'E': '也只降临', 'R': '月神冲刺'}
{1: '心悦打击', '1': '心悦打击', 'W': '苍白质朴', 'E': '也只降临', 'R': '月神冲刺'}