各进制的表示与转换
二进制表示:在开头加上0b
>>> 0b10
2
八进制表示:在开头加上0o
>>> 0o10
8
16进制表示:在开头加上0x
>>> 0x10
16
进制转换
二进制转换:使用bin()函数
>>> bin(10)
'0b1010'
>>> bin(0o10)
'0b1000'
>>> bin(0x10)
'0b10000'
>>>
十进制转换:使用int()函数
>>> int(0x10)
16
>>> int(0o10)
8
>>> int(10)
10
>>>
16进制转换:使用hex()函数
>>> hex(0b10000)
'0x10'
布尔类型与复数
True/False (严格区分大小写)
>>> type(true)
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
type(true)
NameError: name 'true' is not defined
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> bool(1)
True
>>> bool(0)
False
引号的使用
当用单引号和双引号表示字符串的时候 可以交叉使用单双引号, 必要的时候 也可以使用反斜杠 来转义
三引号(可以是三个单引号,也可以是三个双引号)的作用,可以避免字符串过长 用来字符串换行
>>> '''
hello
hello
hello
hello
'''
'\nhello\nhello\nhello\nhello\n'
>>>
还有一种方法,用转义字符:
>>> 'hello\
world'
'helloworld'
当要输出一个路径时,要用\在把层次转义
>>> print('c:\north\northen')
c:
orth
orthen
>>> print('c:\\north\\northen')
c:\north\northen
>>>
还有一种方法 ,就是在字符串前面加上r(加上r之后 ,它表示的就不是一个普通字符,而是一个原始字符)
>>> print(r'c:\north\northen')
c:\north\northen
>>>
判断元素是否在序列中
3 in [1,2,3,4,5] # not in
返回true
获取序列长度用 len
max()
min()
得到asc码函数 ord()
集合 set
1,无序 所以没有切片操作
2,不重复
求集合的长度用len()函数
判断某个元素是否在集合中 用In
从大集合中剔除他的一个子集 用 -
{1,2,3,4,5} - {1,2}
得到{3,4,5}{1,2,3,4,5} & {3,4,7} 得到{3,4} 求交集
{1,2,3,4,5} | {3,4,7} 得到{1,2,3,4,5,6,7} 求并集
定义空的集合并不是 {}//字典 而是 set()
字典 dict
id()函数 用来显示变量的内存地址
== 比较的是 变量值是否相等 is 比较的是 变量的身份(内存地址)是否相等
在任意编辑器 编写程序 , Win+R 输入cmd