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.
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.