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 is 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.
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:
a = 100
if a:
print ("1 - Got a true expression value")
print (a)
b = 0
if b:
print ("2 - Got a true expression value")
print (b)
print ("Done!")
To test your Python code from this lesson, use your code editor like PyCharm.
You can test the above example here! ➡Python Online Compiler
Output:
1 - Got a true expression value
100
Done!
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:
a = 15
b = 20
if a > b:
#False block
print("a is greater")
else:
#True block
print("b is greater")
You can test the above example here! ➡Python Online Compiler
Output:
b is greater
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:
a = 15
b = 15
if a > b:
#if statement
print("a is greater")
elif a == b:
#if the if statement is False, else if condition is executed
print("both are equal")
else:
#else statement is executed, if both if and elif statement if False
print("b is greater")
You can test the above example here! ➡Python Online Compiler
Output:
both are equal
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:
num = 5
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Output:
Positive number
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.
Example:
Below is an example of a Single line if-clause:
a = 20
if (a == 20): print("The value of a is 20")
You can test the above example here! ➡Python Online Compiler
Output
The value of a is 20
Best Practices for Using Decision-Making Statements
When using decision-making statements in Python, it is essential to follow some best practices to ensure clean and readable code:
- Use meaningful variable and function names.
- Keep the code within the decision-making statements concise and focused.
- Avoid deeply nested decision-making statements; refactor complex conditions into separate functions.
- Comment your code to explain the logic behind the conditions and decisions.
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