An if statement is a structure for decision making in Python. Python can execute line(s) of code based on a condition (sometimes this is called conditional statement)
By default Python runs lines of code sequentially, first line 1, then line 2, then line 3 etcetera. By using an if statement you can change this.
Lines of code inside the code blocks must be indented with four spaces at all times (not tabs)
Related Course: Complete Python Programming Course & Exercises
If statement syntax
The most basic form of an if statement is:
if<expr>: |
In this form:
<expr>
is an expressions that is evaluated<code>
one or more lines of code. These must be indentend by four spaces.if the
<expr>
yields True: the code is executed- If the
<expr>
yields False: the code is skipped
Example: Python if statement
The example below shows an if statement.
The user types x (an integer). Only if the value is greater than zero, it is shown.
x = int(input("Enter x: ")) |
An example run is shown below:
Enter x: 5 |
If you type zero or lower, nothing is shown:
Enter x: 0 |
Related Course: Complete Python Programming Course & Exercises
If else
The else
keyword can be added to the if statement. The block of code after the else, is only executed if <expr>
is False.
x = int(input("Enter x: ")) |
Note lines of code are in the block of code, if they have four spaces in front of them.
Related Course: Complete Python Programming Course & Exercises
elif clause
There’s also the elif
clause. Short for else if. This lets you check multiple conditions one by one.
if<expr>: |
Any number can be added. The example shows the checking of multipe conditions:
"Vivian" name = |
You can optionally add another else at the end. Only one line(s) of code will be executed, but which depends on the condition.
Comparison operators
Python if statements usually have one of the following operators:
>
greater than<
smaller than==
equals!=
is not>=
greater than or equal<=
smaller than or equal
For example,
The condition x > 100
means only “the value of x should be greater than 100”.
The condition x != 2
means “the value of x should not be two”.
The program below tests if the value of x is equal to four.
x = int(input("Tell X ")) |
The program always prints ‘End of program’ (normal sequential execution).
The text ‘You guessed correctly!’ will only be printed if the variable x equals four.
Nested if statements
If statements can be within if statements. That structure is called a nested if.
As a general rule you should not do this more than two times, because it gets very confusing to read.
The syntax is:
if<expr>: |
For example the if statement below:
if x > 0: |
The nested statements should have 8 spaces, because they are within the second if statement.
You can still use elif
and else
within a nested if.
if x > 0: |
One line if statements
Usually an if statements spans multiple lines likes so:
if<expr>: |
But you can write an if statement on a single line:
if<expr>:<statement> |
There can even be multiple statements, if you separate them by a semicolon.
if<expr>:<statement>;<statement>;<statement> |
The example below shows the single line if statement:
3 x = |
You can do single line if statements like that:
3 x = |
Another example:
2 x = |
Related Course: Complete Python Programming Course & Exercises
Boolean operators
You can apply boolean operators to the condition. This way you can combine if statements.
if<expr> and<expr>: |
A boolean or is also possible:
if<expr> or<expr>: |
Python if multiple conditions
What if you want to have multiple conditions? One way to deal with that is if you can type a lot of if statements like so:
x = 8 |
But this becomes spaghetti code, not easy to read and might get confusing quickly. What you want to do is use the elif and else keywords.
x = 8 |
These codes will do exactly the same, but the above version is preferred because of readability and maintainability.
Summary
Normal execution is line by line, also called sequential
If statements select which code to execute.
If statements are decision makers. Only execute code if condition is True.
the
if
statement is used when you need to execute code only if the condition is Truethe
else
clause is used when you want to run code if one condition failsthe
elif
clause lets you specify other conditionsAn if statement is a type of control structure. A control structure directs the order of execution, in this case you want to run code only if a condition is True.
If you are a Python beginner, then I highly recommend this book.