PyPython · Lesson 3 of 14
Control Flow
Python uses indentation to define code blocks. Not curly braces. Not keywords. Whitespace. If you love arguments, bring this up at your next dinner party.
Python's if/elif/else statements work like you'd expect. The colon after the condition is required, and the body must be indented consistently (4 spaces is the standard). Python uses elif (not else if) for chained conditions.
Python has two loop types: for (iterates over a sequence) and while (runs while a condition is true). The for loop is used far more often in idiomatic Python.
◆ Note
Python's "truthy" and "falsy" values: empty strings, empty lists, 0, None, and False are all falsy. Everything else is truthy. This lets you write: if my_list: instead of if len(my_list) > 0:
Python's "truthy" and "falsy" values: empty strings, empty lists, 0, None, and False are all falsy. Everything else is truthy. This lets you write: if my_list: instead of if len(my_list) > 0: