Python运算符
Python运算符通常用于对值和变量执行操作。这些是用于逻辑和算术运算的标准符号。在本文中,我们将研究不同类型的Python运算符。
算术运算符
算术运算运算符用于执行数学运算,例如加法、减法、乘法和除法。Operator Description Syntax + Addition: adds two operands x + y – Subtraction: subtracts two operands x – y * Multiplication: multiplies two operands x * y / Division (float): divides the first operand by the second x / y // Division (floor): divides the first operand by the second x // y % Modulus: returns the remainder when the first operand is divided by the second x % y ** Power: Returns first raised to power second x ** y
示例: Python中的算术运算符
Python3
# Examples of Arithmetic Operator
a = 9
b = 4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Modulo of both number
mod = a % b
# Power
p = a ** b
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
Python3
# Examples of Relational Operators
a = 13
b = 33
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Python3
# Examples of Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)
Python3
# Examples of Bitwise operators
a = 10
b = 4
# Print bitwise AND operation
print(a & b)
# Print bitwise OR operation
print(a | b)
# Print bitwise NOT operation
print(~a)
# print bitwise XOR operation
print(a ^ b)
# print bitwise right shift operation
print(a >> 2)
# print bitwise left shift operation
print(a << 2)
Python3
# Examples of Assignment Operators
a = 10
# Assign value
b = a
print(b)
# Add and assign value
b += a
print(b)
# Subtract and assign value
b -= a
print(b)
# multiply and assign
b *= a
print(b)
# bitwise lishift operator
b <<= a
print(b)
Python3
a = 10
b = 20
c = a
print(a is not b)
print(a is c)
Python3
# Python program to illustrate
# not 'in' operator
x = 24
y = 20
list = [10, 20, 30, 40, 50]
if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
Python3
# Examples of Operator Precedence
# Precedence of '+' & '*'
expr = 10 + 20 * 30
print(expr)
# Precedence of 'or' & 'and'
name = "Alex"
age = 0
if name == "Alex" or name == "John" and age >= 2:
print("Hello! Welcome.")
else:
print("Good Bye!!")
Python3
# Examples of Operator Associativity
# Left-right associativity
# 100 / 10 * 10 is calculated as
# (100 / 10) * 10 and not
# as 100 / (10 * 10)
print(100 / 10 * 10)
# Left-right associativity
# 5 - 2 + 3 is calculated as
# (5 - 2) + 3 and not
# as 5 - (2 + 3)
print(5 - 2 + 3)
# left-right associativity
print(5 - (2 + 3))
# right-left associativity
# 2 ** 3 ** 2 is calculated as
# 2 ** (3 ** 2) and not
# as (2 ** 3) ** 2
print(2 ** 3 ** 2)
13
5
36
2.25
2
1
6561
注意:有关这两个运算符的一些有趣事实,请参阅 / 和 // 之间的差异。
比较运算符
关系运算符的比较比较值。它根据条件返回True或False 。
Operator | Description | Syntax |
---|---|---|
> | Greater than: True if the left operand is greater than the right | x > y |
< | Less than: True if the left operand is less than the right | x < y |
== | Equal to: True if both operands are equal | x == y |
!= | Not equal to – True if operands are not equal | x != y |
>= | Greater than or equal to True if the left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to True if the left operand is less than or equal to the right | x <= y |
示例: Python中的比较运算符
Python3
# Examples of Relational Operators
a = 13
b = 33
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
False
True
False
True
False
True
逻辑运算符
逻辑运算符执行逻辑与、逻辑或和逻辑非运算。它用于组合条件语句。Operator Description Syntax and Logical AND: True if both the operands are true x and y or Logical OR: True if either of the operands is true x or y not Logical NOT: True if the operand is false not x
示例: Python中的逻辑运算符
Python3
# Examples of Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)
False
True
False
位运算符
位运算符作用于位并执行逐位操作。这些用于对二进制数进行操作。Operator Description Syntax & Bitwise AND x & y | Bitwise OR x | y ~ Bitwise NOT ~x ^ Bitwise XOR x ^ y >> Bitwise right shift x>> << Bitwise left shift x<<
示例: Python中的位运算符
Python3
# Examples of Bitwise operators
a = 10
b = 4
# Print bitwise AND operation
print(a & b)
# Print bitwise OR operation
print(a | b)
# Print bitwise NOT operation
print(~a)
# print bitwise XOR operation
print(a ^ b)
# print bitwise right shift operation
print(a >> 2)
# print bitwise left shift operation
print(a << 2)
0
14
-11
14
2
40
赋值运算符
赋值运算符用于为变量赋值。Operator Description Syntax = Assign value of right side of expression to left side operand x = y + z += Add AND: Add right-side operand with left side operand and then assign to left operand a+=b a=a+b -= Subtract AND: Subtract right operand from left operand and then assign to left operand a-=b a=a-b *= Multiply AND: Multiply right operand with left operand and then assign to left operand a*=b a=a*b /= Divide AND: Divide left operand with right operand and then assign to left operand a/=b a=a/b %= Modulus AND: Takes modulus using left and right operands and assign the result to left operand a%=b a=a%b //= Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand a//=b a=a//b **= Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand a**=b a=a**b &= Performs Bitwise AND on operands and assign value to left operand a&=b a=a&b |= Performs Bitwise OR on operands and assign value to left operand a|=b a=a|b ^= Performs Bitwise xOR on operands and assign value to left operand a^=b a=a^b >>= Performs Bitwise right shift on operands and assign value to left operand a>>=b a=a>>b <<= Performs Bitwise left shift on operands and assign value to left operand a <<= b a= a << b
示例: Python中的赋值运算符
Python3
# Examples of Assignment Operators
a = 10
# Assign value
b = a
print(b)
# Add and assign value
b += a
print(b)
# Subtract and assign value
b -= a
print(b)
# multiply and assign
b *= a
print(b)
# bitwise lishift operator
b <<= a
print(b)
10
20
10
100
102400
身份运算符
is和is not是身份运算符,两者都用于检查两个值是否位于内存的同一部分。两个相等的变量并不意味着它们是相同的。
is True if the operands are identical
is not True if the operands are not identical
示例:身份运算符
Python3
a = 10
b = 20
c = a
print(a is not b)
print(a is c)
True
True
会员运营商
in和not in是成员运算符;用于测试一个值或变量是否在一个序列中。
in True if value is found in the sequence
not in True if value is not found in the sequence
示例:成员运算符
Python3
# Python program to illustrate
# not 'in' operator
x = 24
y = 20
list = [10, 20, 30, 40, 50]
if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
x is NOT present in given list
y is present in given list
运算符的优先级和关联性
运算符的优先级和关联性:运算符的优先级和关联性决定了运算符符的优先级。
运算符优先级
这用于具有多个具有不同优先级的运算符的表达式中,以确定首先执行哪个操作。
示例:运算符优先级
Python3
# Examples of Operator Precedence
# Precedence of '+' & '*'
expr = 10 + 20 * 30
print(expr)
# Precedence of 'or' & 'and'
name = "Alex"
age = 0
if name == "Alex" or name == "John" and age >= 2:
print("Hello! Welcome.")
else:
print("Good Bye!!")
610
Hello! Welcome.
运算符关联性
如果一个表达式包含两个或多个具有相同优先级的运算符符,则使用运算符关联性来确定。它可以是从左到右或从右到左。
示例:运算符关联性
Python3
# Examples of Operator Associativity
# Left-right associativity
# 100 / 10 * 10 is calculated as
# (100 / 10) * 10 and not
# as 100 / (10 * 10)
print(100 / 10 * 10)
# Left-right associativity
# 5 - 2 + 3 is calculated as
# (5 - 2) + 3 and not
# as 5 - (2 + 3)
print(5 - 2 + 3)
# left-right associativity
print(5 - (2 + 3))
# right-left associativity
# 2 ** 3 ** 2 is calculated as
# 2 ** (3 ** 2) and not
# as (2 ** 3) ** 2
print(2 ** 3 ** 2)
100.0
6
0
512