📜  Python运算符

📅  最后修改于: 2022-05-13 01:55:33.080000             🧑  作者: Mango

Python运算符

Python运算符通常用于对值和变量执行操作。这些是用于逻辑和算术运算的标准符号。在本文中,我们将研究不同类型的Python运算符。

算术运算符

算术运算运算符用于执行数学运算,例如加法、减法、乘法和除法。

OperatorDescriptionSyntax
+Addition: adds two operandsx + y
Subtraction: subtracts two operandsx – y
*Multiplication: multiplies two operandsx * y
/Division (float): divides the first operand by the secondx / y
//Division (floor): divides the first operand by the secondx // y
%Modulus: returns the remainder when the first operand is divided by the secondx % y
**Power: Returns first raised to power secondx ** 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

注意:有关这两个运算符的一些有趣事实,请参阅 / 和 // 之间的差异。

比较运算符

关系运算符的比较比较值。它根据条件返回TrueFalse

OperatorDescriptionSyntax
>Greater than: True if the left operand is greater than the rightx > y
<Less than: True if the left operand is less than the rightx < y
==Equal to: True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to True if the left operand is greater than or equal to the rightx >= y
<=Less than or equal to True if the left operand is less than or equal to the rightx <= 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

逻辑运算符

逻辑运算符执行逻辑与逻辑或逻辑非运算。它用于组合条件语句。

OperatorDescriptionSyntax
andLogical AND: True if both the operands are truex and y
orLogical OR: True if either of the operands is true x or y
notLogical 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

位运算符

位运算符作用于位并执行逐位操作。这些用于对二进制数进行操作。

OperatorDescriptionSyntax
&Bitwise ANDx & y
|Bitwise ORx | y
~Bitwise NOT~x
^Bitwise XORx ^ y
>>Bitwise right shiftx>>
<<Bitwise left shiftx<<

示例: 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

赋值运算符

赋值运算符用于为变量赋值。

OperatorDescriptionSyntax
=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 operanda+=b     a=a+b
-=Subtract AND: Subtract right operand from left operand and then assign to left operanda-=b     a=a-b
*=Multiply AND: Multiply right operand with left operand and then assign to left operanda*=b     a=a*b
/=Divide AND: Divide left operand with right operand and then assign to left operanda/=b     a=a/b
%=Modulus AND: Takes modulus using left and right operands and assign the result to left operanda%=b     a=a%b
//=Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operanda//=b     a=a//b
**=Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operanda**=b     a=a**b
&=Performs Bitwise AND on operands and assign value to left operanda&=b     a=a&b
|=Performs Bitwise OR on operands and assign value to left operanda|=b     a=a|b
^=Performs Bitwise xOR on operands and assign value to left operanda^=b     a=a^b
>>=Performs Bitwise right shift on operands and assign value to left operanda>>=b     a=a>>b
<<=Performs Bitwise left shift on operands and assign value to left operanda <<= 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

身份运算符

isis 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

会员运营商

innot 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

Python运算符测验