A003. Python 数据类型
代码 与 写代码
代码: 现实世界事物在计算机中的映射
写代码: 用 标记符号,数据和运算在计算机中描述现实世界
画家画画: 颜色,图形,光影,结构...元素构成作品
编程: 基本元素抽象描述, 用数据描述世界 -- 基本数据类型
Python 数据类型:
1. Number 数字类型
0b 二进制, 0x 十六进制, 0o 八进制
type() 类型检测
>>> type(1) <class 'int'> >>> type(-2) <class 'int'> >>> type(1.1) <class 'float'> >>> type(1+0.1) <class 'float'> >>> type(1+1.0) <class 'float'> >>> type(1*5) <class 'int'> >>> type(1*5.0) <class 'float'> >>> type(2/2) <class 'float'> >>> type(2//2) <class 'int'> 单斜杠 除 双斜杠 整除
bin() hex() oct() int() 进制转换
>>> int(0b110) 6 >>> int(0o70) 56 >>> bin(8) '0b1000' >>> bin(0b10) '0b10' >>> bin(0x1E) '0b11110' >>> bin(0o10) '0b1000' >>> oct(0b10) '0o2' >>> oct(10) '0o12' >>> oct(0xF) '0o17' >>> hex(12) '0xc' >>> hex(0b110) '0x6' >>> hex(0o12) '0xa'
布尔类型 True / False bool()
>>> true Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'true' is not defined >>> True True >>> False False >>> type(True) <class 'bool'> >>> int(True) 1 >>> int(False) 0 >>> bool(1) True >>> bool(0) False >>> bool(None) False >>> bool([ ]) False >>> bool('') False
单双引号
\’ 转义字符 >>> print('hello \n world') hello world >>> print('hello \\n world') hello \n world
三引号 多行显示
多行显示 换行 >>> ''' ... this ... is ... a ... dog ... ''' '\nthis\nis\na\ndog\n' >>> print('\nthis\nis\na\ndog\n') this is a dog >>> 'hello\ ... world\ ... !' 'helloworld!'
原始字符串 r
>>> print( r'hello \\n world' )
hello \\n world
字符串的运算
>>> 'hello'[1:2] 'e' >>> 'hello'[1:] 'ello' >>> 'hello'[:-1] 'hell' >>> 'hello'[1:-1] 'ell' >>> 'hello'*2 'hellohello' >>> 'hello'+' '+'hi' 'hello hi' >>> 'hello'[0] 'h' >>> 'hello'[-1] 'o'

浙公网安备 33010602011771号