An if statement allows you to modify your program’s behavior based on a specific condition. The general form of an if statement is as follows:
| | if condition: |
| | block |
The condition is an expression, such as color != "neon green" or x < y. (Note that this doesn’t have to be a Boolean expression. As discussed in , non-Boolean values are treated as True or False when required.)
The if statements control the flow of execution
If the condition is true, the statements in the block are executed; otherwise, they are not. As with functions, the block of statements must be indented to show that it belongs to the if statement. If you don’t indent properly, Python will report an error, or worse, may execute the code that you wrote but do something you didn’t intend because some statements were not appropriately indented. You’ll briefly explore both problems in this chapter.
Here is a table of solution categories based on pH level (the measure of acidity or basicity of water-based solutions):
| pH | 0--4 | 5--6 | 7 | 8--9 | 10--14 |
| Category | Strong acid | Weak acid | Neutral | Weak base | Strong base |
You can use an if statement to print a message only when the pH level given by the program’s user is acidic:
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 6.0 |
| | >>> if ph < 7.0: |
| | ... print(ph, "is acidic.") |
| | ... |
| | 6.0 is acidic. |
Recall from , that you have to convert user input from a string to a floating-point number before doing the comparison. Additionally, you should provide a prompt for the user by passing a string to the function input; Python will display this string to inform the user of what information to type.
If the condition is false, the statements in the block aren’t executed:
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 8.0 |
| | >>> if ph < 7.0: |
| | ... print(ph, "is acidic.") |
| | ... |
| | >>> |
If you don’t indent the block, Python lets you know:
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 6 |
| | >>> if ph < 7.0: |
| | ... print(ph, "is acidic.") |
| | ... |
| | File "<python-input-0>", line 2 |
| | print(ph, "is acidic.") |
| | ^^^^^ |
| | IndentationError: expected an indented block after 'if' statement on line 1 |
Since you’re using a block, you can have multiple statements that are executed only if the condition is true:
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 6.0 |
| | >>> if ph < 7.0: |
| | ... print(ph, "is acidic.") |
| | ... print("You should be careful with that!") |
| | ... |
| | 6.0 is acidic. |
| | You should be careful with that! |
When you indent the first line of the block, the Python interpreter changes its prompt to ... until the end of the block, which is signaled by a blank line:
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 8.0 |
| | >>> if ph < 7.0: |
| | ... print(ph, "is acidic.") |
| | ... |
| | >>> print("You should be careful with that!") |
| | You should be careful with that! |
Of course, sometimes you encounter situations where a single decision isn’t sufficient. If multiple criteria need to be examined, you have several ways to handle it. One way is to use multiple if statements. For example, you might print different messages depending on whether a pH level is acidic or basic (if it’s exactly 7, then it’s neutral, and your code won’t print anything):
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 8.5 |
| | >>> if ph < 7.0: |
| | ... print(ph, "is acidic.") |
| | ... |
| | >>> if ph > 7.0: |
| | ... print(ph, "is basic.") |
| | ... |
| | 8.5 is basic. |
| | >>> |
Here’s a flowchart that shows how Python executes the if statements. The diamonds are conditions, and the arrows indicate what path to take depending on the results of evaluating those conditions:

Notice that both conditions are evaluated, even though you know that only one of the blocks can be executed.
You can merge both cases by adding another condition/block pair using the elif keyword (which stands for “else if”); each condition/block pair is called a clause:
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 8.5 |
| | >>> if ph < 7.0: |
| | ... print(ph, "is acidic.") |
| | ... elif ph > 7.0: |
| | ... print(ph, "is basic.") |
| | ... |
| | 8.5 is basic. |
| | >>> |
The difference between the two is that elif is checked only when the if condition above it is evaluated to False. Here’s a flowchart for this code:

This flowchart shows that if the first condition evaluates to True, the second condition is skipped.
If the pH is exactly 7.0, neither clause matches, so nothing is printed:
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 7.0 |
| | >>> if ph < 7.0: |
| | ... print(ph, "is acidic.") |
| | ... elif ph > 7.0: |
| | ... print(ph, "is basic.") |
| | ... |
| | >>> |
With the ph example, you achieve the same result using two if statements as you did with an if/elif statement.
This substitution is not always valid; for example, if the body of the first if changes the value of a variable used in the second condition, they are not equivalent. Here is the version with two ifs:
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 6.0 |
| | >>> if ph < 7.0: |
| | ... ph = 8.0 |
| | ... |
| | >>> if ph > 7.0: |
| | ... print(ph, "is acidic.") |
| | ... |
| | 8.0 is acidic. |
Here is the version with an if/elif:
| | >>> ph = float(input('Enter the pH level: ')) |
| | Enter the pH level: 6.0 |
| | >>> if ph < 7.0: |
| | ... ph = 8.0 |
| | >>> elif ph > 7.0: |
| | ... print(ph, "is acidic.") |
| | ... |
| | >>> |
As a rule of thumb, if two conditions are related, use if/elif instead of two ifs.
Multiple elif clauses can follow an if statement. This longer example translates a chemical formula into English:
| | >>> compound = input('Enter the compound: ') |
| | Enter the compound: CH4 |
| | >>> if compound == "H2O": |
| | ... print("Water") |
| | ... elif compound == "NH3": |
| | ... print("Ammonia") |
| | ... elif compound == "CH4": |
| | ... print("Methane") |
| | ... |
| | Methane |
| | >>> |
As you saw in the , if none of the conditions in a chain of if/elif statements are satisfied, Python does not execute any of the associated blocks. This isn’t always what you’d like, though. In the translation example, you probably want your program to print something, even if it doesn’t recognize the compound.
To do this, add an else clause at the end of the chain:
| | >>> compound = input('Enter the compound: ') |
| | Enter the compound: H2SO4 |
| | >>> if compound == "H2O": |
| | ... print("Water") |
| | ... elif compound == "NH3": |
| | ... print("Ammonia") |
| | ... elif compound == "CH4": |
| | ... print("Methane") |
| | ... else: |
| | ... print("Unknown compound") |
| | ... |
| | Unknown compound |
| | >>> |
An if statement can have at most one else clause, and it has to be the final clause in the statement. Notice that there is no condition associated with else:
| | if condition: |
| | if_block |
| | else: |
| | else_block |
Logically, that code is the same as this code (except that the condition is evaluated only once in the first form but twice in the second form):
| | if condition: |
| | if_block |
| | if not condition: |
| | else_block |