Python has several types of decision-making statements: if, if-else, if-elif-else, and nested statements
. These statements are based on conditions that the program checks. If the condition is true, a set of statements are executed. If it is false, a different set of statements are executed.
Here is an example of a typical structure of decision-making statements in Python.
Any value that is not zero or null is TRUE in the Python programming language. If the value is either zero or null, it is FALSE.
In order to test your Python code from this lesson, use your code editor like PyCharm. If you wish to run the code online, we also have an Online Compiler in Python.
If you want to learn more about Python or are just starting out, our Python Tutorial for Beginners is a great resource.
Types of Decision-Making Statements in Python
Below are the four types of Decision-Making Statements in Python:
- If statements
- If-else Statement
- If-Elif-Else Statement
- Nested If Statement
Python If Statement
The Python If Statement is the same as in other languages. The if statement has a logical expression that compares two sets of data and makes a decision based on the result.
If Statement Syntax
The following code below is the Python If Statement Syntax:
if expression:
#execute your code
The block of statements contained inside the if statement is executed if the boolean expression evaluates to TRUE. The first set of instructions following the end of the if statement(s) is executed if the boolean expression evaluates to FALSE.
If Statement Flow Diagram
Here is the If Statement Flow Diagram:
Example:
Python If-else Statement
The Python if-else statement is used to run both the true and false parts of a condition. If the condition is true, the code in the if block is executed. If it is false, the code in the else block is executed.
If-else Statement Syntax
The following code below is the Python If-else Statement Syntax:
if expression:
#execute your code
else:
#execute your code
When the test condition is True, the if-else statement analyzes the test expression and executes the body of the if.
The else clause’s body is executed if the condition is False. The blocks are separated by indentation.
If-else Statement Flow Diagram
Here is the If-else Statement Flow Diagram:
Example:
Python If-elif-else Statement
Python If-elif-else statement checks the condition of the if statement. If the condition is False, then the elif statement is evaluated. If the elif condition is False, then the else statement is evaluated.
If-elif-else Statement Syntax
The following code below is the If-elif-else Statement Syntax:
if expression:
#execute your code
elif expression:
#execute your code
else:
#execute your code
With the elif statement, you can check if more than one expression evaluates to TRUE and run a block of code when one of them does.
The elif statement is a Python keyword that can be used instead of else if to add another condition to a program. We call this “chained conditional.”
If-elif-else Statement Flow Diagram
Here is the Python If-elif-else Statement Flow Diagram:
Example:
Python Nested If Statement
Another Decision-Making Statement is the Python Nested If statement is one that has an if statement inside of another if statement. This happens when you need to filter a variable more than once.
Nested If Statement Syntax
The following code below is the Python Nested If Statement Syntax:
if expression:
if nested expression:
#execute your code
else:
#execute your code
else:
#execute your code
When you have If statements inside of each other, you should always pay attention to the indentation to show what each statement is about. You can have as many levels of nesting as you want, but that makes the program less efficient and harder to read and understand. So, you should always try to use as few nested IF statements as possible.
Nested If Statement Flow Diagram
Here is the Nested If Statement Flow Diagram:
Example:
Single Statement Suites
In Single Statement Suites, programmers can place the block of an executable statement of an if-clause on the same line as a header statement if the block only has one line.
Below is the example of a Single line if-clause:
Summary
In summary, you learned about if statements, if-else statements, if-elif-else statements, and nested if statements.
I hope that this tutorial helped you understand how to use decision-making statements in Python. Check out our list of Python Tutorial Topics if you missed any of our previous lessons.
You are one step closer to creating stronger, more effective codes now that you are aware of what decision-making statements in Python are. In your code, start implementing all the if-else statements you have studied today.
In the next post, “Python Loops,” you’ll learn about the different kinds of Python loops and how to use them.
Python Operators
Python Loops and Control Statements