Python decision making statements
In
this lecture python decision-making statement tutorial, we will master all
about decision-making constructs like if, if-else, if elif, etc used in python
programming. Also, we will discuss the syntax and flowchart for the
decision-making statement with examples.
To
understand in an easy way let's take the real-world scenario of Traffic
Signals. What happens when the traffic signal lights red? All vehicles will
stop. When it lights green, vehicles will start to drive. Thus the flow of
vehicles on road is controlled by Traffic signals.
Similarly
in the programming world, Decision-making constructs are used to control the
flow of the program. It predicts the outcome and executes the program code
based on the outcome. The outcome of the conditional statement is a boolean
value like True or False.
Note: Python reads non-zero values as True while None and
zero as False.
if
conditional Statements in python
In
plain English, the word if denotes
a condition or a supposition. Similarly in the python programming
language if is a decision-making
construct that refers to a conditional statement.
Among
the decision-making, constructs if is
the basic one. If evaluates
the expression applied to it and checks whether the expression meets the
condition or not. When the expression meets the condition the result will be
True and the python will execute the block of statements that follow the
result. Otherwise, it skips the block and jumps to the statements after the
block.
Syntax
of IF conditional statement in python
if condition:
#body of if
Flowchart
of IF conditional statement in python
if
conditional statement can be best visualized using the flowchart given below.
![]() |
Example: Program to show the working of “if”
a = 20;
if a>10:
print('a is greater than 10')
print('Bye!!!!')
Output:
A
is greater than 10
Bye!!!!
Here
in the above example condition a>10 is
evaluated to find out whether the variable a holds a value greater
than 10 or not. In our case
variable ‘a’ holds the
value 20 which is greater than 10,
hence the condition is True. So the
program control is directed to the very next statement of if and prints the
result as a is greater than 10. Then the program control is shifted
to the statement out of indentation of ‘if’.
Note: Decision-making statement relies on indentation.
The
limitation of a single if statement is that it only executes the statement
following the True. The false case is not addressed in a single if statement.
If
else Statements in python
To
overcome the limitation of a single ‘if’ statement, we use the
‘else’.statement. To be specific in the if-else statement python evaluates the
condition and outputs the result as follows:
- if
the condition is True, the body of if gets executed
- if
the condition is False, the body of else gets executed.
Syntax
of if-else conditional statement in python
if Condition:
#execute body of if
else:
#execute body of else
Flowchart
of if else statements in python
Now
we can check how if else statement works in a python program
Example: If else program
a = 20;
if a<10:
print('a is less than 10')
else:
print('a is greater than 10')
Output:
a
is greater than 10
The
above example evaluates the condition a<10.
If the condition evaluates to True ,
then it prints a is less than 10 or
if the condition evaluates to False then the program control shifts to else
part and prints a is greater than 10.In
this scenario variable ‘a’ contains the value 20 which is greater than 10, so
the outcome is else part statement ie. a is greater than 10.
IF
ELIF Statements
This
is a bit complex version of if else. In this elif is
a python keyword referring to else..if.
In a nutshell, if elif else is a chain of conditional statements. It is
sometimes referred to as if elif else ladder.
Syntax
of IF ELIF ELSE conditional statement in python
if Condition1:
#execute body of if
elif Condtion2:
#execute body of elif
elif Condition3:
#execute body if elif
else:
#execute body if else
Flow
chart of elif
Here
is the example of if..elif..else statements.
Example: if...elif...else
a = 10
b = 20
if a==b:
print('a is equal to b')
elif a < b:
print("a is less than b")
else:
print("a is greater than b")
print('Exit')
Output:
a
is less than b
Exit
In
the above example, condition a== b is
evaluated. If a and b holds the same value then the body of if the part is
executed and prints a is equal to b.
But in our case, the condition evaluates to False and hence the else if the
part is executed where again evaluates the condition whether a is less than b
or not. As variable a holds the value 10 and b holds the value 20, the
condition evaluates to true and hence prints a is less
than b as the result.
In
case the a is 20 and b is 10 then the condition fails and else part gets
executed with result a is greater than b.
Nested
If
In
some situations even though the expression evaluates to True, we need to check
other conditions also. In such scenarios, we make use of nested if despite the
fact of how complex it is. The complexity of a nested if depends on the level
of nesting included.
An
if statement containing another if statement is known as nested if.
We can nest a simple if or if else statement or if elif else statements. This
is purely a programmer choice and hence there is no generalized syntax or flow
chart available. However, for your better understanding consider the below
syntax and flow chart.
Syntax
of Nested if
if Condition1:
#execute body of if(condition1)
if Condtion2:
#execute body of if (condition 2)
elif Condition3:
#execute body if elif
else:
#execute body of else (nested)
else:
#execute body of else
Flowchart
of Nested if
Example: Nested if
a = 25
b = 20
if a > 0:
if a==b:
print('a is equal to b')
elif a < b:
print("a is less than b")
else:
print("a is greater than b")
else:
print("a is less than 0")
print('Exit')
Output:
a
is greater than b
Exit
In
the above example, initially a >0 is
evaluated.
if a>0 is true then the program
control moves to nested if. In this case, a holds value 25 so a is greater than
zero and moves to nested if.
Now evaluates the condition a==b whose
outcome is a false for our scenario as a=25 and b=20.
Hence the flow shifts to elif, where condition a<b< code=""
style="box-sizing: border-box;"> is checked. So our case
prints a is greater than b.</b<>