Python运算符
时间:2020-02-23 14:43:06 来源:igfitidea点击:
Python运算符允许我们对变量进行通用处理。
我们将通过示例和运算符优先级研究不同类型的python运算符。
Python运算符
Python运算符是可以操纵一个或者多个操作数的值的特殊符号。
Python运算符类型
Python运算符可分为几类。
- 算术运算符
- 逻辑运算符
- 比较运算符
- 按位运算符
- 赋值运算符
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算术运算符
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比较运算符
Python按位运算符
| Operator | Description | Example |
|---|---|---|
| & | Binary AND Operator | x = 10 & 7 = 2 |
| | | Binary OR Operator | x = 10 |
| ^ | 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赋值运算符
| 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运算符优先级
python运算符的优先级是指运算符的优先级。
当表达式中包含多个运算符时,这一点至关重要。
例如,考虑以下表达式:
>>> 2+3*4
现在,您认为这一系列操作是什么?我们可以将2和3相加,然后将结果乘以4。
此外,我们可以先将3和4相乘,然后再加上2。
其中我们可以看到运营商的优先级很重要。
以下是指示优先级的运算符列表。
按降序排列。
这意味着上层人群比下层人群具有更高的优先级。
- 括号–
() - 求幂–
** - 恭维,一元加减–"~"," +","-"
- 乘,除,模–" *"," /","%"
- 加减法-
+,- - 左右移位–
>>, << - 按位AND –
& - 按位OR和XOR –
|,^ - 比较运算符–
==,!=,>,<,> =, <= - 赋值运算符-=

