), less than (<), greater than or equal to (>=), and less than or equal to (<=), all returning Boolean results of True or False. It emphasizes the distinction between the equality operator (==) and the assignment operator (=). Additionally, it provides examples and encourages practice with these operators.">), less than (<), greater than or equal to (>=), and less than or equal to (<=), all returning Boolean results of True or False. It emphasizes the distinction between the equality operator (==) and the assignment operator (=). Additionally, it provides examples and encourages practice with these operators.">
Comparison PYTHON Operators with Equations
Comparison PYTHON Operators with Equations
The following examples demonstrate how to use comparison operators with the data types int
(integers, whole numbers) and float (number with a decimal point or fractional value). Comparison
operators return Boolean results. As you learned previously, Boolean is a data type that can hold only
one of two values: True or False.
• == (equality)
• != (not equal to)
• > (greater than)
• < (less than)
• >= (greater than or equal to)
• <= (less than or equal to)
• To check if two values are the same, use the equality operator: ==
• To check if two values are not the same, use the not equal to operator: !=
The print() function can be used to display the results of the comparisons.
Examples:
• To check if one value is larger than another value, use the greater than operator: >
• To check if one value is smaller than another value, use the less than operator: <
Examples:
print(11 > 3*3) # The > operator checks if the left value is
True # greater than the right value. If true, it
# returns a True result.
print(4/2 > 8-4) # If the > operator finds that the left value
False # is NOT greater than the right value, the
# comparison will return a False result.
print(4/2 < 8-4) # The < operator checks if the left value is
True # less than the right side. If true, the
# comparison returns a True result.
print(11 < 3*3) # If the < operator finds that the left side is False
• To check if one value is larger than or equal to another value, use the greater than or equal to
operator: >=
• To check if one value is smaller than or equal to another value, use the less than or equal to
operator: <=
Examples:
print(12*2 >= 24) # The >= operator checks if the left value is
True # greater than or equal to the right value.
# If one of these conditions is true,
# Python returns a True result. In this case
# the two values are equal. So, the comparison
# returns a True result.
print(18/2 >= 15) # If the >= comparison determines that the left False
False # value is NOT greater than or equal to the
# right, it returns a False result.
print(12*2 <= 30) # The <= operator checks if the left value is
True # less than or equal to the right value. In
# this case, the left value is less than the
# right value. Again, if one of the two
# conditions is true, Python returns a True
# result.
print(15 <= 18/2) # If the <= comparison determines that the left
False # value is NOT less than or equal to the right
# value, the comparison returns a False result.
PART 4: Practice
If you would like more practice using comparison operators, feel free to create your own
comparisons using the code block below. Note that there is no feedback associated with this code
block.
For additional Python practice, the following links will take you to several popular online interpreters
and codepads:
• Welcome to Python
• Online Python Interpreter
• Create a new Repl
• Online Python-3 Compiler (Interpreter)
• Compile Python 3 Online
• Your Python Trinket
Key takeaways
Python comparison operators return Boolean results: True or False.