Python Programming-20CS31P
WEEK -3
Control flow: The program instructions are executed sequentially i.e, one after the
Control Flow: after.
Conditional • If you want to skip some instructions or you want to change the flow of the
blocks program, it is done by conditional blocks.
Conditional blocks / statements in Python are
• if statement
• elif statement
• else statement
• Nested if
• The comparison operators such as ==, !=, > Ex: a=30
if statement , <, <=,>= are used to form conditions. b=20
• Ex: a == b, a != b, a < b, a > b etc.. if a > b:
• These conditions are used in if statement. print(“a is greater than b”)
In the above example
• if is keyword
• a>b is the condition to be tested
How if works?
• if the condition a>b is true, then the statement print(“a is greater than b”) is
executed .
• if the condition a>b is false, then the statement print(“a is greater than b”) is not
executed .
General format
if condition:
Statement/ block of statements
Statement
• if the condition is true, then the Statement/ block of statements are executed .
• if the condition is false, then the Statement/ block of statements are skipped and
statement after the if block is executed.
Ex: n1=30
n2=20
if n1 > n2:
print(“n1 is greater than n2”)
Ex: if 5 == 5
print(“both are equal”)
Ex: n1=30 , n2=20
if n1 != n2:
print(“n1 is not equal to n2”)
SHESHADRI A V, DEPT OF CSE, GPT CHN Page 1
Python Programming-20CS31P
• elif statement is used to test another Ex: n1=10
elif statement condition, when the previous if statement n2=10
is false. if n1 > n2:
General format print(“n1 is greater than n2”)
if condition1: elif n1 == n1:
statement 1 print(“n1 is equal to n2”)
elif condition2:
statement 2
else statement • else statement is used when all the Ex: n1=10
previous conditions are false. n2=20
General format if n1 > n2:
if condition: print(“n1 is greater than n2”)
statement1 else:
else: print(“n2 is greater that n1”)
statement 2
Nested if • Placing an if statement inside Ex: x=31
statement another if is called nested if. if x > 10:
print(“x is greater than 10”)
if x > 20:
print(“x is greater than 20”)
if x > 20:
print(“x is greater than 30”)
• The and keyword is a logical Ex: a=200 b=33 c=500
And operator, and is used to combine if a > b and c > a:
conditional statements. print(“both conditions are true”)
Or • The or keyword is a logical Ex: a=200 b=33 c=500
operator, and is used to combine if a > b or a > c:
conditional statements. print(“at least one of the condition is
true”)
• if statements cannot be empty. Ex: a=33
Pass • But due to some reasons, if you b=200
statement have an if statement with no if b > a:
content, put it in the pass statement pass
to avoid getting an error.
SHESHADRI A V, DEPT OF CSE, GPT CHN Page 2
Python Programming-20CS31P
Sample Programs
# Python program to demonstrate if statement
a=30
b=20
if a > b:
print(“a is greater than b”)
# Python program to demonstrate if and elif statement
n1=10
n2=10
if n1 > n2:
print(“n1 is greater than n2”)
elif n1 == n1:
print(“n1 is equal to n2”)
# Python program to find largest of two numbers
a=30
b=20
if a > b:
print(“ a is greater than b”)
else:
print(“ b is greater than a”)
# Python program to find largest of three numbers
a = int(input(“Enter the value of a:”))
b = int(input(“Enter the value of b:”))
c = int(input(“Enter the value of c:”))
if a > b and a > c:
print(“a is largest”)
elif b > c:
print(“b is largest”)
else:
print(“c is largest”)
# Python program to illustrate nested if
x=31
if x > 10 and x < 20:
print(“x is greater than 10”)
if x > 20 and x <30:
print(“x is greater than 20”)
SHESHADRI A V, DEPT OF CSE, GPT CHN Page 3
Python Programming-20CS31P
if x > 30:
print(“x is greater than 30”)
# Python program to illustrate pass statement
a=33
b=200
if b > a:
pass
print("if statement is passed")
SHESHADRI A V, DEPT OF CSE, GPT CHN Page 4