Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Lecture5 Py

Loops in Python can be used to repeat instructions or sequentially traverse lists, strings, and tuples. The document discusses while loops, for loops, break and continue statements, range(), and the pass statement. It provides examples of printing numbers from 1 to 100, finding the sum and factorial of the first n numbers, and searching a list using loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Lecture5 Py

Loops in Python can be used to repeat instructions or sequentially traverse lists, strings, and tuples. The document discusses while loops, for loops, break and continue statements, range(), and the pass statement. It provides examples of printing numbers from 1 to 100, finding the sum and factorial of the first n numbers, and searching a list using loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Loops in Python

ge
Loops are used to repeat instructions.

olle
C
while Loops

pna
while condition :

A
#some work

print hello 5 times


print numbers from 1 to 5

show infinite, iterator


Let‘s Practice

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.

Print the elements of the following list using a loop:

[1, 4, 9, 16, 25, 36, 49, 64, 81,100]

Search for a number x in this tuple using loop:

[1, 4, 9, 16, 25, 36, 49, 64, 81,100]


Break & Continue

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

print all numbers but not multiple of 3


Loops in Python

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 Loop with else

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:

[1, 4, 9, 16, 25, 36, 49, 64, 81,100]


range( )

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

generally used in execption handling


Let‘s Practice

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)

You might also like