python学习笔记——数字基本运算

数字函数及相关操作

加法:+

减法:-

乘法:*

除法:/
注意这里的除法得到的是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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值