Python Comparison Operators
Python Comparison Operators
Python comparison operators are used to compare two values or variables and return a Boolean result: True or False.
1) Comparison operators work with various data types, including numbers, strings (alphabetical comparison), and custom objects if comparison methods are defined.
2) A single equal sign (=) is the assignment operator, used to assign a value to a variable, while == is for equality comparison.
3) Python supports chained comparisons, allowing multiple comparisons in a single expression, such as 1 < x < 10, which evaluates to True only if all individual comparisons are True.
Primary comparison operators in Python:
Equal to (==): Checks if two values are equal.
Python
x = 10
y = 10
print(x == y)
Output: True
Not Equal to (!=): Checks if two values are not equal.
Python
a = 5
b = 8
print(a != b)
Output: True
Greater than (>): Checks if the left operand is greater than the right operand.
Python
num1 = 15
num2 = 7
print(num1 > num2)
Output: True
Less than (<): Checks if the left operand is less than the right operand.
Python
val1 = 20
\\=\
val2 = 30
print(val1 < val2)
Output: True
Greater than or Equal to (>=): Checks if the left operand is greater than or equal to the right operand.
Python
score1 = 90
score2 = 90
print(score1 >= score2)
Output: True
Less than or Equal to (<=): Checks if the left operand is less than or equal to the right operand.
Python
temp1 = 25
temp2 = 28
print(temp1 <= temp2)
Output: True

No comments: