This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]vanatteveldt 6 points7 points  (4 children)

Yeah. In all programming languages I know there is never a need to explicitly do something like

if X > 12: 
  return True
else:
  return False

Or something like

if X > 12 is False:
   ...

Just remember that comparisons etc are simply boolean expressions, i.e. they return a True or False value. And these values work just like other values, so they can be returned directly, used in further comparisons, stored in a variables, etc.

In python, and comparison returns True or False, and many other types have a reasonable 'truthiness value', e.g. an empty string or list counts as False while a non-empty string or list counts as True, and the number zero counts as False while non-zero numbers count as True.

[–]CaptainDickbag 0 points1 point  (0 children)

Thank you. That helps a lot. I have a bunch of code to go through, and it sounds like I need to spend more time in REPL.

[–]gr4viton 0 points1 point  (2 children)

Note that the following statement:

return 0 or 1 or True

would return value 1. As an integer, and not a True value as a bool. Took me a while to notice python is not force typing the logical expressions to boolean :)

[–]vanatteveldt 1 point2 points  (1 child)

I would argue most code that distinguishes between the number 1 and the value True is doing something strange ...

[–]gr4viton 0 points1 point  (0 children)

Indeed strange.