Pythonic comparisons

I would like to know what is considered Pythonic or, if you will, more readable regarding the use of comparison operators. Let me give an example.

So, would you say that

if my_index >= 0:
    print("my_index is bigger or equal to zero.")

is more Pythonic than

if my_index > -1:
    print("my_index is bigger than -1.")

or is it the other way around?

I’m sure that other more experienced coders will have a better answer, but I’d say that, given your example, it’s really only the ‘message’ that’s any different, as the branch evaluates the same.

The only other observation I have is that (and I know that this is a corner case because it’s unlikely that you’d have a floating point number as an index) if the branch was to evaluate a floating point number, then >= 0 would be the way to go.

I find the ‘-1’ to be an inversion requiring a mental stop-and-check. As Ron pointed out, that check includes a type(my_index) == float? among other cracks and crinkles. my_index >= 0 is more readable because it flows consistently forward, whereas the my_index > -1 “skips backward” before moving forward. The prominent feature of your examples is the -1.

In a my_index > 9 vs my_index >= 10, I sometimes prefer the > 9, but will consider the use case carefully before using it.

Readability is certainly a cornerstone of what is meant by ‘Pythonic’. There are several cornerstones.

"From the glossary at `docs.python.org`" (click to unfold)

Pythonic

An idea or piece of code which closely follows the most common idioms of the Python language, rather than implementing code using concepts common to other languages. For example, a common idiom in Python is to loop over all elements of an iterable using a for statement. Many other languages don’t have this type of construct, so people unfamiliar with Python sometimes use a numerical counter instead:

for i in range(len(food)): print(food[i])

As opposed to the cleaner, Pythonic method:

for piece in food: print(piece)

I think a fitting definition of “Pythonic” is code that uses the distinguishing features of Python to apply the principles in PEP20: The Zen of Python (which are neither rules–much less Holy Writ–nor a mere exercise in humor). It’s an entertaining and though-provoking statement of some very fundamental best practices.

The example in the glossary entry above follows several of the principles. You can find much discussion of the Zen here at discuss.python.org.

1 Like

By Boštjan Mejak via Discussions on Python.org at 18Jun2022 08:31:

I would like to know what is considered Pythonic or, if you will, more
readable regarding the use of comparison operators. Let me give an
example.

So, would you say that

if my_index >= 0:
   print("my_index is bigger or equal to zero.")

is more Pythonic than

if my_index > -1:
   print("my_index is bigger than -1.")

or is it the other way around?

I’m basicly reiterating what the other replies have said:

  • readability counts: absent special circumstances, the most important
    thing about code is that it is readable, that its meaning is clear to
    the person reading it
  • Pythonic code uses Python idioms to make readable code; a Python idiom
    is a convenient and hopefully commonly used Python expression or
    construct

I prefer the former test (>=0). If you’re working with ints, the two
tests test the same thing. If working with floats, they do not.

Now, assuming you’re using ints, what is the actual boundary point
you’re concerned with? It seems to be 0. The former test uses that
boundary in the test, so its meaning is clear. The latter test tests
against “a number known not to be in inside the boundary but as
nonnegative as you can get and still do that”. Harder to think about and
explain, to my mind.

I try to talk about the core values in the test when I can. So if the
core value here is 0, I like 0 to be in the test expression.

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes

+1 for using >= 0 over > -1. There are two main reasons for that:

  • On lowest level, both >= and > are one CPU intruction.
  • What’s the point of avoiding core features of a programming language?

If there are >=, you can use it if you mean greater or equal.

Thank you all for pointing me to use >= 0 as oppose to > -1. I wasn’t sure what is best. Now I know. :slight_smile::+1: