In this tutorial, I will discuss about conditional statements in python. Every programming language has a conditional and looping statements which decide the flow of the program. In this tutorial we will discuss about if-else statements and their variations in python.
If-Else Statements in Python
So far we have discussed about keywords and identifiers, operators, data types and their inter conversion in python. These are the building blocks of a program.
But the main essence of a program depends on logic behind the program. In a program there are some decisions to be made to follow one route or other. This decision making is also called the flow control of a program.
If statement
Let’s suppose we want to write ‘Yes’ if user enters 1 and ‘No’ is user enters any other number, then this requires the program to make a decision either write Yes or No. This can be achieved using an if condition.
If condition in python means the same as in simple language, it can simply be translated as "If this then do that".
Syntax:
if condition:
#if this condition is met then only the below statement will be executed
statement
In python the block is indicated using indentation. Thus the body of if block starts with indentation and ends at the first unindented line. If the condition evaluates to True then it will execute the inner block statements otherwise it will ignore the if condition and continue the program flow.
Note that Python interprets all non-zero values as True and None or 0 as False.
Let’s take some examples to understand more clearly :
x = 4
y = 2
if x == 4:
print("x = 4")
if y<x:
print("y is less than x")
if x == y:
print("x is equal to y")
print('next line')
if x:
print("x")
if 0:
print('0')
print("Done")
x = 4
y is less than x
next line
x
Done
When we observe the output, we get to know that when the condition is evaluated to be False then the statement inside the if statement is ignored and program continues from the next line.
If-else statement
If we want to perform something else if the condition is false. For that python has else statement which is written just after the if block has ended.
The syntax for if – else statement is :
if condition:
statement1
else:
statement2
We can read the above code as : if condition is True then execute statement1 else execute statement2.
For example:
x = 10
if x <= 8:
print("x is less than or equal to 8")
else:
print("x greater than 8")
x greater than 8
In the above code we compare the value of variable x with 8 and conclude whether it is less than equal to or grater than 8.
In simple language, if x is less than or equal to 8 then print "x is less than or equal to 8" else print "x is greater than 8".
If-elif-else statement
Python also provides us with the if-elif-else statement using which we can evaluate multiple conditions and perform actions based on the condition which is True.
Syntax is as follows:
if condition1:
statement1
elif condition2:
statement2
else:
statement3
The above code says that if condition1 is True the execute statement1 else if condition2 is True then execute statement2 else execute statement3.
Example of if-elif-else block:
y = 10
if y < 5:
print("y is less than 5")
elif y == 5:
print("y is equal to 5")
else:
print("y is greater than 5")
y is greater than 5
In the above code, the value of variable y is compared to 5 and it concludes whether y is less than 5, equal to 5 or greater than 5.
Nested If Statement
We can have an if-elif-else statement inside another if-elif-else statement. This is called as nesting in programming language.
Any number of these statements can be nested inside one another. To figure out the level of nesting only indentation is used as it differentiates the if-elif-else blocks.
if condition1:
if condition2:
statement1
else:
statement2
else:
statement3
In the above code, when condition1 evaluates to True then it checks condition2 if it is True then statement1 is executed otherwise statement2 is executed. But when condition1 evaluates to False then it goes to else and statement3 is executed.
Example of nested if statements:
num = 0
if num >= 0:
if num == 0:
print("Number is equal to zero")
else:
print("Number is a positive number")
else:
print("Number is a negative number")
Number is equal to zero
Conclusion
In the above code, we check whether the number stored in variable num is negative, zero or positive using nested if-else statements.
In this tutorial we have discussed about if statement, if-else statement, if-elif-else statement and nested if-else statements with examples. These all statements are known as conditional statements which decide the flow of control of our python program.
If you have any questions please comment below also share your views and suggestions in the comment box.