Is using '/' bigger than in Python?

I recently hit golf for codes and you need to save as many characters as possible.

I remember someone saying use if a/b: instead of if a<=b: However, I looked through the Python documentation and did not see anything like it.

I could remember that all this is wrong, but I'm sure I saw how this operator was used and recommended in several instances.

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

+6
source share
2 answers
 >>> 5/6 0 >>> bool(5/6) False >>> 6/5 1 >>> bool(6/5) True 

This is the result of the / operator in Python 2.7, performing integer division, and then converting the result to logical.

If you do from __future__ import division , this will no longer work, because the / operator will perform floating point division rather than integer division.

So the result will be > 0 for x < y and will still evaluate to True.

 >>> from __future__ import division >>> 5/6 0.8333333333333334 >>> bool(5/6) True >>> 6/5 1.2 >>> bool(6/5) True 
+5
source

This is just separation. And, at least for integers a >= 0 and b > 0 , a/b is true if a>=b . Since in this case a/b is a strictly positive integer, and bool() applied to a nonzero integer is True .

For arguments with zero and negative integer arguments, I'm sure you can solve the truth of a/b for yourself.

+6
source

Source: https://habr.com/ru/post/973769/


All Articles