Table of Contents » Chapter 3 : Processing : Decisions : if-else Statements
if-else Statements
Contents
- Overview
- General Form
- Example
- Practice Problems
Overview
On the previous page, we learned the if statement, which evaluates a condition and runs a block of code if the result of that condition is True. Often though we need to run one block of code based on a True result and a different block of code based on a False result. The if-else statement handles this scenario.
The general form of the if-else statement looks like this:
if condition:
code_block_based_on_true_condition
else:
code_block_based_on_false_condition
The key details to notice about this general form are the following:
- The initial if keyword which indicates to Python that it must evaluate an expression (condition) in order to make a decision.
- The condition that follows the if keyword is the expression that must evaluate to True in order for the code_block_based_on_true_condition to be run. If the condition evaluates to False, the second code block (code_block_based_on_false_condition) after the else: will be run instead.
- The colons (:) at the end of both the if statement and the else statement are required syntactic element that indicates the end of those statements and the beginning of their associated code blocks.
- The indentation of the code blocks is a requirement in Python that indicates what line(s) of code are included in the code block.
- The code blocks can be from one to many lines of code.
The flowchart shown here depicts the general form of the if-else statement as well. The execution flow represents the flow of the program up to the point of the if-else condition:. When the if condition is evaluated by the interpreter, the result will be either True or False. If the result is True then the execution flow moves into the code_block_based_on_true_condition and that code runs. If the condition result is False then execution flow moves into the code_block_based_on_false_condition and that code runs. When either is finished flow continues to the next statement after the indented code blocks.
Here's a code example of the if-else statement:
freezing = 32
print("Program started.")
current_temp = int(input("Enter the current temperature: "))
if current_temp > freezing:
print("The current temperature is above freezing.")
else:
print("The current temperature is freezing.")
print("Program complete.")
Running the program looks like this:
Enter the current temperature: 55
The current temperature is above freezing.

Notes:
- In this example, I included an initial print statement to display the start of the program to depict the execution flow.
- The user prompt collects a number for the current temperature from the user. Remember that anything entered at a user prompt defaults to a string, so I used the int() function to convert it to an integer.
- The if-else statement uses the greater than comparison operator (>) to compare the user-entered temperature with the freezing temperature value that I stored in a variable.
- Note that the print statements on lines 5 and 7 are indented, in Python this is how we indicate what lines of code are part of the code blocks "inside" of the decision statements (as indicated on the flowchart above).
- The last line is not indented, which means it is not part of the code_block inside of the decision statement.
For these problems, you may need to refer to the Comparison Operators and Logical Operators tables on the previous if statement page. You may also need the table operators on the Basic Operators page as well. These tables are also available in the Reference Materials.
Problem 1
if-else Statement: Write a Python program that prompts the user for an integer and then prints whether that number is even or odd. Hint: To determine if an integer is even or odd we can divide by 2. If the remainder is 0, the integer is even, if the remainder is 1, the integer is odd. We can use the Python modulus arithmetic operator to help us with this calculation.
Please enter an integer: 157
Your integer is odd.
Problem 2
if-else Statement: Write a Python program that enforces a string length requirement. This is common in circumstances like asking a new user to create a password. One of the criteria of passwords is often a minimum number of characters. Using the Python features we know so far, we can accept a string from a user, check the length of that string, and then use an if-else statement to confirm the string length meets the length requirements. For the sake of this example, let's say our minimum number of characters is 8.
Please enter a new Password of 8 Characters or More: cheese
Your proposed password of 'cheese' is too short, it must be 8 characters or more.
Now we have seen the simple if statement that handles one True condition and the if-else statement that handles a True and a False condition. Next, we'll expand on this again remainder learn the if-elif-else statement that can test multiple conditions.