HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

10) Conditionals and Loops Lesson

Python If Else Statements

6 min to complete · By Martin Breuss

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

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.

Colorful illustration of a light bulb

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-else constructs 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 else keyword gets executed when the conditional statement following the if keyword evaluates to False
  • When the else block is executed, all code inside the if code block is completely ignored
  • Python if else statements allow you to remove the concept of a conditional flag