Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using '/' as greater than less than in Python?

I recently got into code golfing and need to save as many characters as possible.

I remember seeing someone say to use if a/b: instead of if a<=b:. However, I looked through Python documentation and saw nothing of the sort.

I could be remembering this all wrong, but I'm pretty sure I've seen this operator used and recommended in multiple instances.

Does this operator exist? If so, how does it work?

like image 592
RageCage Avatar asked Aug 13 '14 14:08

RageCage


People also ask

Can you use >= in Python?

Many programming beginners wonder how to write “greater than or equal to” in Python. Well, to write greater than or equal to in Python, you need to use the >= comparison operator. It will return a Boolean value – either True or False. The "greater than or equal to" operator is known as a comparison operator.


1 Answers

That's just division. And, at least for integers a >= 0 and b > 0, a/b is truthy if a>=b. Because, in that scenario, a/b is a strictly positive integer and bool() applied to a non-zero integer is True.

For zero and negative integer arguments, I am sure that you can work out the truthiness of a/b for yourself.

like image 192
David Heffernan Avatar answered Sep 22 '22 01:09

David Heffernan