Professional Documents
Culture Documents
IPP - 20-21 - Unit 2 - PPT
IPP - 20-21 - Unit 2 - PPT
UNIT II:
PYTHON PROGRAM FLOW
CONTROL
1
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II) Introduction to Python Programming
Conditional Block’s in Python
Decision structures evaluate multiple expressions which produce TRUE or
FALSE as outcome.
You need to determine which action to take and which statements to execute
if outcome is TRUE or FALSE otherwise.
Python programming language assumes any non-zero and non-null values as
TRUE, and if it is either zero or null, then it is assumed as FALSE value.
Python programming language provides following types of decision making
statements.
1. If statement
2. If else statement
3. If…elif….else statement
2
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II) Introduction to Python Programming
If statement
3
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II ) Introduction to Python Programming
If….else statement
An else statement can be combined with an if statement.
An else statement contains the block of code that executes if the conditional expression
in the if statement resolves to 0 or a FALSE value.
Syntax
if expression:
statement(s)
else:
statement(s)
Example If statement:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
4
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II ) Introduction to Python Programming
If….elif…else statement
The elif statement allows you to check multiple expressions for TRUE and execute a block
of code as soon as one of the conditions evaluates to TRUE.
Syntax
if expression1:
statement(s)
elif expression2:
statement(s)
else:
statement(s)
Example If statement:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal“)
else:
print("a is greater than b")
5
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II ) Introduction to Python Programming
For loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a
set, or a string).
With the for loop we can execute a set of statements, once for each item in a list, tuple,
set etc.
Syntax
for iterating_var in sequence:
statements(s)
6
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II ) Introduction to Python Programming
For loop using range
To loop through a set of code a specified number of times, we can use
the range() function.
The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.
Example 1 :
for x in range(6):
print(x) #output : 0, 1, 2, 3, 4, 5
Example 2 :
for x in range(2, 6):
print(x) #output : 2, 3, 4, 5
Example 3 :
for x in range(2, 10, 2):
print(x) #output : 2, 4, 6, 8
7
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II ) Introduction to Python Programming
For loop using string
Example :
for x in “RSCOE":
print(x)
Output :
R
S
C
O
E
8
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II ) Introduction to Python Programming
While loop
A while loop statement in Python programming language repeatedly executes a target
statement as long as a given condition is true.
Syntax
while expression:
statement(s)
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value.
The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.
Example :
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "Good bye!"
9
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II ) Introduction to Python Programming
Continue statement
It returns the control to the beginning of the loop..
The continue statement rejects all the remaining statements in the current iteration of
the loop and moves the control back to the top of the loop.
Syntax
continue
Example :
# Second Example
var = 10
# First Example
while var > 0:
for letter in 'Python':
var = var -1
if letter == 'h':
if var == 5:
continue
continue
print 'Current Letter :', letter
print 'Current variable value :', var
print "Good bye!"
10
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II ) Introduction to Python Programming
Break statement
It terminates the current loop and resumes execution at the next statement, just like the
traditional break statement in C.
The break statement can be used in both while and for loops.
Syntax
break
Example :
# Second Example
# First Example var = 10
while var > 0:
for letter in 'Python': print 'Current variable value :', var
if letter == 'h': var = var -1
break if var == 5:
print 'Current Letter :', letter break
print "Good bye!"
11
JSPM’s RSCOE, PUNE-33 A.Y. 2020-21 ( Sem-II ) Introduction to Python Programming