Lecture5 Py
Lecture5 Py
ge
Loops are used to repeat instructions.
olle
C
while Loops
pna
while condition :
A
#some work
e
Print numbers from 1 to 100.
lleg
Co
Print numbers from 100 to 1.
pna
A
Print the multiplication table of a number n.
e
Break : used to terminate the loop when encountered.
lleg
Co
Continue : terminates execution in the current iteration & continues execution of the loop
na
with the next iteration.
Ap
take search example
& stop the search when found
ge
Loops are used used for sequential traversal. For traversing list, string, tuples etc.
olle
C
for Loops
pna
for el in list:
A
#some work
for el in list:
#some work
else:
else used as it doesn’t execute
#work when loop ends when break is used
Let‘s Practice
ge
using for
olle
Print the elements of the following list using a loop:
na C
[1, 4, 9, 16, 25, 36, 49, 64, 81,100]
Ap
Search for a number x in this tuple using loop:
ge
Range functions returns a sequence of numbers, starting from 0 by default, and increments by
lle
1 (by default), and stops before a specified number.
o
a C
range( start?, stop, step?)
n
Ap
Let‘s Practice
ge
using for & range( )
olle
Print numbers from 1 to 100.
na C
p
Print numbers from 100 to 1.
A
Print the multiplication table of a number n.
pass Statement
ge
pass is a null statement that does nothing. It is used as a placeholder for future code.
olle
na C
for el in range(10):
Ap
pass
e
WAP to find the sum of first n numbers. (using while)
lleg
a Co
Apn
WAP to find the factorial of first n numbers. (using for)