加法:+
减法:-
乘法:*
除法:/
注意这里的除法得到的是float,如果除不尽还会受到浮点数精度的影响。
例如:
print(7/3,6/3,9/10)
得到的结果是:2.3333333333333335 2.0 0.9
取余:%
整除://
乘方:**
整数、浮点数都可以运用!正数、负数也都可以用!
print(2**3,1.5**2,1.5**2.5)
print((-1)**2)
结果:8 2.25 2.7556759606310752 1
开方:
① **0.5
② 用sqrt:使用cmath
中的函数sqrt
import cmath
x = float(input('input the number:'))
num_sqrt = cmath.sqrt(x)
print(x,"sqrt is:",num_sqrt)
一个经典的运用:一元二次方程
import cmath
a = float(input('input the a:'))
b = float(input('input the b:'))
c = float(input('input the c:'))
if a==0.0:
print("ERROR!")
delta = b*b-4*a*c
if delta<0:
print("no solution")
else:
x1 = (-b+cmath.sqrt(delta))/2/a
x2 = (-b-cmath.sqrt(delta))/2/a
print("the equation is:{0}x^2+{1}x+c=0".format(a,b,c))
print("the solutions:",x1,x2)