2.Python数值计算

1.数值数据类型
python内置函数type()用于判断对象的类型 
2.运算符
操作符操作
+
-
*
/
**指数
abs()绝对值
//取整
%取余

在大多数情况下,对float的操作产生float,对int的操作产生int。大多数时候,我们甚至不必担心正在操作什么类型的操作。

3.类型转换和舍入
int()			转换成整型,也可以操作数字字符串,int("123")
float()			转换成浮点型
round(n,m)		保留m位
round(n)		保留1位
4.math库

Python提供了一个特殊的库(模块)‘math’,提供了许多其他有用的数学函数。

@ 求二次方程的根
def main():
	print("This program finds the real solutions to a quadratic")
	print()
	a = float(input("Enter coefficient a: "))
    b = float(input("Enter coefficient b: "))
    c = float(input("Enter coefficient c: "))
	discRoot = math.sqrt(b*b - 4*a*c)
	root1 = (-b + discRoot) / (2 * a)
	root1 = (-b - discRoot) / (2 * a)
	print()
	print("The solutions are:",root1,',',root2)
Python描述
pipi的近似值
ee的近似值
sqrt(x)开根号
sin(x),cos(x),tan(x)三角函数
asin(x),acos(x),atan(x)反三角函数
log(x)ln x
log10(x)log x
exp(x)e^x
ceil(x)最小的>=x的整数
floor(x)最大的<=x的整数
@ n的阶乘
def main():
	n = input("Enter n:")
	n = int(n)
	result = 1
	for i in range(n+1)
		result = result * i
	print(result )
	
@ range()函数的另一种用法
@ range(a,b,x):产生a到b-1以x为步长的序列
def main():
	n = input("Enter n:")
	n = int(n)
	result = 1
	for i in range(n,0,-1)	#i = [n n-1 n-2 ... 1]
		result = result * i
	print(result )
	
	
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值