Python 運算符 – 快速參考

Python 運算符允許我們對變量進行常見處理。我們將研究不同類型的運算符,並附上示例,還會討論運算符的優先順序。它們是特殊符號,可以操作一個或多個運算元的值。

Python 運算符列表

Python 運算符可以分為幾個類別。

  • 賦值運算符
  • 算術運算符
  • 邏輯運算符
  • 比較運算符
  • 位運算符

Python 賦值運算符

賦值運算符包括基本的等號(=)賦值運算符。

為了簡化代碼,減少冗余,Python 還包括算術賦值運算符。

其中包括 Python 中的 += 運算符 用於加法賦值,//= 地板除法賦值運算符,等等。

以下是 Python 中所有算術賦值運算符的列表。

Operator Description
+= a+=b is equivalent to a=a+b
*= a*=b is equivalent to a=a*b
/= a/=b is equivalent to a=a/b
%= a%=b is equivalent to a=a%b
**= a**=b is equivalent to a=a**b (exponent operator)
//= a//=b is equivalent to a=a//b (floor division)

使用賦值運算符


# 用賦值運算符賦值給兩個變量
a=3
b=4

print("a: "+str(a))
print("b: "+str(b))

# 相當於 a=a+b
a+=b

print("a: "+str(a))
print("b: "+str(b))

# 相當於 a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))

# 相當於 a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))

# 相當於 a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))

# 相當於 a=a**b(指數運算符)
a**=b
print("a: "+str(a))
print("b: "+str(b))

# 相當於 a=a//b(地板除法)
a//=b
print("a: "+str(a))
print("b: "+str(b))

Python算術運算符

Operator Description Example
+ used to add two numbers sum = a + b
used for subtraction difference = a – b
* used to multiply two numbers. If a string and int is multiplied then the string is repeated the int times. mul = a*b>>> “Hi”*5
‘HiHiHiHiHi’
/ used to divide two numbers div = b/a
% modulus operator, returns the remainder of division mod = a%b
** exponent operator

# 創建兩個變量
a=100
b=200

# 加法(+)運算符
print(a+b) 

# 減法(-)運算符
print(a-b) 

# 乘法(*)運算符
print(a*b)

# 除法(/)運算符
print(b/a)

# 餘數(%)運算符
print(a%b) # prints the remainder of a/b

# 指數(**)運算符
print(a**b) #prints a^b

Python比較運算符

Operator Description Example
== returns True if two operands are equal, otherwise False. flag = a == b
!= returns True if two operands are not equal, otherwise False. flag = a != b
> returns True if left operand is greater than the right operand, otherwise False. flag = a > b
< returns True if left operand is smaller than the right operand, otherwise False. flag = a < b
>= returns True if left operand is greater than or equal to the right operand, otherwise False. flag = a > b
<= returns True if left operand is smaller than or equal to the right operand, otherwise False. flag = a < b

# create two variables
a=100
b=200

# (==) operator, checks if two operands are equal or not
print(a==b)

# (!=) operator, checks if two operands are not equal
print(a!=b)

# (>) operator, checks left operand is greater than right operand or not
print(a>b)

# (<) operator, checks left operand is less than right operand or not
print(a<b)
#(>=) operator, checks left operand is greater than or equal to right operand or not
print(a>=b)

# (<=) operator, checks left operand is less than or equal to right operand or not
print(a<=b)

# 創建兩個變量

Operator Description Example
& Binary AND Operator x = 10 & 7 = 2
Binary OR Operator
^ Binary XOR Operator x = 10 ^ 7 = 13
~ Binary ONEs Compliment Operator x = ~10 = -11
<< Binary Left Shift operator x = 10<<1 = 20
>> Binary Right Shift Operator x = 10>>1 = 5

#create two variables
a=10 # binary 1010
b=7  # binary 0111

# Binary AND (&) operator, done binary AND operation
print(a&b)

# Binary OR (|) operator, done binary OR operation
print(a|b)

# Binary XOR (^) operator, done binary XOR operation
print(a^b)

# Binary ONEs Compliment (~) operator, done binary One's Compliment operation
print(~a)

# Binary Left Shift (<<) operator, done binary Left Shift operation
print(a<<1) 
# Binary Right Shift (>>) operator, done binary Right Shift operation
print(a>>1)

# (==) 運算符,檢查兩個操作數是否相等

Operator Description Example
and Logical AND Operator flag = exp1 and exp2
or Logical OR Operator flag = exp1 or exp2
not Logical NOT Operator flag = not(True) = False

# (!=) 運算符,檢查兩個操作數是否不相等
a=int(input())

# (>) 運算符,檢查左操作數是否大於右操作數

if a%4==0 and a%3==0:
    print("divided by both 4 and 3")

# (<) 運算符, 檢查  操作數 是否 小於  操作數  
if a%4==0 or a%3==0:
    print("either divided by 4 or 3")

# (<=) 運算符,檢查左操作數是否小於或等於右操作數
if not(a%4==0 or a%3==0):
    print("neither divided by 4 nor 3")

Python 位運算符

#创建两个变量


>>> 2+3*4

# 二进制与(&)运算符,完成二进制与操作

# 二进制或(|)运算符,完成二进制或操作

  1. # 二进制异或(^)运算符,完成二进制异或操作
  2. # 二进制取反(~)运算符,完成二进制取反操作
  3. # 二进制左移(<< 运算符,完成 二进制 操作
  4. Python逻辑运算符
  5. #将用户输入作为整数
  6. # 逻辑与操作
  7. # 逻辑或操作
  8. # 逻辑非操作
  9. Python运算符优先级
  10. 这些运算符的优先级表示运算符的优先级水平。当表达式中有多个运算符时,这变得至关重要。例如,请考虑以下表达式:

Source:
https://www.digitalocean.com/community/tutorials/python-operators