Python中的关系运算符
关系运算符用于比较值。它根据条件返回 True 或 False。这些运算符也称为比较运算符。 Description > < == != >= <=Operator 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 left operand is greater than or equal to the right x >= y Less than or equal to: True if left operand is less than or equal to the right x <= y
现在让我们看看每个关系 操作员一一。
1)大于:如果左操作数大于右操作数,则此运算符返回 True。
句法:
x > y
例子:
Python3
a = 9
b = 5
# Output
print(a > b)
Python3
a = 9
b = 5
# Output
print(a < b)
Python3
a = 9
b = 5
# Output
print(a == b)
Python3
a = 9
b = 5
# Output
print(a != b)
Python3
a = 9
b = 5
# Output
print(a >= b)
Python3
a = 9
b = 5
# Output
print(a <= b)
输出:
True
2) 小于:如果左操作数小于右操作数,则此运算符返回 True。
句法:
x < y
例子:
Python3
a = 9
b = 5
# Output
print(a < b)
输出:
False
3)等于:如果两个操作数相等,即如果左操作数和右操作数彼此相等,则此运算符返回 True。
例子:
Python3
a = 9
b = 5
# Output
print(a == b)
输出:
False
4) 不等于:如果两个操作数不相等,此运算符返回 True。
句法:
x != y
例子:
Python3
a = 9
b = 5
# Output
print(a != b)
输出:
True
5)大于或等于:如果左操作数大于或等于右操作数,则此运算符返回 True。
句法:
x >= y
例子:
Python3
a = 9
b = 5
# Output
print(a >= b)
输出:
True
6) 小于或等于:如果左操作数小于或等于右操作数,则此运算符返回 True。
句法:
x <= y
例子:
Python3
a = 9
b = 5
# Output
print(a <= b)
输出:
False