網站首頁 編程語言 正文
Python 在“運算符”模塊下為許多數學、邏輯、關系、按位等操作預定義了函數。本文介紹了一些基本功能。
1. add(a, b) ?:- 這個函數返回給定參數的加法。
操作 -?a + b。
2. sub(a, b) ?:- 此函數返回給定參數的差異。
操作 -?a - b。
3. mul(a, b) ?:- 這個函數返回給定參數的乘積。
操作 -?a * b。
# 演示 add()、sub()、mul() 工作的 Python 代碼
# importing operator module
import operator
# 初始化變量
a = 4
b = 3
# 使用 add() 將兩個數字相加
print ("The addition of numbers is :",end="");
print (operator.add(a, b))
# 使用 sub() 減去兩個數字
print ("The difference of numbers is :",end="");
print (operator.sub(a, b))
# 使用 mul() 將兩個數字相乘
print ("The product of numbers is :",end="");
print (operator.mul(a, b))
輸出:
The addition of numbers is:7
The difference of numbers is :1
The product of numbers is:12
4. truediv(a,b) ?:- 這個函數返回給定參數的除法。
操作 -?a / b。
5. floordiv(a,b) ?:- 此函數還返回給定參數的除法。但該值是下限值,即返回最大的小整數。
操作 –?a // b。
6. pow(a,b) ?:- 這個函數返回給定參數的冪。
操作 –?a ** b.
7. mod(a,b) ?:- 這個函數返回給定參數的模數。操作 –?a % b.
# 演示 truediv()、floordiv()、pow()、mod() 工作的 Python 代碼
# importing operator module
import operator
# 初始化變量
a = 5
b = 2
# 使用 truediv() 將兩個數字相除
print ("The true division of numbers is : ",end="");
print (operator.truediv(a,b))
# 使用 floordiv() 將兩個數字相除
print ("The floor division of numbers is : ",end="");
print (operator.floordiv(a,b))
# 使用 pow() 對兩個數字求冪
print ("The exponentiation of numbers is : ",end="");
print (operator.pow(a,b))
# 使用 mod() 取兩個數的模
print ("The modulus of numbers is : ",end="");
print (operator.mod(a,b))
輸出:
The true division of numbers is: 2.5
The floor division of numbers is: 2
The exponentiation of numbers is: 25
The modulus of numbers is: 1
8. lt(a, b) ?:- 此函數用于檢查 a 是否小于 b。如果 a 小于 b,則返回 true,否則返回 false。
操作 -?a < b。
9. le(a, b) ?:- 此函數用于檢查 a 是否小于或等于 b。如果 a 小于或等于 b,則返回 true,否則返回 false。
操作 -?a <= b。
10. eq(a, b) ?:- 此函數用于檢查 a 是否等于 b。如果 a 等于 b,則返回 true,否則返回 false。
操作 -?a == b。
# 演示 lt()、le() 和 eq() 工作的 Python 代碼
# importing operator module
import operator
# 初始化變量
a = 3
b = 3
# 使用 lt() 檢查 a 是否小于 b
if(operator.lt(a,b)):
print ("3 is less than 3")
else : print ("3 is not less than 3")
# 使用 le() 檢查 a 是否小于或等于 b
if(operator.le(a,b)):
print ("3 is less than or equal to 3")
else : print ("3 is not less than or equal to 3")
# 使用 eq() 檢查 a 是否等于 b
if (operator.eq(a,b)):
print ("3 is equal to 3")
else : print ("3 is not equal to 3")
輸出:
3 is not less than 3
3 is less than or equal to 3
3 is equal to 3
11. gt(a,b) ?:- 此函數用于檢查 a 是否大于 b。如果 a 大于 b,則返回 true,否則返回 false。
操作 -?a > b。
12. ge(a,b) ?:- 此函數用于檢查 a 是否大于或等于 b。如果 a 大于或等于 b,則返回 true,否則返回 false。
操作 -?a >= b。
13. ne(a,b) ?:- 此函數用于檢查 a 是否不等于 b 或是否相等。如果 a 不等于 b,則返回 true,否則返回 false。
操作 -?a != b。
# 演示 gt()、ge() 和 ne() 工作的 Python 代碼
# importing operator module
import operator
# 初始化變量
a = 4
b = 3
# 使用 gt() 檢查 a 是否大于 b
if (operator.gt(a,b)):
print ("4 is greater than 3")
else : print ("4 is not greater than 3")
# 使用 ge() 檢查 a 是否大于或等于 b
if (operator.ge(a,b)):
print ("4 is greater than or equal to 3")
else : print ("4 is not greater than or equal to 3")
# 使用 ne() 檢查 a 是否不等于 b
if (operator.ne(a,b)):
print ("4 is not equal to 3")
else : print ("4 is equal to 3")
輸出:
4 is greater than 3
4 is greater than or equal to 3
4 is not equal to 3
原文鏈接:https://juejin.cn/post/7143141248893714469
相關推薦
- 2023-11-21 MAC 打開不開第三方應用XXX can’t be opened because the ident
- 2022-08-03 python+html實現前后端數據交互界面顯示的全過程_python
- 2022-06-10 FreeRTOS實時操作系統之可視化追蹤調試_操作系統
- 2022-09-09 C#正則表達式與HashTable詳解_C#教程
- 2022-11-22 python模塊導入方式淺析步驟_python
- 2022-09-18 Pycharm快速安裝OpenCV的詳細操作步驟_python
- 2022-11-20 Go項目實現優雅關機與平滑重啟功能_Golang
- 2022-10-19 react創建項目啟動報錯的完美解決方法_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支