Explaining Comparison Operators in Python

I have covered quite a few of the basics of Python I learned. Today, I continue that trend by explaining how to use comparison operators. Comparison operators are used to compare various values. The result of the comparison is always a Boolean (True or False). Using comparison operators is an essential part of Python. It also combines well with the input function, which I wrote an article about as well. Let’s get into it.

How to use comparison operators

There are a total of 6 comparison operators in Python. The easiest way to try them, is using the Python interpreter. I recently wrote an article explaining how to use PowerShell with Python. I recommend reading it if you are unsure on how to use it, or if you need a refresher.

You can use these comparison operators without any other syntax. I won’t give a long textual explanation on how to use these operators. Instead, I wrote an example with commentary that should do the job.

# this is the "less than" operator, which checks if the value on the right
# is smaller than the one on the left
10 < 5 # this returns false, because 10 isn't smaller than 5
12 < 62 # this returns true, because 12 is smaller than 62
4 < 4 # this returns false, because 4 is equal to 4, not smaller
# this is the "less than or equal to" operator, which is the same as less
# than, but includes the value on the right of the operator
1 <= 2 # this returns true, because 1 is smaller than 2
2 <= 2 # this returns true, because 2 is equal to 2
3 <= 2 # this returns false, because 3 is not smaller than or equal to 2
# this is the "greater than" operator, which checks if the value on the
# right is larger than the one on the left
5 > 3 # this returns true, because 5 is larger than 3
3 > 3 # this returns false, because 3 is equal to 3, not larger
3 > 4 # this returns false, because 3 is smaller than 4
# this is the "greater than or equal to" operator, which is the same as
# greater than, but includes the value on the right
1 >= 2 # this returns false, because 1 is smaller than 2
2 >= 2 # this returns true, because 2 is equal to 2
3 >= 2 # this returns true, because 3 larger than 2
# this is the "equivalent to" operator, which checks if the given values
# are equal
1 == 2 # this returns false, because 1 does not equal 2
2 == 2 # this returns true, because 2 is equal to 2
3 == 2 # this returns true, because 3 does not equal 2
# this is the "not equivalent to" operator, which checks if the given
# values are not equivalent to each other
1 != 2 # this returns true, because 1 does not equal 2
2 != 2 # this returns false, because 2 does equal 2
3 != 2 # this returns false, because 3 does not equal 2

This should give you a good idea on how the comparison operators work. However, I still encourage you try them out yourself. That way, you’ll get a handle on them quickly. You can reference this part of the Python documentation for more information if you need it.

Wrapping up

Like mentioned earlier, the combination of user input and comparison operators sets up neatly for conditional logic. Conditional logic is another essential part of Python (and most programming languages). It will allow for a lot more functionality in your programs. While I didn’t cover it today, I will soon. Until then!

Let me know if this was helpful or if you would like to see more of these types or articles in the future. As always, if you have questions or concerns, feel free to comment below. Lastly, make sure to share the article if you liked it!

I am an ambitious student currently studying software engineering and journeying through the world of software development. I also have a passion for gaming, football, darts, F1 and other sports and I'm the founder of Iconic Developers.

I am an ambitious student currently studying software engineering and journeying through the world of software development. I also have a passion for gaming, football, darts, F1 and other sports and I'm the founder of Iconic Developers.

Categories

Archives

Iconic Developers is a personal blog dedicated to my journey through the world of software and web development where I share my knowledge and experience. Do you have questions, concerns or anything else? Just get in touch!