# 運算子

>>> '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
• 位 元運算：如&、|、~、^、>>、<<
• 指 定運算：如+=、-=、*=、/=、%=、&=、|=、>>=、<<=...

>>> 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
3
>>> 10 % 3
1
>>> (10).__mod__(3)
1
>>>

>>> 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]
>>>

>>> passwd = ''
>>> if not passwd:
...
>>>

and、or有捷徑運算，and左運算元若判定為假，則就不會繼續運算右運算元，or則是左運算元判斷為真，則不的運算右運算元。當判斷確認時停在哪個運算元，就會傳回該運算元，例如：
>>> [] and 'Justin'
[]
>>> [1, 2] and 'Justin'
'Justin'
>>> [] or 'Justin'
'Justin'
>>> [1, 2] or 'Justin'
[1, 2]
>>>

>>> if [] and 'Justin':
...     print('True')
... else:
...     print('False')
...
False
>>> if [1, 2] and 'Justin':
...     print('True')
... else:
...     print('False')
...
True
>>>