1.算数运算符 +,-,*,/,%,**,//
(1)加号(Plus) +
(2)减号(Minus)-
(3)乘号(Multipy) *
(4)乘方(Power)**
例如:3 ** 4 = 3 * 3 * 3* 3
(5)除号1(Divide) /
(6)除号2(Fllor) //
a // b:结果与a,b的小数位有关
(7) 模(Modulo)%
2.位操作运算符
(1)左移位(Left Shift)<<
2 << 2 -->00000010向左移动2位:00001000 = 8(等效于右边加2个0,左边去2个0)
(2)右移位(Right Shift)>>
与左移位相反
(3)按位与(Bitwise AND)&
5 & 3 = 00000101 & 00000011 = 00000001(都是1才为1,否则为0)
(4)按位或(Bitwise OR)|
5 | 3 = 00000101 | 00000011 = 00000111(有1为1,都是0才为0)
(5)按位异或(Bit-wise XOR) ^
5 ^ 3 = 00000101 ^ 00000011 = 0000110(都是1或都是0返回0,否则返回1)
(6)按位取反(Bit-wise invert) ~
~ x = - (x+1)
例如:~00000101 = - 00000110 = 10000110
3.比较运算符 >, <, >=, <=, !=, ==,返回值是True,False
(1)小于(Less Than)<
(2)小于等于(Less Than or Equal To)<=
(3)大于(Greater Than)>
(4)大于等于(Greater Than or Equal To)>=
(5)判断是否等于(Equal To)==
(6)不等于(Not Equal To)!=
View Code
import math
from decimal import Decimal
from decimal import getcontext
from fractions import Fraction
def func_test(num_list, num_list2):
for one_num in num_list:
print('向上取整:', one_num, math.ceil(one_num))
print('向下取整:', one_num, math.floor(one_num))
print('取绝对值:', one_num, math.fabs(one_num))
print('截断整数部分:', one_num, math.trunc(one_num))
print('是否是数字:', one_num, math.isnan(one_num))
for one_num in num_list2:
print('开方:', one_num, math.sqrt(one_num))
print('阶乘:', one_num, math.factorial(one_num))
x, y = 12, 3
print('x,y乘积:', math.sqrt(x * x + y * y))
print('x,y乘积:', math.hypot(x, y))
print('幂指数计算:', math.pow(x, y))
print('浮点数计算:')
getcontext().prec = 4 # 设置全局精度
print(Decimal('0.1') / Decimal('0.3'))
print('分数简化:', Fraction(16, -10)) # 分子分母
print('圆周率:', math.pi)
print('取余操作:', math.fmod(10, 3))
print('对数运算:', math.log(x, y))
print('对数运算:', math.log10(x))
print('对数运算:', math.log1p(x))
print('角度弧度转化:', math.radians(30))
print('角度弧度转化:', math.degrees(math.pi))
print('三角函数使用——x的正弦、余弦:', math.sin(x), math.cos(x))
print('三角函数使用——x的双曲正弦、余弦', math.sinh(x), math.cosh(x))
print('三角函数使用——x的正切、双曲正切', math.tan(x), math.tanh(x))
# print 'x的反余弦', math.acos(x)
# print 'x的反双曲余弦', math.acosh(x)
# print 'x的反正弦', math.asin(x)
# print 'x的反双曲正弦', math.asinh(x)
print('Pi、e', math.pi, math.e)
print('e的幂指数', math.exp(y))
if __name__ == '__main__':
num_list = [3, 4, 5, -7.9, 6.4]
num_list2 = [6, 12, 25, 9]
func_test(num_list, num_list2)