Decision Structures
Sometimes the program needs to be executed depending upon a particular
condition. Python provides the following statements for implementing the
decision structure.
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)
#This program illustrates if statement.
number = int(input("Enter a number:"))
if number < 0:
number = - number
print("The absolute value of number is", number)
Output
#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)
# This program test a number is even or odd.
number = int(input('Enter number: '))
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)
# This program determines greater number.
x = int(input("Enter first number:"))
y = int(input("Enter second number:"))
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)
# This program determines greater number.
x = int(input("Enter first number:"))
y = int(input("Enter second number:"))
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
price = int(input('Enter price of product: '))
quantity = int(input('Enter quantity: '))
amount = price * quantity
if amount > 2000:
discount = amount*0.20
else:
discount = 0
net_amount = amount - discount
print('Bill amount:',amount)
print('Discount:',discount)
print('Your net bill amount is',net_amount)
Output
Enter price of product: 550
Enter quantity: 4
Bill amount: 2200
Discount: 440.0
Your net bill amount is 1760.0
Question 2
The marks obtained by a student in computer science is input by the user.
Your program should display the grade. The student gets a grade as per the
following rules:
marks Grade
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
Program
marks = int(input('Enter your marks in CS: '))
if marks>=90:
grade = 'A'
elif marks>=80:
grade = 'B'
elif marks>=70:
grade = 'C'
elif marks>=60:
grade = 'D'
else:
grade = 'F'
print('Your grade is',grade)
Output
Enter your marks in CS: 67
Your grade is D
Review Questions
Short Answer
1. Construct a logical expression to represent each of the following conditions:
(i) score is greater than or equal to 80 but less than 90
score >= 80 and score < 90
(ii) answer is either ‘N’ or ‘n’
answer == 'N' or answer == 'n'
(iii) n is between 0 and 7 but not equal to 3
n >= 0 and n <= 7 and n != 3
2. Write an if statement that assigns 20 to the variable y and assigns 40 to the
variable z if the variable x is greater than 100.
if x > 100:
y = 20
z = 40
3. Write an if statement that assigns 0 to the variable b and assigns 1 to the
variable c if the variable a is less than 10.
if a < 10:
b = 0
c = 1
4. Write an if-else statement that assigns 0 to the variable b if the variable a is
less than 10. Otherwise, it should assign 99 to the variable b.
if a < 10:
b = 0
else:
b = 99
5. What will be the output of the following code:
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')
Fun with condition
I am greater than 5
I am greater than 10
6. What will be the output of the following code:
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
7. What will be the output of the following code:
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
8. What will be the output of the following code:
age = 18
if age > 10:
print('More than ten')
if age < 20:
print('Less than twenty')
print('All done')
More than ten
Less than twenty
All done
9. What will never print regardless of the value of x?
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')