Search⌘ K

Control Flow and Built-in Functions

Explore essential Python control flow concepts such as if-else conditions, elif statements, and for loops. Understand list comprehensions for efficient list creation and discover how to use built-in functions like sort, sorted, and zip to manipulate data effectively.

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:

Python 3.5
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))

The for construct

...