In Python, branching control structures are used to change the normal flow of execution based on some condition. There are three types of branching control structures in Python:
If statements are an example of a branching control structure. They allow your code to respond to things like variable values and user input by executing different blocks of code in different situations.
The following is an example of an if statement:
if x == 10: print("x is equal to 10")
In this example, the value of the variable x is checked to see if it is equal to 10. If it is, the block of code inside the if statement will be executed. Otherwise, the block of code will be skipped. See the diagram below for a visual representation of this:
I'd like you to notice a few other things about these lines of code, as well:
Here is an example of what it would look like to put this if statement and a code block in a regular program:
print("This is a regular line of code") if x == 10: print("x is equal to 10") print("This line also only gets printed if x is equal to 10") print("This will be printed no matter what.")
So, if x is equal to 10 above, you will see the following when you run the program:
This is a regular line of code x is equal to 10 This line also only gets printed if x is equal to 10 This will be printed no matter what.
If x is equal to anything else, then you would see the following:
This is a regular line of code This will be printed no matter what.
The following is an example of an else statement:
if x == 10: print("x is equal to 10") else: print("x is not equal to 10")
In this example, the value of the variable x is checked to see if it is equal to 10. If it is, the block of code inside the if statement will be executed. Otherwise, the block of code inside the else statement will be executed.
Here is a diagram that shows how this works:
Notice that in the code, the “if” line and the “else” line are both indented the same. Also, the blocks of code beneath these lines are all indented one level more than the if and else lines. This is how Python. knows that these statements and blocks of code go together.
A simple way to think about an if…else block is an “either/or” situation, where you are telling Python to “do thing A” if something is true, and to “do thing B” if it isn't.
The following is an example of an elif statement:
if x == 10: print("x is equal to 10") elif x == 20: print("x is equal to 20") else: print("x is not equal to 10 or 20")
In this example, the value of the variable x is checked to see if it is equal to 10. If it is, the block of code inside the if statement will be executed. Otherwise, the value of x is checked to see if it is equal to 20. If it is, the block of code inside the elif statement will be executed. Otherwise, the block of code inside the else statement will be executed.
Notice that in the code block above, there are 3 different possible outcomes:
Here is a visual flowchart showing how this block of code works:
Also, notice that the if and the elif lines have expressions, but the else line doesn't. This is because the else block is what gets executed if none of the expressions in if and elif are true.
While you are always required to start out with an if, elif and else are optional. You can also have as many elifs as you want, with or without an else. So many possibilities!
Branching control structures (if..elif…else) are an essential part of Python programming, and pretty much every other kind of programming as well. By understanding how branching control structures work, you can write more powerful and efficient code.
Here are some additional tips for using branching control structures:
With a little practice, you'll be using branching control structures like a pro!
Player 1 and Player 2 are playing Rock, Paper, Scissors. If Player 1 chooses Rock, then what happens if Player 2 chooses Rock, Paper or Scissors?
Can you write an if…elif…else block in the window below that will show the correct responses based on what Player 2 chooses?
No terms have been published for this module.
Test your knowledge of this module by choosing options below. You can keep trying until you get the right answer.
Skip to the Next Question