contact@eeehub.in +919470012104

Conditional statements in Python

Normally, the execution of the statements will be one after another. But in some case if we want to skip some line of code. Then, conditional statements help us to do that.

Conditional Statements in Python perform different computations or actions depending on whether a specific Boolean constraint evaluates to True or False.  So, conditional statements help us to execute a set of statements which will execute on the basis of the result of some expression or condition.

Conditional statements in python are below:

  1. If
  2. If..else
  3. If..elif..else
  4. Nested if

If Statement:


An if statement consists of a Boolean expression followed by one or more statements.

if expression:
statement(s)

The if statement contains a logical expression using which the data is compared and a decision is made based on the result of the comparison.



>>> a = 2 # 2 is assign to variable a
>>> if(a ==2): # checking a is equal to 2. Yes! So it execute the statement
... print("a is equal to 2")
...
a is equal to 2
>>> if(a ==3): # checking a is equal to 3. No! So it does execute the statement
... print("a is equal to 2")
...
>>>


If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed. In Python, statements in a block are uniformly indented after the : symbol. If boolean expression evaluates to FALSE, then the first set of code after the end of block is executed.



>>> if(True):
... print("if condition evaluate as True")
...
if condition evaluate as True
>>> if(False):
... print("if condition evaluate as False")
...
>>>


If...else Statement:


An else statement can be combined with an if statement. An else statement contains a block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at the most only one else statement following if.



>>> if(True):
... print("if condition evaluate as True")
... else:
... print("if condition evaluate as False")
...
if condition evaluate as True
>>> if(False):
... print("if condition evaluate as True")
... else:
... print("if condition evaluate as False")
...
if condition evaluate as False


If..elif..else statements:


The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. Similar to the else, the elif statement is optional. However, unlike else, for which there can be at the most one statement, there can be an arbitrary number of elif statements following an if.



>>> if(True):
... print("if condition evaluate as True")
... elif(True):
... print("Condition evalute as True")
... else:
... print("Condition evaluate as False")
...
if condition evaluate as True
>>> if(False):
... print("if condition evaluate as True")
... elif(True):
... print("Condition evaluate as True")
... elif(False):
... print("Condition evaluate as True only")
... else:
... print("Condition evaluate as False")
...
Condition evaluate as True


Nested If  statements:


There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct. In a nested if construct, you can have an if...elif...else construct inside another if...elif...else construct.



>>> if(True):
... print("If condition evaluate as True")
... if(True):
... print("Nested condition evaluate as True")
... else:
... print("Nested condition evaluate as False")
... else:
... print("Condition evaluate as False")
...
If condition evaluate as True
Nested condition evaluate as True