網站首頁 編程語言 正文
Python 在其定義中提供了執行就地操作的方法,即使用“?operator?”模塊在單個語句中進行賦值和計算。 例如,
x += y is equivalent to x = operator.iadd(x, y)
一些重要的就地操作:
1. iadd() ?:- 該函數用于分配和添加當前值。該操作執行“?a+=b?”操作。在不可變容器(例如字符串、數字和元組)的情況下不執行分配。
2. iconcat() ?:- 該函數用于在第二個末尾連接一個字符串 。
# 演示 iadd() 和 iconcat() 工作的 Python 代碼
# importing operator to handle operator operations
import operator
# 使用 iadd() 添加和賦值
x = operator.iadd(2, 3);
# 打印修改后的值
print ("添加賦值后的值為:", end="")
print (x)
# 初始化值
y = "geeks"
z = "forgeeks"
# 使用 iconcat() 連接序列
y = operator.iconcat(y, z)
# 使用 iconcat() 連接序列
print ("拼接后的字符串為:", end="")
print (y)
輸出:
添加賦值后的值為:5
拼接后的字符串為:geeksforgeeks
3. isub() ?:- 該函數用于分配和減去當前值。該操作執行“?a-=b?”操作。在不可變容器(例如字符串、數字和元組)的情況下不執行分配。
4. imul() ?:- 該函數用于分配和乘以當前值。該操作執行“?a=b*?”操作。在不可變容器(例如字符串、數字和元組)的情況下不執行分配。
# 演示 isub() 和 imul() 工作的 Python 代碼
# importing operator to handle operator operations
import operator
# 使用 isub() 減去和賦值
x = operator.isub(2, 3);
# 打印修改后的值
print ("減法運算后的值:", end="")
print (x)
# 使用 imul() 進行乘法和賦值
x = operator.imul(2, 3);
# 打印修改后的值
print ("乘法運算后的值:", end="")
print (x)
輸出:
減法運算后的值:-1?
乘法運算后的值:6
5. itruediv() ?:- 該函數用于對當前值進行賦值和除法。此操作執行“?a/=b?”操作。在不可變容器(例如字符串、數字和元組)的情況下不執行分配。
6. imod() ?:- 該函數用于分配和返回余數。該操作執行“?a%=b?”操作。在不可變容器(例如字符串、數字和元組)的情況下不執行分配。
# 演示 itruediv() 和 imod() 工作的 Python 代碼
# importing operator to handle operator operations
import operator
# 使用 itruediv() 進行除法賦值
x = operator.itruediv(10, 5);
# 打印修改后的值
print ("除法賦值后的值:", end="")
print (x)
# 使用 imod() 取模并賦值
x = operator.imod(10, 6);
# 打印修改后的值
print ("取模賦值后的值:", end="")
print (x)
輸出:
除法賦值后的值:2.0
取模賦值后的值:4
原文鏈接:https://juejin.cn/post/7143436032090996749
相關推薦
- 2023-08-13 基于TP5框架的excel導入功能
- 2022-08-26 詳解Python中元組的三個不常用特性_python
- 2022-05-12 Android10 分享微信提示獲取資源失敗
- 2022-04-27 bash?shell獲取當前腳本的絕對路徑(pwd/readlink)_linux shell
- 2022-04-19 前端開發中幾種存儲方式詳解
- 2022-04-30 DataGridView凍結列或行、列順序調整、操作行頭列頭標題的方法_C#教程
- 2023-01-18 fastadmin使用學習中的常見問題匯總_其它CMS
- 2022-07-06 Qt之使用GraphicsView框架實現思維導圖的示例_C 語言
- 最近更新
-
- 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同步修改后的遠程分支