Skip to content

Latest commit

 

History

History
65 lines (51 loc) · 1.6 KB

File metadata and controls

65 lines (51 loc) · 1.6 KB
description 夫未戰而廟算勝者,得算多也;未戰而廟算不勝者,得算少也。多算勝,少算不勝,而況於無算乎?【始計篇】

2.2 Operators

「運算子就是把兩個運算元連接在一起的東西」by 03t

「表示我們要做事的東西」by Sean

Arithmetic Operators

Operator Description Example
+ a 加 b 3 + 2
- a 減 b 3 - 2
* a 乘 b 3 * 2
/ b 除 a 3 / 2
% a 模 b 3 % 2
** a 的 b 次方 3 ** 2
// b 除 a 的商 3 // 2

Comparison Operators

Operator Description Example
== 等於 3 == 5
!= 不等於 3 != 5
> 大於 3 > 5
< 小於 3 < 5
>= 不小於 3 >= 5
<= 不大於 3 <= 5

Logical Operators

Operator Description Example
and 3 >= 2 and 3 < 6
or 3 < 2 or 3 > 1
not not a == b

Assignment Operators

Operator Example Same as
= x = 1 x = 1
+= x += 1 x = x + 1
-= x -= 1 x = x - 1
*= x *= 2 x = x * 2
/= x /= 2 x = x / 2
%= x %= 2 x = x % 2
**= x **= 2 x = x ** 2
//= x //= 2 x = x // 2

📎 Bitwise Operators

Operator Name Example
& AND 3 & 5
| OR `3
^ XOR 3 ^ 5
~ NOT ~0
<< Zero fill left shift 3 << 1
>> Signed right shift 3 >> 1