Logo

Developer learning path

Python

Comparison Operators in Python

Comparison Operators

64

#description

Comparison operators are used in Python to compare values and return Boolean values (True or False) depending on whether the comparison is true or not.

The following are the comparison operators in Python:

  1. Equal to operator (==): This operator compares two values and returns True if they are equal, and False otherwise.
  1. Not Equal to operator (!=): This operator compares two values and returns True if they are not equal, and False otherwise.
  1. Greater than operator (>): This operator compares two values and returns True if the first value is greater than the second value, and False otherwise.
  1. Less than operator (<): This operator compares two values and returns True if the first value is less than the second value, and False otherwise.
  1. Greater than or equal to operator (>=): This operator compares two values and returns True if the first value is greater than or equal to the second value, and False otherwise.
  1. Less than or equal to operator (<=): This operator compares two values and returns True if the first value is less than or equal to the second value, and False otherwise.

Here are some examples of using comparison operators in Python:

a = 5

b = 10

print(a == b) # False

print(a != b) # True

print(a > b) # False

print(a < b) # True

print(a >= b) # False

print(a <= b) # True

We use these operators frequently in conditional statements like if, elif or while loops.

March 25, 2023

If you don't quite understand a paragraph in the lecture, just click on it and you can ask questions about it.

If you don't understand the whole question, click on the buttons below to get a new version of the explanation, practical examples, or to critique the question itself.