python if
python if
if statement
if else statement
if-elif-else Statement
nested if statement
if statement
In Python, the general format of the if statement:
if condition:
statement
statement
etc.
If the condition is true, statement is executed; otherwise it is skipped
Program (absolute_number.py)
if number < 0:
number = - number
#1.
Enter a number:-20
The absolute value of number is 20
#2
Enter a number:100
The absolute value of number is 100
if-else Statement
The syntax looks like this:
if condition:
statement 1
etc.
else:
statement 2
etc.
The given condition is evaluated first. If the condition is true, statement1 is
executed. If the condition is false, statement 2 is executed.
Program (even_odd.py)
if number%2 == 0:
print(number, 'is even.')
else:
print(number, 'is odd.')
Output:
#1
Enter number: 15
15 is odd.
#2
Enter number: 20
20 is even.
if-elif-else Statement
When there are more than two possibilities and we need more than two
branches, if-elif-else statement is used. Here is the general format of the if-
elif-else statement:
if condition_1:
statement
statement
etc.
elif condition_2:
statement
statement
etc.
else:
statement
statement
etc.
elif is an abbreviation of "else if." Again, exactly one branch will be executed.
There is no limit of the number of elif statements, but the last branch has to be
an else statement.
Program (greater_number.py)
if x < y:
print (x, "is less than", y)
elif x > y:
print (x, "is greater than", y)
else:
print (x, "and", y, "are equal")
Output
#1
Enter first number:25
Enter second number:36
25 is less than 36
#2
Enter first number:36
Enter second number:25
36 is greater than 25
#3
Enter first number:36
Enter second number:36
36 and 36 are equal
Nested Decision Structures
One conditional can also be nested within another. We could have written the
above example as follows:
Program (nested_example.py)
if x == y:
print (x, "and", y, "are equal")
else:
if x < y:
print (x, "is less than", y)
else:
print (x, "is greater than", y)
Output
#1
Enter first number:25
Enter second number:36
25 is less than 36
#2
Enter first number:36
Enter second number:25
36 is greater than 25
#3
Enter first number:36
Enter second number:36
36 and 36 are equal
Solved Examples
Question 1
Write a program which asks user to enter price and quantity of a product. Your
program should calculate and display the the bill amount as price * quantity. If
bill amount is more than 2000 discount of 20% on bill amount should be
subtrated from bill.
Program
print('Bill amount:',amount)
print('Discount:',discount)
print('Your net bill amount is',net_amount)
Output
marks Grade
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
Program
if marks>=90:
grade = 'A'
elif marks>=80:
grade = 'B'
elif marks>=70:
grade = 'C'
elif marks>=60:
grade = 'D'
else:
grade = 'F'
Review Questions
Short Answer
if x > 100:
y = 20
z = 40
if a < 10:
b = 0
c = 1
if a < 10:
b = 0
else:
b = 99
x = 14
print('Fun with condition')
if x > 5:
print('I am greater than 5')
if x > 10:
print('I am greater than 10')
if x > 15:
print('I am greater than 15')
x = 14
print('Fun with condition')
if x > 5:
print('I am greater than 5')
elif x > 10:
print('I am greater than 10')
elif x > 15:
print('I am greater than 15')
else:
print('Who am I?')
Fun with condition
I am greater than 5
x = 9
if x >= 10:
print('I am 10')
print('Or greater than 10')
print('Hello Pyhon!')
if x >= 5:
print('I am 5')
print('Or greater than 5')
Hello Pyhon!
I am 5
Or greater than 5
age = 18
if age > 10:
print('More than ten')
if age < 20:
print('Less than twenty')
print('All done')
if x < 2:
print('Below 2')
elif x < 20:
print('Below 20')
elif x < 10:
print('Below 10')
else:
print('Something else')
print('Below 10')