Control Flow
Learn how to build control flow into your python code by including if, else, and elif statements as well as try and except statements. Expect to learn all you need to know about boolean variables and logical operators.
StartKey Concepts
Review core concepts you need to learn to master this subject
elif
Statement
Handling Exceptions in Python
or
Operator
Equal Operator ==
Not Equals Operator !=
Comparison Operators
if
Statement
else
Statement
elif
Statement
elif
Statement
# elif Statement
pet_type = "fish"
if pet_type == "dog":
print("You have a dog.")
elif pet_type == "cat":
print("You have a cat.")
elif pet_type == "fish":
# this is performed
print("You have a fish")
else:
print("Not sure!")
The Python elif
statement allows for continued checks to be performed after an initial if
statement. An elif
statement differs from the else
statement because another expression is provided to be checked, just as with the initial if
statement.
If the expression is True
, the indented code following the elif
is executed. If the expression evaluates to False
, the code can continue to an optional else
statement. Multiple elif
statements can be used following an initial if
to perform a series of checks. Once an elif
expression evaluates to True
, no further elif
statements are executed.
- 1Imagine waking up in the morning. You wake up and think, “Ugh. Is it a weekday?” If so, you have to get up and get dressed and get ready for work or school. If not, you can sleep in a bit longer…
- 2In order to build control flow into our program, we want to be able to check if something is true or not. A boolean expression is a statement that can either be True or False. Let’s go back to the…
- 3Now that we understand what boolean expressions are, let’s learn to create them in Python. We can create a boolean expression by using relational operators. Relational operators compare two it…
- 4Before we go any further, let’s talk a little bit about True and False. You may notice that when you type them in the code editor (with uppercase T and F), they appear in a different color than var…
- 5“Okay okay okay, boolean variables, boolean expressions, blah blah blah, I thought I was learning how to build control flow into my code!” You are, I promise you! Understanding boolean variables…
- 6Now that we’ve added conditional statements to our toolkit for building control flow, let’s explore more ways to create boolean expressions. So far we know two relational operators, equals and not …
- 7Often, the conditions you want to check in your conditional statement will require more than one boolean expression to cover. In these cases, you can build larger boolean expressions using _boolean…
- 8The boolean operator or combines two expressions into a larger expression that is True if either component is True. Consider the statement Oranges are a fruit or apples are a vegetable. This s…
- 9The final boolean operator we will cover is not. This operator is straightforward: when applied to any boolean expression it reverses the boolean value. So if we have a True statement and apply a n…
- 10As you can tell from your work with Calvin Coolidge’s Cool College, once you start including lots of if statements in a function the code becomes a little cluttered and clunky. Luckily, there are…
- 11We have if statements, we have else statements, we can also have elif statements. Now you may be asking yourself, what the heck is an elif statement? It’s exactly what it sounds like, “else if”. A…
What you'll create
Portfolio projects that showcase your new skills
Magic 8-Ball
We’ve learned about and explored a powerful tool in Python — control flow! It’s so powerful, in fact, that it can be used to tell someone’s fortune.
Sal's Shipping
Use your knowledge of control flow to create a program that will help Sal's customers get the best deals on all their shipping needs.
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory