Unit 3 Python Program Flow
Unit 3 Python Program Flow
Subject
Introduction to Python
by
Prof. M. V. Tiwari
1. If Loop
If statement in Python is required when there is a need to
make some kind of Decision. This decision making is used
to execute a code only if certain condition is satisfied.
Syntax:
if test expression: False
statement(s) Test
Expression
True
Body of if
O/p :-
Enter the number for testing : 2
2 is a positive number.
Syntax of if...else
if test expression:
Body of if
else:
Body of else
O/P : -
Enter the value for testing : -2
Negative number
if test expression:
Body of if
elif test condition:
Body of elif
else:
Body of else
•The elif is short for else if. It allows us to check for multiple expressions.
•If the condition for if is False, it checks the condition of the next elif block
and so on.
•If all the conditions are False, the body of else is executed.
•Only one block among the several if...elif...else blocks is executed according
to the conditions.
•The if block can have only one else block. But it can have multiple elif
blocks.
DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari
2SAEKNX01 : Introduction to Python P R M I T & R ,B A D N E R A
Syntax of if...elif...else
if test expression:
Body of if
elif test condition:
Body of elif
else:
Body of else
O/P : -
Enter the value for testing : -2
Negative number
DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari
2SAEKNX01 : Introduction to Python P R M I T & R ,B A D N E R A
2. While Statement
•The while loop in Python is used to iterate over a block of code as long as the
test expression (condition) is true.
•We generally use this loop when we don't know the number of times to iterate
beforehand.
•In the while loop, test expression is checked first. The body of the loop is entered only if
the test_expression evaluates to True. After one iteration, the test expression is checked again.
This process continues until the test_expression evaluates to False.
•In Python, the body of the while loop is determined through indentation.
•The body starts with indentation and the first unindented line marks the end.
•Python interprets any non – zero value are True.
•None and 0 are interpreted as False
while i <= n:
sum = sum + i
i = i+1 # update counter
O/p :-
Enter n: 10
The sum is 55
DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari
2SAEKNX01 : Introduction to Python P R M I T & R ,B A D N E R A
O/p :-
The sum is 48
O/P :-
1. range(0, 10)
2. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3. [2, 3, 4, 5, 6, 7]
4. [2, 5, 8, 11, 14, 17]
O/P :-
I like pop
I like rock
I like jazz
O/P :-
0
1
5
No items left.
DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari
2SAEKNX01 : Introduction to Python P R M I T & R ,B A D N E R A
assert
1. Assert is one of the keywords in Python
2. The assert keyord is used when debugging code.
3. The assert keyword lets you test if a condition in your code returns True, if not,
the program will raise an AssertionError.
4. You can write a message to be written if the code returns False.
Syntax:-
assert condition, error_message(optional)
x = "hello"
#if condition returns True, then nothing happens:
assert x == "hello"
#if condition returns False, AssertionError is raised:
assert x == "goodbye“
In the above example (assert x = “hello”) nothing will happens since the condition is true.
When (assert x == “goodbye”) AssertionError will raised since the condition is false.
x = "welcome"
#if condition returns False, AssertionError is raised:
assert x == "hello", “should be ‘hello’ ”
In this code x holds “welcome” and assert statement checks for x == “hello”
condition. As this condition is false then assert will raised an error with message
(“should be ‘hello’ ”)
o/p:- AssertionError: x should be 'hello
'
Q 1)
even = False
if even = True:
print(“It is even ! ”)
Q 3) You are coding a math utility by using Python. You are writing a function
to compute roots. The function must meet the following requirements:
If a is non – negative
return a**(1/b)
If a is negative and even
return “result is imaginary number”
If a is negative and odd
return – (-a)**(1/b)
How should you complete the code ?
Using the following options:
if a >= 0:
elif a % 2 ==0:
else:
elif:
Option 1: Option 2:
A. if grade = 90: B. if gade >= 90: A. if grade = 800: B. if gade >= 80:
C. elif grade >90: D. elif grade >= 90: C. elif grade >80: D. elif grade >= 80:
Option 1: Option 1:
A. if grade = 70: B. if gade >= 70: A. if grade = 65: B. if gade >= 65:
C. elif grade >70: D. elif grade >= 70: C. elif grade >965: D. elif grade >= 65:
A) error B) hello
C) good D) bad
Q 6) You are writing a Python Practice Test program to ask the user to enter a number and determine if the
number is 1 digit 2 digits or more than 2 digits long. You need to write the program. How should you
complete the code ? To answer, select the appropriate code segment in the answer area.
A)9
B) 10
C) 11
D)Infinite number of times
number = 6
while number > 0:
number -= 3
print(number, end = ‘ ‘)
Options :
A)6 3 0 B) 6 3
C) 3 0 D) 3 0 -3
i=1
while True:
if i % 2 == 0:
break
print(i, end = ‘ ‘)
i += 2
Options :
A)1 B) 1 2
C) 1 2 3 4 5 6 ...... D) 1 3 5 7 9 11 .........
x=4
while x >= 1:
if x % 4 == 0:
print(“Party”, end = ‘ ‘)
elif x – 2 < 0:
print(“cake”, end = ‘ ‘)
elif x / 3 == 0:
print(“greeting”, end = ‘ ‘)
else:
print(“birthday”, end = ‘ ‘)
x = x -1
Options :
Options :
A)0 0 1 0
B) 0 1
C) Error
D)None of the mentioned
A)1 2 3 4 5 6 7 8 9
B) 1 2 3 4 5 6 7 8 9 10
C) 1 2 3 4 5
D)1 3 5 7 9
y=0
for i in range (10, 1, -2)
y += i
print(y)
A) 10
B) 40
C) 30
D) 20
x = ‘abcd’
for i in range (len(x)):
print(i, end = ‘ ‘)
A)a b c d
B) 0 1 2 3
C) Error
D)1 2 3 4