Python基礎概念知識站:原地運算符

Python基礎概念知識站:原地運算符


Python基礎概念知識站登場拉~有興趣的人可多加利用喔~ (九)

原地運算符In-place Operators

Python 中的原地運算符 (In-place operators) 可以用來精簡代碼,例如「x=x+3」就可以精簡為「x+=3」。加減乘除都可以用。
>>> x = 70
>>> print(x)
70
>>> x += 7
>>> print(x)
77
>>> x = 70
>>> print(x)
70
>>> x *= 7
>>> print(x)
490
Python 中的原地運算符 (In-place operators) 除了用在數值外,也可以用在字串。
>>> x = "cat"
>>> print(x)
cat

>>> x += "meow"
>>> print(x)
catmeow
>>> x = "cat"
>>> print(x)
cat

>>> x *=3
>>> print(x)
catcatcat

對Python有興趣的同學也可以看看部落格裡其他相關文章喔~



其他閱讀








Python課程讓你免煩惱AI智慧的8億職缺危機 還能輕鬆領高薪