Python comparison operators list
Python comparison operators list
Here is the list of Python comparison operators:
==
Equal: Returns True if the values of two operands are equal.
Example:
a = 5
b = 5
print(a == b) # Output: True
!=
Not Equal: Returns True if the values of two operands are not equal.
Example:
a = 5
b = 10
print(a != b) # Output: True
>
Greater Than: Returns True if the value of the left operand is greater than the value of the right operand.
Example:
a = 5
b = 2
print(a > b) # Output: True
<
Less Than: Returns True if the value of the left operand is less than the value of the right operand.
Example:
a = 5
b = 7
print(a < b) # Output: False
>=
Greater Than or Equal to: Returns True if the value of the left operand is greater than or equal to the value of the right operand.
Example:
a = 5
b = 5
print(a >= b) # Output: True
<=
Less Than or Equal to: Returns True if the value of the left operand is less than or equal to the value of the right operand.
Example:
a = 5
b = 3
print(a <= b) # Output: False
is
Is (Identity): Checks whether two operands refer to the same object in memory.
Example:
a = [1, 2, 3]
b = a
print(a is b) # Output: True
is not
Is Not (Not Identity): Checks whether two operands do not refer to the same object in memory.
Example:
a = [1, 2, 3]
b = [4, 5, 6]
print(a is not b) # Output: True
in
In: Checks whether a value is present in a sequence (such as list, tuple, or string).
Example:
fruits = ['apple', 'banana', 'cherry']
print('banana' in fruits) # Output: True
not in
Not In: Checks whether a value is not present in a sequence.
Example:
fruits = ['apple', 'banana', 'cherry']
print('pear' not in fruits) # Output: True
@
Matmul (Matrix Multiplication): Used to perform matrix multiplication on numpy arrays.
Example:
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.matmul(a, b)) # Output: Matrix product
These comparison operators are used to compare values in Python and return a boolean result. They can be used in various contexts, such as control flow statements (if-else), logical operations, or data validation.
Comparison operators in Python with example
I'd be happy to help!
In Python, comparison operators are used to compare values and determine whether they are equal, greater than, less than, or fall within a specific range. These operators can be used in various contexts, such as conditional statements (if-else) and logical expressions.
Here's a list of Python's comparison operators along with examples:
Equal:==
Example:
x = 5
y = 5
print(x == y) # Output: True
Not Equal: !=
or <>
Example:
x = 5
y = 6
print(x != y) # Output: True
Greater Than: >
Example:
x = 10
y = 8
print(x > y) # Output: True
Less Than: <
Example:
x = 5
y = 7
print(x < y) # Output: True
Greater Than or Equal: >=
Example:
x = 10
y = 9
print(x >= y) # Output: True
Less Than or Equal: <=
Example:
x = 8
y = 8
print(x <= y) # Output: True
Is: is
(checks for identity, i.e., whether two variables refer to the same object)
Example:
a = [1, 2, 3]
b = a
print(a is b) # Output: True
Is Not: is not
(negation of the "is" operator)
Example:
a = [1, 2, 3]
b = "hello"
print(a is not b) # Output: True
These comparison operators can be used in various contexts, such as:
Conditional statements (if-else):if x > 5: print("x is greater than 5")
Logical expressions: (x > 5) and (y < 3)
Loops: while x <= 10: print(x); x += 1
It's essential to note that Python performs comparisons in a case-sensitive manner, which means that uppercase letters are considered distinct from lowercase letters. If you're working with strings and want to perform case-insensitive comparisons, you can use the casefold()
method or convert both strings to lowercase/uppercase before comparing them.
I hope this explanation helps! Let me know if you have any further questions.