Python入门教程1. 基本运算【四则运算、变量、math模块等】 原创
Python  /  管理员 发布于 7年前   237
在熟悉了Python的基本安装与环境配置之后,我们来看看Python的基本运算操作。
>>>6 # 这里的‘#'是注释符号,不参与运算6>>>666666666666666 #整数类型,原样输出666666666666666>>>3.14 #浮点数类型3.14>>>id(6) #id()函数用于查看内存地址1409471616>>>help(id) #help()函数可用于查看函数文档Help on built-in function id in module builtins:id(obj, /) Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (CPython uses the object's memory address.)>>> 5+16>>>5.0+1 #这里运算结果会自动转换为浮点型6.0>>>10/25.0>>>10/3 #这里由于计算机是将数字转换为二进制进行计算时,浮点数转换偏差造成的3.3333333333333335>>>2.5*25.0>>>2.5**2 #符号**用指数计算,例如这里计算2.5的2次方6.25>>>5//2 # 符号//可用于计算相除的结果再进行取整2>>>5%2 #取余,没啥好说的1>>>5.0%2 #浮点数的取余运算,同理1.0>>>(5 + 6) * 2 - 2 ** 3 + 5//2 - 5 % 3 #综合计算(表达式计算)14
>>>a=6 #变量定义与赋值>>>a6>>>b = 3*a #变量运算与赋值>>>b18>>>type(a) #type函数用于检测变量类型<class 'int'>>>> b = True #布尔类型<class 'bool'>>>> c = 3.14 #浮点数类型>>> type(c)<class 'float'>>>> d = ''>>> type(d)<class 'str'>>>> e = ['a','b','c'] #列表类型>>> type(e)<class 'list'>>>> f = ('x','y','z') #元组类型>>> type(f)<class 'tuple'>>>> g = {'a':'1','b':'2','c':'3'} #字典类型>>> type(g)<class 'dict'>>>>
sin(x) | 求x的正弦 |
cos(x) | 求x的余弦 |
asin(x) | 求x的反正弦 |
acos(x) | 求x的反余弦 |
tan(x) | 求x的正切 |
atan(x) | 求x的余切、反正切 |
hypot(x,y) | 求直角三角形的斜边长 |
fmod(x,y) | 求x/y的余数 |
ceil(x) | 取不小于x的最小整数(向上取整) |
floor(x) | 取不大于x的最大整数(向下取整) |
fabs(x) | 求绝对值 |
exp(x) | 求e的x次幂 |
pow(x,y) | 求x的y次幂 |
log10(x) | 求x以10为底的对数 |
sqrt(x) | 求x的平方根 |
pi | 圆周率π的值(常量) |
>>> abs(-2) #求绝对值(系统函数)2>>> pow(2,4) #计算2的4次方(系统函数)16.0>>> round(3.4) #round四舍五入运算(系统函数)3>>> round(3.5) #round四舍五入运算4>>> import math #使用import语句可以引入math模块进行运算>>> dir(math) #查看库中所有东西['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']>>> piTraceback (most recent call last): File "<pyshell#1>", line 1, in <module> piNameError: name 'pi' is not defined>>> math.pi3.141592653589793>>> from math import *>>> pi3.141592653589793>>>>>> sqrt(9) #sqrt计算开方3.0>>> ceil(3.1) #ceil向上取整4>>> floor(3.9) #floor向下取整3>>> fmod(7,4) # fmod取余数3.0
简单入门教程~
基本一看就懂~O(∩_∩)O~
未完待续~~欢迎讨论!!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号