Control Flow and Built-in Functions
This lesson covers how the flow of execution transfers between lines of code depending on the scenarios. It also discusses some built-in functions for data structures studied in the previous lesson.
Control structures in Python #
The if-else construct #
If-then statements are a staple of any programming language. Basically, if you meet a certain condition, then something happens. In Python, elif
stands for else if
, meaning that if the previous conditions were not met, check that condition. Else
is a catch-all condition for any remaining flows. Python follows the following syntax:
if condition:
statements
elif condition:
statements
else:
statements
Following is a code example:
Press + to interact
def age_check(age):if age > 40: # if age greater than 40, print "Older than 40"print("Older than 40")elif age > 30 and age <= 40: # if age greater than 30 and less than or equal to 40, print "Between 30 and 40"print("Between 30 and 40")else: # if neither of the previous conditions are met, print "Other"print("Other")print(age_check(41))