Conditional blocks using if, else and
elif.
Week-4 , Lecture-1
Course: Programming in Python By: Dr. Rizwan Rehman
Recap
• Installation of Anaconda.
• Learned about Spyder the Python
Development Environment.
• Basic Syntax, Variable and Data Types.
• Operators.
Course: Programming in Python By: Dr. Rizwan Rehman
• Program is a set of instructions.
• Program can be executed:
– Sequentially: where each instructions are
executed one after the another.
or
– We may also alter the sequence of execution of
the instructions.
Course: Programming in Python By: Dr. Rizwan Rehman
Decision Control
Sequential Instruction 1
Instruction 1 -----
Instruction 2 Test condition
-------
-------- True False
Instruction n
Course: Programming in Python By: Dr. Rizwan Rehman
• Thus the decision control statements also
known as the conditional branching
statements allows the programmer to jump
from one part of the program to another
depending upon a condition.
Course: Programming in Python By: Dr. Rizwan Rehman
• The conditional branching statement
supported by Python are as follows:
– if statement
– if-else statement
– Nested if statement
– if-elif-else statement
Course: Programming in Python By: Dr. Rizwan Rehman
True Test False
Expression
Statement Block 1 Statement Block 2
Course: Programming in Python By: Dr. Rizwan Rehman
Write a program to determine whether
a person is eligible to drive.
age=int(input("Enter the age : "))
if (age>=18):
print("You are eligible to drive")
Course: Programming in Python By: Dr. Rizwan Rehman
if-else example
age=int(input("Enter the age : "))
if (age>=18):
print("You are eligible to drive")
else:
print("You are not eligible to drive")
Course: Programming in Python By: Dr. Rizwan Rehman
Program to find Largest of two numbers.
f_num=int(input("Enter the first number : "))
s_num=int(input("Enter the second number : "))
if(f_num>s_num):
large = f_num
else:
large=s_num
print ("Largest is : ",large)
Course: Programming in Python By: Dr. Rizwan Rehman
elif example
marks=int(input("Enter the marks : "))
if (marks>=85 and marks<=100):
print("Distinction")
elif (marks>=75 and marks<=84):
print("A+ Grade")
elif (marks>=65 and marks<=74):
print("A Grade")
elif (marks>=55 and marks<=64):
print("B+ Grade")
else:
print("Enter a correct marks")
Course: Programming in Python By: Dr. Rizwan Rehman
Try yourself:
• Write a Python program to determine whether a
person is eligible to vote or not.
Course: Programming in Python By: Dr. Rizwan Rehman
Thank
You