In this tutorial, we will learn about the conditional statements for decision making in Python. In Python, to check the condition, the if-else statement is used. The if-else statement in Python, helps the interpreter to execute the instructions based on the given condition. Different forms of the if-else statement are given below.
- if
- if-else
- if-elif-else
- nested if
The IF Condition
The if statement will use only when you want to execute some statements based on the condition (if the condition is True).
Syntax:
if Condition:
#statements to be executed
#if the condition is true
Example 1: Python program, to implement the IF statement.
marks=int(input("Enter Your Marks"))
if marks < 60:
print("You need more practice")
print("Your Marks:",marks)
You can write the if block in a single line also
if marks < 60: print("You need more practice")
print("Your Marks:",marks)
In the above code, statements with indentation will execute only, when the condition will True.
Indentation in Python
Indentation in Python refers to the spaces or tabs at the beginning of the statements. Indentation in Python treated as a block of code with the same spaces, instead of curly braces in other programming languages like (c, c++, Java). Generally, four spaces or tabs are used as an indentation.
Colon (:) – In Python
In Python, colon (:) is used to specify the start of a code block. The colon is used to signal the beginning of the function block.
If – Else in Python
If – else in Python will use only, when we have two different blocks of code to execute, based on two different situations. Either if or, else block will execute based on the condition using if.
Syntax:
if Condition:
# statements to be executed
# when the condition is true
else:
# statements to be executed
# when the condition is false
Example 2: Python program to check even or odd, using the if-else statement.
n=int(input("Enter a Number"))
if n%2==0:
print("Even")
else:
print("Odd")
Or you can write the above code as below also.
n=int(input("Enter a Number"))
if n%2==0: print("Even")
else: print("Odd")
If-elif-else in Python
If-elif-else in Python is useful when you have more than two conditions to test and want to execute different statements based on different conditions.
n=int(input("Enter a number : "))
if n>0:
print("Postive Number")
elif n<0:
print("Negative Number")
else:
print("Number is Zero")
It is allowed only one if, at the beginning, only one else at the end but, more then one elif block can possible within if-else if you have many conditions to test.
Nested IF
Whenever we use if condition as a statement within if or else block, then it is known as nested if.
a=int(input("Enter First number : "))
b=int(input("Enter Second number : "))
c=int(input("Enter Third number : "))
if a>b:
if a>c:
print("First Largest")
else:
print("Third Largest")
elif b>c:
print("Second Largest")
else:
print("Third Largest")
Use of ‘and’ Operator with if
a=int(input("Enter First number : "))
b=int(input("Enter Second number : "))
c=int(input("Enter Third number : "))
if a>b and a>c:
print("First Largest")
elif b>c:
print("Second Largest")
else:
print("Third Largest")