coding180 icon
Coding180
Python Comparison Operators: =, !=, >, <, >=, <=

Python Comparison Operators: =, !=, >, <, >=, <=

Home > Tutorials > Python for beginners > Operators


As a beginner, understanding comparison operators is essential. In this article, we'll introduce you to six comparison operators in Python.

What are Comparison Operators?

Comparison operators are used to compare values in programming languages. Python has six types of comparison operators:

  1. Equal to (==)
  2. Not equal to (!=)
  3. Greater than (>)
  4. Less than (<)
  5. Greater than or equal to (>=)
  6. Less than or equal to (<=)

Examples and Exercises

Let's take a closer look at each one of these comparison operators and provide some examples and exercises to help you understand how they work.

Equal to (==)

The equal to operator is represented by two equal signs (==) and is used to check if two values are equal. For example:

x = 5
y = 5
if x == y:
    print("x is equal to y")

In this example, we are checking if the value of x is equal to the value of y. Since both values are equal, the output will be "x is equal to y".

- Exercise 1

Write a program that checks if two numbers are equal and prints out a message accordingly.

Not equal to (!=)

The not equal to operator is represented by an exclamation mark followed by an equal sign (!=) and is used to check if two values are not equal. For example:

x = 5
y = 3
if x != y:
    print("x is not equal to y")

In this example, we are checking if the value of x is not equal to the value of y. Since both values are not equal, the output will be "x is not equal to y".

Exercise 2

Write a program that checks if two numbers are not equal and prints out a message accordingly.

Greater than (>)

The greater than operator is represented by the greater than sign (>) and is used to check if one value is greater than another. For example:

x = 5
y = 3
if x > y:
    print("x is greater than y")

In this example, we are checking if the value of x is greater than the value of y. Since x is indeed greater than y, the output will be "x is greater than y".

Exercise 3

Write a program that checks if one number is greater than another and prints out a message accordingly.

Less than (<)

The less than operator is represented by the less than sign (<) and is used to check if one value is less than another. For example:

x = 3
y = 5
if x < y:
    print("x is less than y")

In this example, we are checking if the value of x is less than the value of y. Since x is indeed less than y, the output will be "x is less than y".

Exercise 4

Write a program that checks if one number is less than another and prints out a message accordingly.

Greater than or equal to (>=)

The greater than or equal to operator is represented by the greater than sign followed by an equal sign (>=) and is used to check if one value is greater than or equal to another. For example:

x = 5
y = 5
if x >= y:
    print("x is greater than or equal to y")

In this example, we are checking if the value of x is greater than or equal to the value of y. Since x is equal to y, the output will be "x is greater than or equal to y".

Exercise 5

Write a program that checks if one number is greater than or equal to another and prints out a message accordingly.

Less than or equal to (<=)

The less than or equal to operator is represented by the less than sign followed by an equal sign (<=) and is used to check if one value is less than or equal to another. For example:

x = 5
y = 5
if x <= y:
    print("x is less than or equal to y")

In this example, we are checking if the value of x is less than or equal to the value of y. Since x is equal to y, the output will be "x is less than or equal to y".

Exercise 6

Write a program that checks if one number is less than or equal to another and prints out a message accordingly.

Conclusion

Comparison operators are essential for making decisions in programming languages like Python. By mastering these operators, you will have a solid foundation for building more complex programs. So go ahead and try out some of the exercises we've provided, and don't be afraid to experiment with different values and operations!

Remember, these operators are just the beginning of what you can do with Python. Keep exploring and learning, and you'll be surprised at what you can accomplish. Happy coding!


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel