Python3中的算术运算符详解
作者:AYO小涛 时间:2022-04-13 03:01:45
一·算术运算符
在python中,算术运算符与数学中的算术运算极为类似,只是有些运算符号有所差别。算术运算符的算术计算一般是运用于int类型与floa类型,同时+与*还可以运用到各种序列的拼接合并与复制中。
优先级:有括号先算括号内的,再乘方>乘除>整除>取余>加减进行运算。
运算符 | 名称 | 说明 |
+ | 求和 | 数学里的加法 |
- | 取差 | 数学里的减法 |
* | 相乘 | 数学里的乘法 |
/ | 相除 | 数学里的除法 |
% | 取余 | 得到除法的余数,比如:10%3=1 |
** | 幂运算 | 得到某数的的多少次方,比如:3**3=27 |
// | 整除 | 得到商的整数部分,比如:14//5=2 |
二·代码演示
1·求和 +
print(7 + 8 + 2)
# 输出结果:17
2·取差 -
print(7 - 2 - 2)
# 输出结果:3
3·相乘 *
print(7 * 2)
# 输出结果:14
4·相除 /
print(8 / 2)
# 输出结果:4
5·取余 %
print(9 % 2)
# 输出结果:1
6·幂运算 **
print(9 ** 2)
# 输出结果:81
7·整除 //
print(9 // 2)
# 输出结果:4
8·优先级混合运算
print((2+1)**3*3/3//2%2+2)
# 输出结果:3
三·'+'与'*'在序列中的使用
1·拼接合并 +
运用于字符串,列表,元组这三个数据序列中。
字符串:
str1 = "大家好"
str2 = '我是'
str3 = str2 + str1
print(str3)
# 输出结果:我是大家好
列表:
list1 = [1.2, 2]
list2 = [3, 4]
list3 = list1+list2
print(list3)
# 输出结果:[1.2, 2, 3, 4]
元组:
tuple1 = (1, 2)
tuple2 = (3, 4)
tuple3 = tuple1+tuple2
print(tuple3)
# 输出结果:(1, 2, 3, 4)
2·复制 *
运用于字符串,列表,元组这三个数据序列中。
字符串:
print('_'*30)
# 输出结果:______________________________
列表:
# * 列表 复制列表中的数据,不会复制括号
list1 = [1.2, 2]
list4 = list1*2
print(list4)
# 输出结果:[1.2, 2, 1.2, 2]
元组;
# * 元组 复制列表中的数据,不会复制括号
tuple1 = (1, 2)
tuple4 = tuple1*2
print(tuple4)
# 输出结果:(1, 2, 1, 2)
来源:https://blog.51cto.com/u_15320761/5621781
标签:Python,算术,运算符
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python Excel vlookup函数实现过程解析
2022-05-24 06:12:07
![](https://img.aspxhome.com/file/2023/5/112365_0s.png)
ajax标签导航实例详解教程
2008-02-01 10:54:00
![](https://img.aspxhome.com/file/UploadPic/up/2008020111434016.gif)
python 正则表达式的使用
2022-02-16 15:29:09
如何将数据库里的记录生成一个Excel文件?
2009-12-03 20:09:00
精细讲述SQL Server数据库备份多种方法
2009-01-13 13:33:00
python中reload重载实例用法
2021-05-12 07:02:09
从源码解析Python的Flask框架中request对象的用法
2021-02-20 02:15:57
Python 实现打印单词的菱形字符图案
2021-06-30 20:08:52
![](https://img.aspxhome.com/file/2023/8/93528_0s.jpg)
python在线编译器的简单原理及简单实现代码
2022-01-06 18:40:49
![](https://img.aspxhome.com/file/2023/7/87747_0s.png)
Pytorch 抽取vgg各层并进行定制化处理的方法
2023-01-28 16:30:15
Python与人工神经网络:使用神经网络识别手写图像介绍
2022-05-23 03:11:36
![](https://img.aspxhome.com/file/2023/7/86667_0s.png)
Django QuerySet查询集原理及代码实例
2023-04-06 01:30:18
Python face_recognition实现AI识别图片中的人物
2023-08-14 21:34:35
![](https://img.aspxhome.com/file/2023/7/79807_0s.png)
python基于openpyxl生成excel文件
2022-08-03 03:10:47
![](https://img.aspxhome.com/file/2023/4/82684_0s.png)
通过作业调度建立SQL Server的自动备份
2008-12-09 14:58:00
js常用排序实现代码
2023-08-30 08:50:24
Python pickle类库介绍(对象序列化和反序列化)
2021-12-31 00:28:33
python异常处理try的实例小结
2022-01-25 06:06:51
Python基于keras训练实现微笑识别的示例详解
2022-06-14 10:02:07
![](https://img.aspxhome.com/file/2023/3/77613_0s.jpg)
如何更快更好地调试ASP程序代码?
2009-11-23 20:13:00