Course Code: CSD106A
Course Title: Programming in Python
Lecture No. 9:
Conditional statements in Python
Delivered by: Dr. Anitha Kumari S D
1
Lecture Intended Learning Outcomes
At the end of this lecture, student will be able to
• Explain different types of conditional statements
• Use selection statements to make choices in a program
• Identify flow chart elements and connectors that are associated with branched
control flow
2
Contents
• Conditional Statements, Alternate Execution, Chained Conditionals, Nested
conditionals
3
Conditional statements
• Conditional statements: Give the ability to check conditions and change
the behavior of the program accordingly
• Different types of conditional statements include: if, if-else, if-elif-else, if-
elif, Nested if
4
if statement
• The simplest form of the conditional execution is the if statement
• Conditionally executes a statement or a block of statements
5
if statement
>>>x = 10
>>>if (x>0):
print('x is positive')
• The boolean expression (x>0) after if is called the condition
• If it is true, then the indented statement gets executed
• If not, nothing happens
6
if statement
>>>if (10>5):
print('10 is greater than 5')
print('Program ended')
Output is :
10 is greater than 5
Program ended
7
Pass keyword
• There is no limit on the number of statements that can appear in the body, but
there has to be at least one
>>>if (10>5):
SyntaxError: unexpected EOF while parsing
(Parsing: process of analyzing and breaking down a given input (such as a string,
file, or code) into its components to extract useful information)
8
Pass keyword
>>>if (10>5)
SyntaxError: invalid syntax
• It is useful to have a body with no statements (usually as a place keeper for the
code that is not yet written): Use pass statement, which does nothing
>>>if (x<0):
pass # need to handle negative values!
9
Alternate execution
• In alternate execution, there are two possibilities and the condition determines
which one gets executed
• A general if-then-else control structure
if boolean_expression:
statement_on_true_condition
else:
statement_on_false_condition
10
Alternate execution
x=10 x=11
if (x%2==0): if (x%2==0):
print('x is even') print('x is even')
else: else:
print('x is odd') print('x is odd')
Output is: x is even Output is: x is odd
• If the remainder when x divided by 2 is 0: x is even. If the condition is false, the
second set of statement is executed
• Since the condition must be true or false, exactly one of the alternative will be
executed
• The alternatives are called branches, because they are branches in the flow of
11
execution
If-else statement
• if (condition):
#Executes this block if
Condition is true
else:
#Executes this block if
Condition is false
12
If-else statement
>>>x=3 #if-else chain statement
letter = 'A'
>>>if (x==4): if (letter =='B'):
print('Letter is B')
print('Yes')
else:
else: if (letter =='C'):
print('Letter is C')
print('No') else:
if (letter =='A'):
Output is : No
print('Letter is A')
else:
print('Letter is not A, B or C'))
Output is : Letter is A 13
Nested If statement
if (condition1):
#executes when condition1 is true
If (condition2):
#executes when condition2 is true
#if block ends here
#if block ends here
14
Nested If statement
num = 10 num = 20
if (num>5): if (num>5):
print('Bigger than 5') print('Bigger than 5')
if (num<=15): if (num<=15):
print('Number is in between 5 and 15') print('Number is in between 5 and 15')
Output is: Output is:
Bigger than 5 Bigger than 5
Number is in between 5 and 15
15
If–elif statement (Chained condition)
• There are more than two possibilities and need
more than two branches
• elif is an abbreviation of “else if”
• Exactly one branch will be executed
• There is no limit on the number of elif statements
if (condition):
statement
elif (condition):
statement
else:
statement
16
If–elif statement (Chained condition)
letter='A' letter=‘P'
if letter =='B': if letter =='B':
print('Letter is B') print('Letter is B')
elif letter == 'C': elif letter == 'C':
print('Letter is C') print('Letter is C')
elif letter == 'A': elif letter == 'A':
print ('Letter is A') print ('Letter is A')
else: else:
print('Letter is not A, B or C') print('Letter is not A, B or C')
Output is: Letter is A Output is: Letter is not A, B or C
17
If–elif statement (Chained condition)
• If there is an else clause, it has to be at the end, but there doesn’t have to be one
• Each condition is checked in order; if the first is false, the next is checked, and so
on
• If one of them is true, the corresponding branch executes, and the statement
ends
• Even if more than one condition is true, only the first true branch executes
18
Nested statements
• Although the indentation of the statements makes the structure apparent, nested
conditionals become difficult to read very quickly
• Logical operators often provide a way to simplify nested conditional statements
x=4
if (x>0):
if (x<10):
print('x is a positive single digit number')
The same output with the and operator:
if x>0 and x < 10:
print('x is a positive single-digit number’)
19
Nested Conditions
• One condition can be nested within another
x = 13
y=17
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 is : x is less than y
The outer condition contains two branches; the first branch contains a simple
statement; the second branch contains another if statement, which has two
branches of its own; those two branches are both simple statements 20
Exercise
• Draw a flowchart describing the logic of given nested conditional statements
if door is closed:
if door is locked:
unlock door( )
open door( )
advance()
21
Exercise: Solution
• Draw a flowchart describing the logic of given nested conditional statements
22
Summary
• Conditional statements give the ability to check conditions and change the
behavior of the program accordingly
• The boolean expression in a conditional statement determines which branch is
executed
• if statement conditionally executes a statement or a block of statements
• In chained condition, there are a series of alternative branches
• In nested condition, a conditional statement appears in one of the branches of
another conditional statement
• Python relies on indentation rules to figure out the body of any construct
• Python does not permit empty block –pass statement should be used
23