While programming, there are times when we need to make decisions based on certain values and conditions.
In Python, we use the if
statement to create programs that can
make decisions.
The syntax of the if
statement is:
if test_condition:
statement(s)
If test_condition
is True
, the body of the
if
statement is executed.
However, if test_condition
is False
, the body of the
if
statement is skipped from execution.
Example: Check if student passed the exam
Suppose you are a university student and to pass the examination you need to score 50 or more. Let's look at a program to check if you passed the exam.
score = int(input("Enter a number: "))
if score >= 50:
print("You have passed your exams.")
print("Congratulations!")
Output
For a number greater than or equal to 50:
Enter a number: 75 You have passed your exams. Congratulations!
Otherwise (number smaller than 50):
Enter a number: 35
As we can see, there is no output as the if
block is skipped. We
can add one more if statement to handle this as:
score = 35
if score >= 50:
print("You have passed your exam.")
print("Congratulations!")
if score
Output
Sorry, you have failed your exam.
Python if...else statement
The if statement can have an optional else clause to run when
test_condition
is False
. The syntax of
if..else
is:
if test_condition:
statement(s)
else:
statement(s)
If the test_condition
evaluates to True
,
- the code inside
if
is executed - the code inside
else
is skipped
If the test_condition
evaluates to False
,
- the code inside
else
is executed - the code inside
if
is skipped
Let's see an example,
score = 35
if score >= 50:
print("You have passed your exam.")
print("Congratulations!")
else:
print("Sorry, you have failed your exam.")
Output
Sorry, you have failed your exam.
Here, since the if score >= 50
evaluated to
False
, the else
block is executed.
Python if...elif...else statement
The if
statement can also have multiple elif
clauses
to handle more than two test cases. The syntax of
if...elif...else
is:
if test_condition1:
statements_1
elif test_condition2:
statement_2
else:
statements_3
Here, if test_condition1 evaluates to True
, code
statement_1 is executed.
However, if test_condition1 evaluates to False
,
code test_condition2 is executed.
If test_condition2 is True
,
statement_2 is executed. But if
test_condition2 is False, statement_3 is
executed.
Example: Python if...elif...else statement
score = 105
if score > 100 or score = 50:
print("You have passed your exam.")
print("Congratulations!")
else:
print("Sorry, you have failed your exam.")
Output
Score is invalid.
Here, since score > 100 or score < 0
evaluates to
True
, the statement inside if
is executed.
Python Nested if Statement
In Python, we can also use an if
statement inside another
if
statement. This is called a
nested if statement.
The syntax of nested if statement is:
if test_condition1:
statements_1
if test_condition2:
statement_2
Note:
- We can nest multiple layers of
if
statements. -
We can add
else
andelif
to the innerif
statement. -
We can also add inner
if
statement inside outerelse
orelif
if needed.
Example: Python Nested if Statement
Let's check when the number is positive, either it is odd or even.
num = 5
if num > 0:
if num % 2 == 0:
print('Number is even')
else:
print('Number is odd')
else:
print('Number is a non-positive integer')
Output
Number is odd
Here, the program will first check if num is greater than 0. If the num is greater than 0, the program will again check another condition, if the num is divisible by 2.
-
If num is divisible by 2, the program will print
'Number is even'
-
If num is not divisible by 2, the program will
print
'Number is odd'
However, if num is not greater than 0 in the first
place, the program will directly execute the outer else
block.
Recommended Reading: Python match...case