In the previous lesson, you wrote your first conditional statements using the if keyword.
Chaining If Statements
After finishing the task, you ended up with a code snippet similar to the one below:
flag = True
if flag == True:
print("yay!")
flag = False # Reassign the flag value
if flag == False:
print("yo!") # Now this prints too!
You changed the state of the flag variable to weave your way through the code and make sure that both print() calls will execute.
Now, you only want one of the conditional code blocks to execute. Which one? That should depend on the state of your flag. In this scenario, you can skip re-assigning its value:
flag = True
if flag == True:
print("yay!")
if flag == False:
print("yo!")
In the example above, the value of your flag variable decides which of the two print() calls will run. While this works, there's a more elegant and concise way to do the same.
Python Else If Keywords
Because your Boolean flag should only be True or False, you can write a more concise version of the code by adding the else keyword:
flag = True
if flag == True:
print("yay!")
else:
print("yo!")
When are the Blocks Executed
If the conditional statement next to the if keyword evaluates to True, then the first indented code block runs. In this case, the second code block gets skipped.
If the conditional statement evaluates to False instead, then the first indented code block gets skipped, and the second one gets executed.
If Else Python Practice
- Write a conditional statement that encodes Hamlet's famous phrase To be, or not to be into Python code.
As you can see, using the else keyword in your conditional statements can make your code more concise and user-friendly.
Complex Conditional Statements
Great! You managed to cut down the necessary code and even made it easier to read.
Remove Conditional Flag
So now it's time to ditch the flag completely and introduce more complex conditional statements following the if keyword. For example:
if 2 + 2 == 4:
print("Math was right!")
else:
print("Whoa! Quantum Mechanics? 1984? Halpp!!")
In this example, you are using the expression 2 + 2 == 4 following the if keyword. The expression evaluates either to True or False; in this case, it will be True.
Info: You can use any Python expression that evaluates to either True or False as the decision-maker in your conditional statement.
Just like in the example further up in this lesson, if the expression following the if statement evaluates to True, then the first indented code block runs.
If the expression evaluates to False, then the first indented code block gets skipped, and the second one, following the else keyword, executes.
Tasks
- Write three different
if-elseconstructs that each use different operators to decide which code block should run. - Return to the section about operators and booleans to refresh your memory.
- Can you think of a small game that depends on making such decisions? Post your ideas in Discord.
There are a lot of decisions that you need to take on a daily basis, and your code needs to be able to make decisions as well. Using conditional statements with if and else gives you a concise way of expressing different paths your code can take.
Summary: Python If Else Statements
- The indented code block following the
elsekeyword gets executed when the conditional statement following theifkeyword evaluates toFalse - When the
elseblock is executed, all code inside theifcode block is completely ignored - Python
ifelsestatements allow you to remove the concept of a conditional flag