Python Unit 2
Python Unit 2
1. Introduction 2.3 Iterative/ Looping 2.3 Iterative/ Looping 2.4 Jump Statements
2. Selection / Statements Statements • Break
• While loop • Range( • Continue
Decision control
statement • Nested )
While loop Functio 2.5 Else with Loop
• Simple If
n •
statement While with
• For
• If Else else
loop • For with else
statement • Nested
• If-Elif statement
for
•
Nested If
loop
Else
statement
4
Session Plan - Day 1
1. Introduction to Decision Control Statements
2. Selection / Decision control statement
• Simple If Statements
• If Else Statements
• If ..elif..else statements
• Nested If Else Statements
• Examples
• Review questions
• Summary
2
Introduction
Control flow of the program is:
Conditional statements.
Iterative/looping statements.
3
Contd..
Conditional statement examples: Based on certain conditions, we take
decisions in our daily life.
• Darkness in the room: Turn on • Weather is Cold: Drink
lights. Coffee
• No Darkness: Do Nothing • Weather is not Cold:
Softdrink
4
Contd..
Iterative Looping statements examples: For taking decisions we perform
various actions and repeat the same action many times.
5
Selection/decision control statements
It is used to control the flow of execution of program
depending upon condition:
6
Revision of Flowchart
7
Flowchart of the grading system
On the basis of the conditions, corresponding
block of the code will be executed or not.
8
Types of decision/selection control statements:
Simple if
if-else
if..elif..else
Nested...if…
else
9
Simple if statement
if evaluates whether a
condition is: True
False.
if block is executed:
if and only if the value of its condition
expression is true.
Note: As we use curly braces in ‘C’ Language to define the scope of conditions and loops; in
the same manner we use colon (:) and indentation in Python.
10
Syntax of Simple if
11
Example
s
Print whether a number is even
or not. Case-2 : When n is not
Case-1 : When n is even. even
12
Examples
A program to increment a number if it is
positive.
Explanation:
The condition x>0 is True for value of x = 10 so the statements in the block of
code will be executed.
The statement x = x+1 will increment the value of x by 1 i.e. x = 10 + 1; x = 11 and
statement
print (x) will print x value as 11. 13
if else Statement
It checks whether a condition is true or
false.
If a condition is true,
14
Syntax of if else Statement
15
Example
A program to print “Excellent” if marks is greater than 95
otherwise print “Not Excellent”
Explanation:
For input 68, condition marks > 95 is False. Therefore, else statement print (“Not
Excellent”) is executed and output will be “Not Excellent”.
16
if..elif..else statement
Extension of the if-else statement
Note: The conditional expression of if-elif-else are executed from top to bottom in a sequential
manner. An elif statements are known as elif Ladder.
17
Syntax of if..elif..else statement
18
Example
Explanation:
For input 88, condition marks > 95 is False & control goes to next elif statement
(marks>80 and marks<=95), which is True and statement print (“Good”) will be
executed.
The output will be “Good”.
19
Nested if-else statement
When an if-else statement is present inside the body of another “if” or
“else” then this is called nested if else.
20
Syntax of Nested if..else statement
21
Example
A program to take age as input from user and print “You are less than 18” if
age is less than 18, otherwise print “You are above 18”.Also check that age
could not be less than 0. If so then print “Age could not be less than 0”.
22
Example
A program to take age as input from user
and print
23
Can you answer these questions?
1. Which one of the following is a valid Python if
statement :
A) if a>=2 :
B) if (a >= 2)
C) if (a => 22)
D) if a >= 22
24
3.Which of following is not a decision-making
statement?
A) if-elif
statement
B) for statement
C) if -else
statement
D) if statement
25
4.Predict the output of the following
code:
A) No output
B) okok
C) ok
D) None of
above
26
5. What is the output of the following code
snippet?
A) Launch a
Missile
B) Let's have
peace
C) 0.3
D)None
27
Summary
Control statement are statements that control the flow of execution of
statements so that they can be executed repeatedly and randomly.
When an if..else statement is present inside the body of another “if” or “else”
then this is called nested if else. 28