0% found this document useful (0 votes)
3 views

Python Unit 2

Uploaded by

Tanya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Unit 2

Uploaded by

Tanya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Topics Covered

Day 1 Day 2 Day 3 Day 4

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:

The order in which the code of the program


executes.
Regulated by:

 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.

Real Life Scenario:

 Suppose you want to buy a new t-


shirt, visit the shop.
 If you do not found a shirt of your
choice, visit to the new shop.
 Until your desired found, action
shirt is performed is
again and again.
 This is called looping.

5
Selection/decision control statements
 It is used to control the flow of execution of program
depending upon condition:

 if the condition is true then the code block will execute


 if the condition is false then code block will not execute.

Real Life Scenario:


Consider the grading system for students:
 Excellent : Above 95%.
 Good: Greater than 80% &
less than equal to 95%.
 Average: Greater than 60% tha
& less equal to 80%. n
 Below average: Less than 60%

6
Revision of Flowchart

Used for start and stop.


Used for decision making.

Used for input/output.

Used for processing


statement.
Connector / flow

7
Flowchart of the grading system
On the basis of the conditions, corresponding
block of the code will be executed or not.

Consider the grading system for students:

 Excellent :Above 95%.


 Good :Greater than 80% &less than equal t 95%.
 Average : Greater than 60% & less than equal to 80%.
 Below average: Less than 60%

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,

 The if statement executes


 Otherwise, the else statement executes.

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

For a multiple conditional


statements
 We use if-elif 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

 “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”.

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.

 The if statement executes a group of statements depending upon whether a


condition is true or false.

 The if..else statement executes a group of statements


when a condition is true; Otherwise, it will execute another
group of statements.

 The if..elif statement is an extension of the if-


else statement. When we have multiple conditional statements, then we use
if-elif statement.

 When an if..else statement is present inside the body of another “if” or “else”
then this is called nested if else. 28

You might also like