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