0% found this document useful (0 votes)
18 views10 pages

18 For Looppdf Lyst8872

The document provides an overview of the 'for loop' in Python, explaining its use with iterables and how it differs from the 'while loop'. It also covers concepts such as the 'break' statement within loops and the 'for-else' construct. Additionally, it includes examples and practice programs related to counting characters in a string and handling specific conditions during iteration.

Uploaded by

Shilpa Munjal
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)
18 views10 pages

18 For Looppdf Lyst8872

The document provides an overview of the 'for loop' in Python, explaining its use with iterables and how it differs from the 'while loop'. It also covers concepts such as the 'break' statement within loops and the 'for-else' construct. Additionally, it includes examples and practice programs related to counting characters in a string and handling specific conditions during iteration.

Uploaded by

Shilpa Munjal
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

Python

for loop

Saurabh Shukla (MySirG)


- Agendas
① for loop

② Practice program
③ For us while

⑥ break in for

⑤ for else
torloop
For loop only works on iterables .

for variable in Iterable :

code
code

Examine
" "

for x in My Sira :

print (se )
output in
y
s
i
r
G
Write to Count
'
a
program a'

in for loop
a
green string using .

)
"
"

input ( Enter
Si =
a
string
count = 0

for e in SI :
" "
a :
if e==
count -1--1

(
"
count
''
count )
print -_
,
whilevsfoo

while loop iterates a block of code

till the specified condition is true .

for loop iterates a block of code


number
number of times same as

the itesabhe
of elements in .

for deterrents of iterate


It iterates
element to the least
from first
element
[Link]
group of values
elements of the
to access
• Ability
the
( from first erdement
to
object
last element )
is applicable on

certain types
breakinfor
break keyword can be used only in

the body of loop ( while or for )


the control outside
break transfers

the loop body and loop terminates


for else

for variable in iterable :

code
code
if condition :

break
code
else :

code
code
Print all the character of a
string ,
but

if appeared the
' '

stop printing r in sequence .

If all the character successfully printed


characters
"
the print message All the are

processed
"

)
"
"

si = input ( Enter a
string
for e in SI :

' '
r :
if e = =

break
)
'

print ( e , end =

processed )
'

else : " characters are


( AM the
print
)
"
"
✗ = input ( Enter a
string

for I in x :
' '
if i r
= = :

break
)
' '

( i end
print
=
,

else :
processed )
"
"

All the characters


print (
are

You might also like