温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中有哪些基本运算

发布时间:2021-09-29 13:42:49 来源:亿速云 阅读:162 作者:iii 栏目:开发技术

这篇文章主要讲解了“Python中有哪些基本运算”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python中有哪些基本运算”吧!

1. 基本运算

>>>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+1
6
>>>5.0+1 #这里运算结果会自动转换为浮点型
6.0
>>>10/2
5.0
>>>10/3 #这里由于计算机是将数字转换为二进制进行计算时,浮点数转换偏差造成的
3.3333333333333335
>>>2.5*2
5.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

2. 变量与变量类型

>>>a=6 #变量定义与赋值
>>>a
6
>>>b = 3*a #变量运算与赋值
>>>b
18
>>>type(a) #type函数用于检测变量类型
<class 'int'>
>>> b = True #布尔类型
<class 'bool'>
>>> c = 3.14 #浮点数类型
>>> type(c)
<class 'float'>
>>> d = 'www.jb51.net'
>>> 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'>
>>>

3. 专业计算模块:math

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']
>>> pi
Traceback (most recent call last):
 File "<pyshell#1>", line 1, in <module>
  pi
NameError: name 'pi' is not defined
>>> math.pi
3.141592653589793
>>> from math import *
>>> pi
3.141592653589793
>>>>>> sqrt(9) #sqrt计算开方
3.0
>>> ceil(3.1) #ceil向上取整
4
>>> floor(3.9) #floor向下取整
3
>>> fmod(7,4) # fmod取余数
3.0

感谢各位的阅读,以上就是“Python中有哪些基本运算”的内容了,经过本文的学习后,相信大家对Python中有哪些基本运算这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI