Python操作符允许我们对变量进行公共处理。我们将通过示例和操作符优先级来研究不同类型的python操作符。
Python运算符
Python运算符是可以操作一个或多个操作数的值的特殊符号。
Python运算符类型
Python操作符可以分为几个类别。
- 算术运算符
- 逻辑运算符
- 比较运算符
- 位运算符
- 赋值运算符
Python算术运算符
操作员 | 说明 | 例子 |
---|---|---|
+ | 用来加两个数 | 总和=a+b |
–个; | 用于减法 | 差异=a和#8211;b |
* | 用来乘以两个数。如果字符串与int相乘,则字符串将重复int次。 | mul=a*b >;>;“;嗨”;*5‘;嗨嗨嗨’; |
/ | 用来除两个数的 | div=b/a公司 |
% | 模运算符,返回除法的余数 | 模式=a%b |
** | 指数运算符 |
#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算术运算符
Python比较运算符
操作员 | 说明 | 例子 |
---|---|---|
== | 如果两个操作数相等,则返回True,否则返回False。 | 标志=a==b |
!= | 如果两个操作数不相等,则返回True,否则返回False。 | 旗子=a!=b |
>; | 如果左操作数大于右操作数,则返回True,否则返回False。 | 标志=a>;b |
<; | 如果左操作数小于右操作数,则返回True,否则返回False。 | 标志=a<;b |
>= | 如果左操作数大于或等于右操作数,则返回True,否则返回False。 | 标志=a>;b |
<= | 如果左操作数小于或等于右操作数,则返回True,否则返回False。 | 标志=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比较运算符
Python位运算符
操作员 | 说明 | 例子 |
---|---|---|
&; | 二进制与运算符 | x=10和#038;7=2 |
| | 二进制或运算符 | x=10 | 7=15 |
^ | 二进制异或运算符 | x=10^7=13 |
~ | 二元一补运算符 | x=~10=-11 |
<;<; | 二进制左移位算子 | x=10<;<;1=20 |
>;>; | 二元右移算子 | 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位运算符
Python逻辑运算符
操作员 | 说明 | 例子 |
---|---|---|
和 | 逻辑与运算符 | 标志=exp1和exp2 |
或 | 逻辑或运算符 | 标志=exp1或exp2 |
不 | 逻辑非运算符 | 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逻辑运算符
Python赋值运算符
操作员 | 说明 |
---|---|
+= | a+=b相当于a=a+b |
*= | a*=b相当于a=a*b |
/= | a/=b相当于a=a/b |
%= | a%=b相当于a=a%b |
**= | a**=b相当于a=a**b(指数运算符) |
//= | a//=b相当于a=a//b(楼层划分) |
# 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赋值运算符
Python运算符优先级
python运算符的优先级表示运算符的优先级。当一个表达式中有多个运算符时,这一点就变得至关重要了。例如,考虑以下表达式:
>>> 2+3*4
现在,你认为这一系列的手术会是什么?我们可以加上2和3,然后把结果乘以4。另外,我们可以先把3和4相乘,然后再加上2。在这里我们可以看到运算符的优先级很重要。
下面是表示优先级的运算符列表。它是按降序排列的。这意味着上一组的优先级高于上一组。
- 括号–;
()
- 指数化;
**
- 恭维,一元加减–;
~
,+
,-
- 乘、除、模和#8211;
*
,/
,%
- 加减法–;
+
,-
- 左右移动–;
>>
,<<
- 按位与–;
&
- 按位“或”与“异或”–;
|
,^
- 比较运算符–;
==
,!=
,>
,<
,>=
,<=
- 赋值运算符-
=
您可以从我们的GitHub存储库.
参考文献:Python官方文档