Python操作符-快速查询
Python的运算符允许我们对变量进行常见的处理。我们将通过示例讨论不同类型的运算符以及运算符的优先级。它们是可以操纵一个或多个操作数值的特殊符号。
Python操作符列表
Python的操作符可以分为几个类别。
- Assignment Operators
- Arithmetic Operators
- Logical Operators
- Comparison Operators
- Bitwise Operators
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) |
使用赋值运算符
# take two variable, assign values with assignment operators
a=3
b=4
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a+b
a+=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a**b ( exponent operator)
a**=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a//b ( floor division)
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 |
#create two variables
a=100
b=200
# addition (+) operator
print(a+b)
# subtraction (-) operator
print(a-b)
# multiplication (*) operator
print(a*b)
# division (/) operator
print(b/a)
# modulus (%) operator
print(a%b) # prints the remainder of a/b
# exponent (**) operator
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)
Python的位运算符
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)
Python 逻辑运算符
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 |
#take user input as int
a=int(input())
# logical AND operation
if a%4==0 and a%3==0:
print("divided by both 4 and 3")
# logical OR operation
if a%4==0 or a%3==0:
print("either divided by 4 or 3")
# logical NOT operation
if not(a%4==0 or a%3==0):
print("neither divided by 4 nor 3")
Python运算符优先级
这些运算符的优先级指的是运算符的优先级水平。当一个表达式中包含多个运算符时,这就变得至关重要。例如,考虑下面的表达式:
>>> 2+3*4
现在,你认为操作序列应该是什么?我们可以先加2和3,然后将结果乘以4。另外,我们也可以先将3和4相乘,然后再与2相加。从这里我们可以看出操作符的优先级是很重要的。
以下是一份运算符优先级的列表,按降序排列。这意味着上层组具有比下层组更高的优先级。
-
- 括号 – ()
-
- 指数运算 – **
-
- 补数、一元正负 – ~、+、-
-
- 乘法、除法、取模 – *、/、%
-
- 加法和减法 – +、-
-
- 右移和左移 – >>、<< 位与运算 – & 位或和异或运算 – |、^ 比较运算符 – ==、!=、>、<、>=、<=
- 赋值运算符 – =