Python 中的就地運算符 |設置1(iadd(),isub(),iconcat()…)

Python 在其定義中提供了執行操作的方法,即 使用 運算符 模塊在一個表達式中執行賦值和求值。比如

 x + = y 等價於 x = operator.iadd(x, y) 

一些重要的就地操作

1. iadd() : —此函數用於分配和添加當前值。該操作執行操作“ a + = b ”。 不是 賦值對於字符串、數字和元組等不可變容器是正確的。

2.iconcat() : —此函數用於在一秒結束時 連接 一行。

# Python 代碼來演示它是如何工作的
# iadd() 和 iconcat()


# import 語句處理語句操作

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拼接後的字符串為:pythonengineering 

3. isub() : —該函數用於分配和減去當前值。該操作執行“ a- = b ”操作。 不是 賦值對於字符串、數字和元組等不可變容器是正確的。

4. imul() : —該函數用於分配和乘以當前值。該操作執行操作“ a * = b ”。 不是 賦值對於字符串、數字和元組等不可變容器是正確的。

# Python 代碼來演示它是如何工作的
# isub () 和 imul ()


#import語句處理語句操作

import operator


#使用isub()進行減法賦值

x = operator.isub ( 2 , 3 );


# 打印改變的值

print ( "減賦值後的值:" , end = "")

print (x)


# 使用imul()進行乘法賦值

x = operator.imul ( 2 , 3 );


# 打印改變的值

print ( "相乘賦值後的值:" , end = "")

print (x)

輸出:< /p>

 減法運算後的值:-1 乘法運算後的值:6 

5. itruediv() : —該函數用於對當前值進行賦值和除法。該操作執行“ a / = b ”操作。 不是 賦值對於字符串、數字和元組等不可變容器是正確的。

6. imod() : —該函數用於分配和返回餘數。該操作執行操作“ a% = b ”。 不是 賦值對於字符串、數字和元組等不可變容器是正確的。

# Python 代碼來演示它是如何工作的
# itruediv() 和 imod()


#import語句處理語句操作

import operator


#使用itruediv()進行除法賦值

x = operator.itruediv ( 10 , 5 );


# 打印改變的值

print ( "分割賦值後的值:" , end = "")

print (x)


# 對模塊使用 imod() 並賦值

x = operator.imod ( 10 , 6 );


# 打印改變的值

print ( "取模賦值後的值:" , end = "")

print (x)

輸出:< /p>

 除法後的值:2.0 取模後的值:4 

後續文章

本文由 Manjit Singh ... 如果你是 Python.Engineering 並且想貢獻,你也可以使用 貢獻.python.engineering 或通過發布文章貢獻@python.engineering。在 Python.Engineering 主頁上查看我的文章並幫助其他極客。

如果您發現任何錯誤或想要分享有關上述主題的更多信息,請發表評論。

我們希望本文能幫助您解決問題。 除了 Python 中的就地運算符 |設置1(iadd(),isub(),iconcat()…),查看其他與 Python functions 相關的主題。

想在 Python 中出類拔萃? 查看我們對 最佳 Python 在線課程 2023的評論。 如果您對數據科學感興趣,請查看如何學習 R 編程

順便說一句,此材料也有其他語言版本:



Schneider Wu

Paris | 2023-03-23

简单地说,很清楚。谢谢你的分享。Python 中的就地運算符 |設置1(iadd(),isub(),iconcat()…)和其他问题的Python functions一直是我的弱点😁。. 將在我的學士論文中使用它

Marie Williams

New York | 2023-03-23

我正在为我的编码面试做准备,谢谢你的澄清--Python中的Python 中的就地運算符 |設置1(iadd(),isub(),iconcat()…)并不是最简单的一个。. 昨天檢查了,有效!

Frank Schteiner

Abu Dhabi | 2023-03-23

谢谢你的解释! 我被Python 中的就地運算符 |設置1(iadd(),isub(),iconcat()…)卡住了几个小时。了几个小时,最后终于完成了 🤗. 將在我的學士論文中使用它

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically