Decision Making Statements: If, If..else and Nested If..else
Introduction
Do you ever find yourself making decisions in Python? Perhaps you want to take certain steps based on a condition or the input that is given by a user. Whatever the case may be, having decision-making statements available is an incredibly powerful tool when programming with Python! Another decision which may be important for you if whether you need a Python certification course after reading this article. In this article, we will discuss how to make use of these statements and how they can help your coding and make it easier to maintain and read. So join me as we explore together decision-making statements in Python!
What are Decision Making Statements in Python?
Decision making statements in python are an essential tool for any programmer. They allow any program to make intelligent and informed decisions by evaluating information before executing that particular code. Decision making statements can direct a program to execute on the based of instructions if a certain condition is True or another if it's False. Different decision making structures can be used such as if statements, else-if statements, and nested if else statements for decision making in python programming language. Each decision structure has its own benefits and applications; however, decision making statements provide an invaluable tool for programming complex conditions in Python.
Types of decision-making in python
There are three types of decision making statements in python language, those are
- If statements in python
- If else statement in python
- Nested if else statement in python
If statements in python
If statements are one of the most important aspects of any programming language, and that is true in Python programming language as well. If statements allow for conditions to be set within code so that it behaves differently depending on whether or not certain conditions are true. If statements help to make any program more efficient by allowing common tasks by accomplished with less code and also making it much easier to debug errors.
Syntax
if expression:
statement(s)
Example
var1 = 100
if var1:
print "1 - Got a true expression value"
print var1
var2 = 0
if var2:
print "2 - Got a true expression value"
print var2
print "Good bye!"
Output
1 - Got a true expression value
100
Good bye!
If else statement in python
If else statements in a python programming language are extremely useful when making decisions. If certain conditions are met then a certain action will occur. This is the essential idea behind using if else statements. This statement can be used to enhance the logic and branching of an existing program or to create branching within a program that does something different depending on how it is invoked. If else statements are very powerful tools for controlling the flow of programs, so it is important to understand how they are used in python.
Syntax
if expression:
statement(s)
else:
statement(s)
Example
var1 = 100
if var1:
print "1 - Got a true expression value"
print var1
else:
print "1 - Got a false expression value"
print var1
var2 = 0
if var2:
print "2 - Got a true expression value"
print var2
else:
print "2 - Got a false expression value"
print var2
print "Good bye!"
Output
1 - Got a true expression value
100
2 - Got a false expression value
0
Good bye!
Nested if else statement in python
Nested if else statements in python enable complex decisions to be coded into a single statement. Nested if else statements serve as an effective decision-making tool in python programming by allowing developers to code any number of decisions or conditions within an if else statement. Depending on the output of the conditional checks, nested if else statements can take one of several paths, directing the program towards different outcomes. Nested if else statements can help keep high levels of complexity from escalating too quickly and make the code easier to read and troubleshoot.
Syntax
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
else:
statement(s)
Example
var = 100
if var < 200:
print "Expression value is less than 200"
if var == 150:
print "Which is 150"
elif var == 100:
print "Which is 100"
elif var == 50:
print "Which is 50"
elif var < 50:
print "Expression value is less than 50"
else:
print "Could not find true expression"
print "Good bye!"
Output
Expression value is less than 200
Which is 100
Good bye!
Summary
By understanding how to use if statements, if else statements and nested if else statements in Python programming you can give your applications the ability to make basic decisions. You’ll be able to take user input and respond accordingly, compare data, and execute different codes depending on the conditions met. These Decision Making Statements are an important part of learning Python and will help you write more sophisticated programs down the road. As always, we welcome any questions or feedback in the comments below! Happy Coding!
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.