Python Iteration (Loops) statements are of two type:-
1. While Loop
2. For Loop
While Loop
It is used to execute a block of statement if a given condition is true. And when the
condition become false, the control will come out of the loop. The condition is checked
every time at the beginning of the loop.
Syntax
while (condition):
[statements]
Counter=0 #initiation stmt
while Counter < 5: #test condition
print(Counter)
Counter += 2 #increment stmt
OUTPUT:
For Loop
It is used to iterate over items of any sequence, such as a list or a string.
Syntax
for val in sequence: #here val will take value of each element in the sequence
statements
Example:
for I in [1,2,3,4,5]:
print(I*5)
OUTPUT:
5
10
15
20
25
range() Function
This function generates a sequence of numbers based on the parameters passed.
Parameters
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step(Optional): Determines the increment between each numbers in the sequence.
Python use range() function in three ways:
a. range(stop)
b. range(start,stop)
c. range(start,stop,step)
Note:
All parameters must be integers.
All parameters can be positive or negative.
a. range(stop): By default, It starts from 0 and increments by 1 and ends upto
stop, but not including stop value.
Example:
for x in range(4):
print(x)
Output:
0
1
2
3
b. range(start,stop) : It starts from the start value and upto stop, but not
including stop value.
c. range(start, stop, step): Third parameter specifies to increment or decrement
the value by adding or subtracting the value.
Use with for loop:
for i in range(1,5): for i in range(5,3,-1):
print(i) print(i)
OUTPUT: Output
1 5
2 4
3
4
Jump Statements
Jump statements are used to transfer the program's control from one location to
another. Means these are used to alter the flow of a loop like - to skip a part of a loop or
terminate a loop
There are three types of jump statements used in python.
[Link] / [Link] / [Link]
Break
It is used to terminate the loop.
e.g.
for val in "string":
if val == "i":
break
print(val)
print("The end")
Output
st r
The end
Continue
• Used to skip the rest of the statements of the current loop block and to move to next
iteration, of the loop.
• Continue will return back the control to the beginning of the loop.
• Can be used with both while & for. Eg:
for letter in ‘Python’:
if letter == ‘h’:
continue
print (letter)
Output
Pyton
Pass Statement
This statement does nothing. It can be used when a statement is required syntactically but the
program requires no action.
Use in loop :
while True:
pass # Busy-wait for keyboard interrupt (Ctrl+C)
WORKSHEETS
Sr Question
no
Q1 Can we write if/else into one line in python?
a) Yes
b) No
c) if/else not used in python
d) None of the above
Which one of the following is a valid Python if statement.
Q2 a) if a>=2:
b) if (a >= 2)
c) if (a => 22)
d) if a >= 22
Q3 What keyword would you use to add an alternative condition to an if
statement?
a) else if
b) elseif
c) elif
d) None of the above
Q4 What will be the data type of the var in the below code snippet?
var = 10
print(type(var))
var = "Hello"
print(type(var))
a) int and str
b) int and int
c) str and str
d) float and str
Q5 How is a code block indicated in Python?
a) Brackets
b) Indentation
c) Key
d)None of the above
Q6 What will be the output of the following code snippet?
a = [1, 2, 3] a =
tuple(a) a[0] = 2
print(a)
a) [1,2,3]
b) (1,2,3)
c) [2,2,3]
d) error
Q7 What will be the output of the following code snippet?
print(type(5 / 2))
print(type(5 // 2))
a) int and int
b) str and str
c) int and float
d) float and int
Q8 What will be the output of the following Python code?
x = [ 'ab' , 'cd' ]
for I in x:
i. upper()
print ( x )
a) [‘ab’, ‘cd’ ]
b) [‘AB’, ‘CD’]
c) [None, None]
d) none of the mentioned
What will be the output of the following Python code?
i=5
Q9 while True:
if i%0O11 == 0:
break
print(i) i
+= 1
a) 5 6 7 8 9 10
b) 5678
c) 56
d) error
Q10 What will be the output of the following Python code? i = 1
while True: if i%3 == 0:
break
print(i)
i+=1
a) 1 2
b) 1 2 3
c) error
d) none of the mentioned
11 Find the output of the following program segments:
I. a = 10
while a > 2:
print (a)
a -= 2
II. for i in range (20, 30, 2):
print (i)
III. var = 7
while var > 0:
print (“current variable value: ” , var)
var = var – 1
if var == 3:
break
else:
if var == 6:
var = var – 1
continue
print (“Good bye!”)
2 Write a Python program to find the factorial of a positive number
Q6) What are the four elements of a while loop in Python?
3 What are endless loops? Why do such loops occur? Give example.
4 Draw flowchart for displaying first 10 odd numbers.
5 What is a flowchart? How is it useful?
6 What is empty statement in Python? What is its need?
7 Write a program to check whether a years is leap year or not.
8 Write a program to check a character is vowel or not.
COMPETENCY BASED
SECTION A: MCQ
1. The first line of Python code can’t have an indentation. Say True or False
Ans: True
2. Which of the following expression is not allowed in if statement
a) arithmetic expression b) relational expression c) logical expression d) none
Ans: a.
3. x=[ ‘P’ , ‘ y’ , ‘t’ , ‘h’ , ‘o’ , ‘n’ ]
for i in x:
print(i,end=’’)
`a) P b) python c) P y t h o n d) PYTHON
Ans: c
4. Function range(3) is equivalent to :
a) range(1,3) b) range(0,3) c) range(0,3,1) d) range(1,3,0)
Ans: b or c
5. The else block of a loop will not get executed if a.............statement has
terminated
the loop.
Ans: break
6. The range() function can only be used in..............loops
Ans: for
7. The in and not in operators are also called as ………….
Ans: membership operators
8. For a while loop, an equivalent for loop can always be written. Say True or
False.
Ans: False
9. The range() function generates a sequence of.................type
Ans: list
10. for is a ……. loop whereas while is a.............loop
Ans: counting, conditional
SECTION B : 2 marks question:
1) Define pass statement.
2) What is the difference between elif and else construct of if statement?
3) Write the equivalent while loop for the following for loop
n=20
for i in [1,10]:
n+=i
4) What is the output for the following code?
i=1
while(i<=7):
i*=2
print(i)
5) What is the output of the following
loop? for a in[1,4,7]: