avatarFatos Morina

Summary

The article discusses a straightforward method for comparing three numbers in Python, similar to mathematical expressions.

Abstract

The article "How to Compare 3 Numbers in Python Just Like in Math" explains that while traditional programming comparison involves multiple conditional statements, Python allows for a more intuitive approach reminiscent of elementary math expressions. The author emphasizes that instead of using complex logic, one can simply use a chained comparison such as 1 < x < 10 directly in Python code. This method simplifies the process of determining if a value falls within a range or ordering multiple variables. The article also points out that this feature is underutilized despite its simplicity and direct correlation to mathematical expressions taught in school.

Opinions

  • The author suggests that the common method of comparing three numbers in Python is clunky and could be simplified.
  • They express surprise that the simpler method of chained comparisons is not more widely known or used in the programming community.
  • The author believes that this straightforward comparison technique does not require memorization of fancy methods and is as easy as recalling math expressions from school.
  • There is an implication that programming languages, particularly Python, can incorporate more intuitive mathematical concepts, making coding more accessible.

How to Compare 3 Numbers in Python Just Like in Math

Photo by Clem Onojeghuo on Unsplash

One of the main ways that you are used to do comparisons in a programming language is the following:

a < b

Now, if you need to do a comparison with a third variable, it gets a bit clunky:

a

Yes, it does work like this, however, since you are reading this article, you probably can tell by now that there is indeed a simpler method that you can use to compare numbers.

And it is also quite straightforward.

You do not need to memorize any fancy method whatsoever. It’s just like a Math expression that you memorize from school.

If you have a value and you want to compare it whether it is between two other values, there is a simple expression that you use in Math:

1 < x < 10

That is the algebraic expression that we learn in elementary school. However, you can also use that same expression in Python as well.

Yes, you read that right. You have probably done comparisons of such form up until now:

1 < x and x < 10

For that, you simply need to use the following in Python:

1 < x < 10

Needless to say, but you can also do the other way around:

a > b > c

And yes, you can also use the equal sign with both “>” and “<”:

a >= b >= c

That’s not a new reinvention of a wheel or rocket science, but it can still be worth knowing that such a simple comparison does exist in Python.

I am a bit surprised that I do not see it being used or mentioned in more places.

More content at plainenglish.io

Programming
Python
Artificial Intelligence
Machine Learning
Data Science
Recommended from ReadMedium