0% found this document useful (0 votes)
12 views9 pages

5TH Lecture

Uploaded by

iswella121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

5TH Lecture

Uploaded by

iswella121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Loops in Python THERE ARE TWO TYPES OF LOOP

e
1) WHILE LOOP 2) FOR LOOP

g
Loops are used to repeat instructions.

olle
C
while Loops

pna
while condition :

A
#some work
THE VARIABLE USED IN THE LOOP IS CALLED THE ITERATOR
THE PROCESS OF CIRCLE IN THE LOOP OF PYTHON IS CALLED THE ITERATION

INFINITE LOOP IS VERY DANGEROUS IN THE COODING


ITS CRASH THE ANY WEBSITE WHICH WE PRODUCED

THE VALUE WITH WE START IN THE LOOP IS CALLED THE INITIALIZATION


i=0 THIS VALUE IS CALLED THE INITIALIZATION IN THE LOOP

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]


IN THIS QUESTION WE USED THE TERM TRAVERSE
TRAVERSE MEANS THE TRAVELING
IN PYTHON WE USED THE TRAVERSE AS PRINT THE ALL ELEMENTS
PRESENT IN MY LIST OR TUPLE ONE BY ONE

Search for a number x in this tuple using loop:

[1, 4, 9, 16, 25, 36, 49, 64, 81,100] THIS TYPE OF SEARCHING IS CALLED THE
LINEAR SEARCHING IN THE PYTHON
Break & Continue

e
Break : used to terminate the loop when encountered.

lleg
TERMINATE MEANS STOPPED WE ALSO USED THE BREAK KEYWORD TO PRINT ONLY FIRST

o
WHEN WE USED THE BREAK FUNCTION IN THE LOOP FINDING ELEMENT WHILE LOOP PRINT ALL FINDING ELEMENTS
OUR LOOP STOPPED THERE

C
Continue : terminates execution in the current iteration & continues execution of the loop

na
with the next iteration.

Ap WE USED THE CONTINUE KEYWORD IN THE LOOP FOR SKIPPING


CONTINUE KEYWORD SKIP THE SPECIFIC VALUE WHICH WE CHOOSE IN THE LOOP

take search example


& stop the search when found

print all numbers but not multiple of 3


WHEN WE USED THE STRINGS IN THE FOR LOOP
THE CHARACTERS OF THE STRINGS ARE PRINTED

Loops in Python
ONE BY ONE

ge
Loops are used used for sequential traversal. For traversing list, string, tuples etc.

olle
C
for Loops

pna
WE USED THIS TYPE OF THE LOOP FOR THE DATA TYPE
for el in list:

A
ETC FOR LIST,STRING AND TUPLES

#some work

WHEN BREAK KEYWORD IS USED IN THE LOOP

for Loop with else ELSE IS OPTIONAL IN THE FOR LOOP


IT IS NOT COMPULSARY IN THE LOOP
ELSE PRINTING WORD IS NOT EXECUTED
WHILE WHEN WE CANNOT USED ELSE WITH THE BREAK
KEYWORD ,THEN THE WORD OF THE PRINTING FUNCTION
IS PRINTED

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]


THIS TYPE OF SEARCHING IS CALLED THE
LINEAR SEARCHING IN THE PYTHON
ENDING ELEMENT IS NOT INCLUDED IN THE RANGE OF THE LOOP

range( )
NOT THE SEQUENCE OF THE DATA TYPE

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
C
THERE ARE THREE MAIN POINTS FOR RANGE:

a
START==O AND ETC
STEP MEANS INCREASE == 1 AND ETC
range( start?, stop, step?)

n
STOP OR END == X

Ap START IS THE OPTIONAL


IF WE CANNOT WRITE THE
STARTING POINT IN THE
RANGE THEN ITS START
STOP IS COMPULSARY IN THE
RANGE OF LOOP , FOR THIS
PYTHON NEED A STOPPING POINT
YOU IN THE RANGE OF THE LOOP
STEP IS ALSO OPTIONAL AS WELL AS START
IN THE RANGE
IF WE CANNOT WRITE THE STEPPING POINT
IN THE RANGE OF THE LOOP THEN ITS INCREASE
OR STEPPED BY 1 BY DEFAULT
FROM THE 0
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 PASS STATEMENT IS USED FOR SKIPPING BUT
NOT LIKE CONTINUE KEYWORD
ITS WORKS LIKE COMMENTS

ge
pass is a null statement that does nothing. It is used as a placeholder for future code.

olle
PASS STATEMENT IS USED IN THE LOOP

C
AND AS WELL AS IN CONDITIONAL STATEMENTS

na
for el in range(10):

Ap
pass

generally used in execption handling


Let‘s Practice
SOLVE THESE QUESTIONS AGAIN AND AGAIN
FOR GET THE BEST CONTROL ON THE LOOPS

THESE QUESTIONS IS TOO IMPORTANT FOR LOOPS

e
WAP to find the sum of first n numbers. (using while)

g
SOLVE THIS QUESTION WITH

e
THE FOR LOOP

Coll
pna
A
WAP to find the factorial of first n numbers. (using for) SOLVE THIS QUESTION WITH
THE WHILE LOOP

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy