top of page

Truthy and Falsy Values in Python

  • Writer: CL
    CL
  • Jan 30, 2020
  • 1 min read

Updated: Apr 8, 2020

ree


Takeaway from the freecodecamp article


Falsy Values include:


  • Sequences & Collections: Empty lists, dictionaries, tuples, sets, strings, range(0)

  • Numbers: Zero of any numeric types including integer (0), float (0.0), complex(0Abc)

  • Constants: none, False



Truthy Values Include:

non-zero numeric values, non-empty sequences and collections, True


Python built-in function: bool() function to check whether a value is truthy or falsy



Benefits:

  • More Concise code with IF or WHILE loop

  • Make custom objects truthy and falsy values by adding the special method __bool__( )


ree

jupyte notebook example

Comments


bottom of page