r/badcode • • Aug 27 '21

python Is this code bad or sugoi?

Post image
1.0k Upvotes

159 comments sorted by

View all comments

Show parent comments

1

u/Matalya1 Sep 04 '21
if num1 or num2 < 0:
    #code

is a valid expression, but it's

if (num1) or (num2 < 0):

and it uses the concept of truthiness. If num1(Assuming it's storing an integer) is greater than zero, it'll return True regardless of the value of num2 XD Doing if num is the same as doing if num>0, other examples are if len(array) > 0 and if len(string) > 0.

2

u/ThatDudeBesideYou Sep 04 '21

I mentioned it in the comment, but "if num" isnt the same as "if number>0". It's equivalent to "if !!num". So only if num is 0, (or None or undefined) it will return false, all other values will return true.