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

Double Star Operator (**) ใน Python หมายถึงอะไร?

สำหรับ Numeric data types Double asterisk (**) ถูกกำหนดเป็น Exponentiation operator (ตัวดำเนินการยกกำลัง)

>> a = 10; b = 2
>>> a**b
100
>>> a = 1.5; b = 2.5
>>> a**b
2.7556759606310752
>>> a = 3+2j
>>> b = 3+5j
>>> a**b
(-0.7851059645317211+2.350232331971346j)

ใน Function Definition Argument ที่มี Double asterisks เป็น Prefix ช่วยส่ง Multiple Keyword Arguments ให้จาก Calling Environment

>> def function(**arg):
...     for i in arg:
...         print(i, arg[i])
>>> function(a=1, b=2, c=3, d=4)
a 1
b 2
c 3
d 4