本博客主要为了学习Python使用,若有错误欢迎指正。
1. Python数据类型
整数和具有小数部分的数字以不同的方式存储,这是两种不同数据类型
Python提供 type()函数返回值得数据类型
Python的 int 可表示无限位
2. Python内置数值操作(其他均与C语言相同)
** 指数运算
// 整数除法
/ 浮点除法
3. 类型转换和舍入
强制类型转换,高精度转为低精度存在损失
对数字四舍五入可使用内置 round(num,number) 函数,对num进行小数点后number位的舍入
int() float()等转换,可以用于将数字字符串转换为数字
4. Python的math库(其他均与C语言相同)import math引入
pi π的近似值
exp(x) e^x
ceil(x) 大于等于 x 的整数
floor(x) 小于等于 x 的整数
以ax^2+bx+c=0为例,保证有解
# File: quadratic.py
# A program that computes the real roots of a quadratic equation
import math
def main():
print("This program finds the real solutions to a quadratic.")
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
dis = math.sqrt(b * b - 4 * a * c)
root1=(-b + dis) / (2 * a)
root2=(-b - dis) / (2 * a)
print("The solutions are: ",root1,root2)
main()