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 | 描述 |
---|---|
pi | pi的近似值 |
e | e的近似值 |
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 )