数字
整型 int 只有一种类型
浮点型 float(本身就是双精度)
除法(2/2)为float
除法(2//2)为int 整除
10进制 直接写
2进制 0b10 2进制的10回车转换十进制
8进制 0O10 8进制的10回车转换十进制
16进制 0X10 16进制的10回车转换十进制
进制转换:bin(数字)转换成为2进制
oct(数字)转换成为8进制
int(数字)转换成为10进制
hex(数字)转换成为16进制
bool 布尔类型:真True 1 假 False 0 必须大写
bool(0)=False; bool(’’)=False;bool([])=False;bool(不是0)=True
所有空值都是false;
complex 复数
字符串
str
建议每行79字符
单引号 在引号中还要加入“的时候需要使用‘’
或者‘let’s go’转义字符
双引号 在引号中还要加入‘’的时候需要使用“”
三引号 换行 表示多行\n 转义字符 表示换行
‘’’(或者""")可以不单独一行
qwerty
qretet
qewrt
‘’’(或者""")
输入为:print(""“abda\nwwg”"")
输出为:
abda
wwg
输入为:""“abda\nwwg”""
输出为:abda\nwwg
输入为:‘hello
world’
输出为:‘helloworld’
转义字符
特殊字符 看不见或与语法有冲突
\n 换行
\r 回车
’ 单引号
\t 横向制表符
输入为:print(‘hello\n world’)
输出为:hello\n world
输入为:print(r’hello\n world’)
输出为:hello\n world
字符串前加入r或R,就不是一个普通字符串而是一个原始字符串,所见即所得
但字符串的引号必须成对出现
字符串的运算
输入为:“hello”+“world”
输出为:‘helloworld’
输入为:“hello”*3
输出为:‘hellohellohello’
获取字符 切片
输入为:‘helloworld’[0] 以数字0开始
输出为:h
输入为:‘helloworld’[-1]
输出为:d
输入为:‘helloworld’[0:4]
输出为:hell
输入为:‘helloworld’[0:5]
输出为:hello
输入为:‘helloworld’[0:-1]
输出为:‘helloworl’
[x:y]表示步长不表示第几
输入为:‘helloworld’[5:10]
输出为:‘world’
输入为:‘helloworld’[5:20]
输出为:‘world’
输入为:‘helloworld’[-5:]
输出为:‘world’
输入为:‘helloworld’[5:]
输出为:‘world’
输入为:‘helloworld’[:-5]
输出为:‘hello’
============================================================================
补充
浮点运算
1.浮点运算的精确性
浮点数计算会产生不确定尾数
可以使用round函数辅助运算
round(x, d)
对x进行四舍五入,取d位小数
2.大精确浮点数运算
使用科学计数法,将整数和小数位数转换为数组
一些数学函数
abs(x) 绝对值
divmod(x, y) 商余 同时输出商和余数
pow(x, y[, z]) 幂余 (x**y)%z
round(x, d) 四舍五入,d默认值为0
max(x1, x2, x3…) 求最大值
min(x1, x2, x3…) 求最小值
一些字符串函数
str.center(width[, fillchar]) 字符串根据width居中
str.strip(chars) 从字符串两侧的chars删除
str.join(iter) 将一个元素,加入到字符串的中间(每个字中间都插入一个)
格式化:"{1}:计算机{0}的CPU占用率为{2}%".format(“2018”, “c”,10)