Python if...
else Statement
In computer programming, the if statement is a conditional
statement. It is used to execute a block of code only when a specific
condition is met.
For example,
Suppose we need to assign different grades to students based on
their scores.
1. a student scores above 90, assign grade A
2. If a student scores above 75, assign grade B
3. If a student scores above 65, assign grade C
These conditional tasks can be achieved using the if statement.
Python if Statement
An if statement executes a block of code only when the specified
condition is met.
Syntax
if condition:
# body of if statement
Here, condition is a Boolean expression, such as number > 5, that
evaluates to either True or False.
• If condition evaluates to True, the body of the if statement is
executed.
• If condition evaluates to False, the body of the if statement will
be skipped from execution.
Let's look at an example.
Working of if Statement
Example: Python if Statement
number = int(input('Enter a number: '))
# check if number is greater than 0
if number >0:
print(number, 'is a positive number.')
Sample Output 1
Enter a number: 10
10 is a positive number.
If user enters 10, the condition number > 0 evaluates to True.
Therefore, the body of if is executed.
Sample Output 2
Enter a number: -2
If user enters -2, the condition number > 0 evaluates to False.
Therefore, the body of if is skipped from execution.
Python if...else Statement
An if statement can have an optional else clause. The else statement
executes if the condition in the if statement evaluates to False.
Syntax
if condition:
# body of if statement
else:
# body of else statement
Here, if the condition inside the if statement evaluates to
• True - the body of if executes, and the body of else is skipped.
• False - the body of else executes, and the body of if is skipped
Let's look at an example.
Working of if…else Statement
Example: Python if…else Statement
number = int(input('Enter a number: '))
if number > 0:
print('Positive number')
else:
print('Negative number')
Sample Output 1
Enter a number: 10
Positive number
This statement always executes
If user enters 10, the condition number > 0 evalutes to True.
Therefore, the body of if is executed and the body of else is skipped.
Sample Output 2
Enter a number: 0
Not a positive number
This statement always executes
If user enters 0, the condition number > 0 evalutes to False.
Therefore, the body of if is skipped and the body of else is executed.
Python if…elif…else Statement
The if...else statement is used to execute a block of code among two
alternatives.
However, if we need to make a choice between more than two
alternatives, we use the if...elif...else statement.
Syntax
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
Let's look at an example.
Working of if…elif…else Statement
Example: Python if…elif…else Statement
number = -5
if number > 0:
print('Positive number')
elif number < 0:
print('Negative number')
else:
print('Zero')
Output
Negative number
Examples Using Only if
1. Check if a number is positive
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
2. Check if a number is even
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
3. Check if age is 18 or above
age = int(input("Enter age: "))
if age >= 18:
print("You are an adult")
4. Print "Hello" if the user enters 1
a = int(input("Enter a number: "))
if a == 1:
print("Hello")
5. Check if a student passed (marks ≥ 40)
marks = int(input("Enter marks: "))
if marks >= 40:
print("You passed")
6. Check if a word contains 'a'
word = input("Enter a word: ")
if 'a' in word:
print("The word contains 'a'")
Examples Using if …. else
Even or Odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Adult or Minor
age = int(input("Enter your age: "))
if age >= 18:
print("Adult")
else:
print("Minor")
Divisible by 5
num = int(input("Enter a number: "))
if num % 5 == 0:
print("Divisible by 5")
else:
print("Not divisible by 5")
Vowel or Consonant
ch = input("Enter a character: ")
if [Link]() in "aeiou":
print("Vowel")
else:
print("Consonant")
Examples Using if …. elif …. Else
Largest of Three Numbers
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(a, "is the largest")
elif b >= a and b >= c:
print(b, "is the largest")
else:
print(c, "is the largest")
Grade Based on Marks
marks = int(input("Enter marks: "))
if marks >= 90:
print("A")
elif marks >= 75:
print("B")
elif marks >= 50:
print("C")
else:
print("Fail")
Python if-condition examples using logical operators:
and, or, and not.
Check if a number is between 10 and 20
num = int(input("Enter a number: "))
if num >= 10 and num <= 20:
print("Number is between 10 and 20")
num = int(input("Enter a number: "))
if num >= 1 and num <= 10:
print("Number is between 1 and 10")
elif num > 10 and num <= 50:
print("Number is between 11 and 50")
else:
print("Number is above 50")
Practice:
1. Check if a person is eligible to vote (age ≥ 18)
2. Check if two numbers are equal
3. Check age group(child, Teenager, Adult, Senior)
4. Check the student mark:
Mark > 90 => Grade – A
Mark 89 - 70 => Grade – B
Mark 69 – 50 => Grade – C
Mark 49 – 35 => Grade – D
Mark <35 =>Print(‘Fail’)