Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

In-place Operators ใน Python (operator module)


In-place operation เปลี่ยนค่าตรงในตัวแปรโดยไม่สร้าง copy ใช้โมดูล operator

ฟังก์ชันเทียบเท่าคำอธิบาย
iadd(a,b)a += bบวกและกำหนด
isub(a,b)a -= bลบและกำหนด
imul(a,b)a *= bคูณและกำหนด
itruediv(a,b)a /= bหารและกำหนด
imod(a,b)a %= bmod และกำหนด
iconcat(a,b)a + bconcat sequence

ตัวอย่าง

import operator
print(operator.iadd(1,3))    # 4
print(operator.isub(8,6))    # 2
print(operator.imul(8,6))    # 48
print(operator.itruediv(54,6)) # 9.0
print(operator.imod(10,5))   # 0
print(operator.iconcat("jupyter", " notebook")) # jupyter notebook