Python IF,IF ELSE,ELIF AND NESTED IF

Rayman Sodhi
4 min readOct 2, 2020

What are Conditional Statements?

Conditional Statement in Python perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false. Conditional statements are handled by IF statements in Python.

In this tutorial, we will see how to apply conditional statements in Python.

1)Python IF statement:

>If statement is one of the most commonly used conditional statement in most of the programming languages.

>It decides whether certain statements need to be executed or not. If statement checks for a given condition, if the condition is true, then the set of code present inside the if block will be executed.

>The If condition evaluates a Boolean expression and executes the block of code only when the Boolean expression becomes TRUE.

Syntax:

If (Boolean expression): Block of code  #Set of statements to execute if the condition is true

Here, the condition will be evaluated to a Boolean expression (true or false). If the condition is true, then the statement or program present inside the if block will be executed and if the condition is false, then the statements or program present inside the if block will not be executed.

Let’s see how it looks on a flow chart:

Example:

Num = 5

If(Num < 10):

print(“Num is smaller than 10”)

print(“This statements will always be executed”)

Output: Num is smaller than 10.

2) Python Else statement:

>The statement itself tells that if a given condition is true then execute the statements present inside if block and if the condition is false then execute the else block.

>Else block will execute only when the condition becomes false, this is the block where you will perform some actions when the condition is not true.

>If-else statement evaluates the Boolean expression and executes the block of code present inside the if block if the condition becomes TRUE and executes a block of code present in the else block if the condition becomes FALSE.

Syntax:

if(Boolean expression): 
Block of code #Set of statements to execute if condition is true
else:
Block of code #Set of statements to execute if condition is false

Here, the condition will be evaluated to a Boolean expression (true or false). If the condition is true then the statements or program present inside the if block will be executed and if the condition is false then the statements or program present inside else block will be executed.

Let’s see the flowchart of if-else:

Example:

num = 5

if(num > 10):

print(“number is greater than 10”)

else:

print(“number is less than 10”)

print(“This statement will always be executed”)

Output: number is less than 10.

2) Python ELIF statement:

>In python, we have one more conditional statement called elif statements. Elif statement is used to check multiple conditions only if the given if condition false.

>It’s similar to an if-else statement and the only difference is that in else we will not check the condition but in elif we will do check the condition.

>Elif statements are similar to if-else statements but elif statements evaluate multiple conditions.

Syntax:

if (condition):
#Set of statement to execute if condition is true
elif (condition):
#Set of statements to be executed when if condition is false and elif condition is true
else:
#Set of statement to be executed when both if and elif conditions are false

Example:

num = -7

if (num &amp;gt; 0):

print(“Number is positive”)

elif (num &amp;lt; 0):

print(“Number is negative”)

else:

print(“Number is Zero”)

Output: Number is negative

2) Python Nested IF-ElSE statement

>Nested if-else statements mean that an if statement or if-else statement is present inside another if or if-else block.

>Python provides this feature as well, this in turn will help us to check multiple conditions in a given program.

>An if statement present inside another if statement which is present inside another if statements and so on.

Nested if Syntax:

if(condition):
#Statements to execute if condition is true
if(condition):
#Statements to execute if condition is true
#end of nested if
#end of if

The above syntax clearly says that the if block will contain another if block in it and so on. If block can contain ’n’ number of if block inside it.

Example: 1

num = 5

if(num >0):

print(“number is positive”)

if(num<10):

print(“number is less than 10”)

Output:

number is positive

number is less than 10

Thank you for reading!!

For more articles refer to :

https://medium.com/@raymansodhi28

--

--

Rayman Sodhi

Sophomore in Information Technology,Tech Enthusiast ,Web -developer ,Keen learner and Observer.