| >>> 'Just' + 'in' 'Justin' >>> [1, 2] + [3, 4] [1, 2, 3, 4] >>> (1, 2) + (3, 4) (1, 2, 3, 4) >>> |
*在Python上,則通常可以作加倍的動作,例如:
| >>> 'Justin' * 4 'JustinJustinJustinJustin' >>> [1, 2] * 4 [1, 2, 1, 2, 1, 2, 1, 2] >>> (1, 2) * 4 (1, 2, 1, 2, 1, 2, 1, 2) >>> |
如果要作數值次方運算,則使用**。例如:
| >>> 9 ** 2 81 >>> 9 ** 3 729 >>> 9 ** 0.5 3.0 >>> |
在數值運算上,還有個//運算子,這是用來取除法的商值,而%則用來取除法的餘數。例如:
| >>> 10 // 3 3 >>> 10 % 3 1 >>> |
Python還提供了關係運算(Rational operation)、邏輯運算(Logical operation)、位元運算(Bitwise operation)等對物件的運算子:
- 關係運算:如>、>=、<、<=、==、!=
- 邏輯運算:如and、or、not
- 位 元運算:如&、|、~、^、>>、<<
- 指 定運算:如+=、-=、*=、/=、%=、&=、|=、~=、>>=、<<=...
不僅僅是數值可以使用其它的運算子,在適當的場合,特定的物件也可以使用這些運算子,例如集合(Set)可以使用&、|、^來作交集、聯集等設定,可以參考 集合 的說明。
在現階段,你可以先知道的是,運算子作用於物件的動作,其實是可以透過定義特殊方法(Method)來決定該如何進行處理。例如+可以定義__add__()方法,而%可以定義__mod__()方法:
| >>> dir(1) ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delatt r__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__ ', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__ha sh__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__po s__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ro r__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtrue div__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subcla sshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'de nominator', 'imag', 'numerator', 'real'] >>> 1 + 2 3 >>> (1).__add__(2) 3 >>> 10 % 3 1 >>> (10).__mod__(3) 1 >>> |
就算是[]索引運算,也是可以透過對應的__getitem__()、__setitem()__來定義:
| >>> dir([]) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__doc__' , '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__' , '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__re pr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', ' __str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'p op', 'remove', 'reverse', 'sort'] >>> x = [1, 2, 3] >>> x[1] 2 >>> x.__getitem__(1) 2 >>> x.__getitem__(2) 3 >>> x[2] 3 >>> x[0] = 10 >>> x.__setitem__(1, 20) >>> x [10, 20, 3] >>> |
初學目前僅需了解到這邊,在了解如何自定義類別之後,將會再介紹如何定義這些特定的方法。
在使用邏輯運算and、or、not時,要注意到,在Python中,數字不是0就是True,而所有物件不是「空」就是True,例如字串'Justin'在判斷式中會是 True,而空字串''在判斷式中會是False,所以空串列是False、空字典是False、空Tuple是False,而None也是False。例如:
| >>> passwd = '' >>> if not passwd: ... print('Password Required') ... Password Required >>> |
and、or有捷徑運算,and左運算元若判定為假,則就不會繼續運算右運算元,or則是左運算元判斷為真,則不的運算右運算元。當判斷確認時停在哪個運算元,就會傳回該運算元,例如:
| >>> [] and 'Justin' [] >>> [1, 2] and 'Justin' 'Justin' >>> [] or 'Justin' 'Justin' >>> [1, 2] or 'Justin' [1, 2] >>> |
如果是在判斷式中,同樣的,and、or傳回的運算元再進行True、False的結果判斷,即數字不是0就是True,而所有物件不是「空」就是True。例如:
| >>> if [] and 'Justin': ... print('True') ... else: ... print('False') ... False >>> if [1, 2] and 'Justin': ... print('True') ... else: ... print('False') ... True >>> |
沒有留言:
張貼留言