0% found this document useful (0 votes)
5 views7 pages

Python Looping Statements Explained

The document provides an overview of looping statements in Python, including while loops and for loops, along with their syntax and advantages. It also discusses jumping statements like break and continue, and provides examples of assignments and nested loops. Additionally, it covers the use of iterators, dynamic lists, and dictionaries, as well as formatted strings and various loop patterns.

Uploaded by

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

Python Looping Statements Explained

The document provides an overview of looping statements in Python, including while loops and for loops, along with their syntax and advantages. It also discusses jumping statements like break and continue, and provides examples of assignments and nested loops. Additionally, it covers the use of iterators, dynamic lists, and dictionaries, as well as formatted strings and various loop patterns.

Uploaded by

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

Looping Statment in python

Looping statement are used to execute


a same block of code of specified no.
Type of loop:-
[Link] loop
[Link] loop
Advantages of loop.
* less no of line
*complexity reduced
* execution fast

while loop:-
it is also called entry conrtrolled loop.
syntax

initialization # where to start


while(condition):
body of loop
icrement/decrement

jumping statement:-
break, contiue, pass, return, exit()

break syntax:-
initialization
while(condition):
break

contiue syntax:-
initialization
while(condition):
increment
contiue

exit()

exit() syntax:-
initialization
while(condition):
exit()

dif b/w berak and contiue statement


break statement is a jumping staement that is used to
pass the control to the end of the loop.

contiue statement.
continue statement is a jumping statement that is used to skip
the present iteratin and forces next iteration of loop to take place

how to make infinite loop


why to make infinite loop

assignment using while loop:-


[Link] all even no 1 to 100
[Link] all odd no 1 to 100
[Link] table at run time
4. factorial of any no
[Link] no is prime or not
[Link] fibonacci series:-01123.....
7.# find the no is palindrome or not
8 find out no is neon no or not :9=8+1=9
9 find out no is armsstrong no or not=153=1+125+27
10 find out no is perfect no or not

for loop:-
syntax:-
for var in sequence:
body of loop
sequece. list, tupe , dictionary,string

iterator: iterator in python is simply an object


that can be iterated upon, an object which will return
data, one element at a time.
iterators are everywhere in python. They are elegantly
implemented within for loop.
#using iterators.
a['java','net','python']
for i in a:
print(i)

# dictionary using loop


data={"id":1,"name":"aman","age":23}
for i in data:
print(i)// by default key value

for i in [Link]():
print(i)

for i,j in [Link]():


print(i,j)

#how iterator works


Iterator in python every where it is simply an object that can be
iterated upon, an object which will return data, one element at a time.
they are elegantly
implemented within for loops, list cprehension,generators.
in python iterator object must implement two special menthod

iter():- use for hold memory address


next():- use for inrement data

# find the length of object without using len functin


# add the all element of list

for var in range(start,end,step)

loop with index:-


li=[1,2,3,4,5]
for i in range(len(li)):
print(i,li[i])
or
for i,j in enumerate(li):
print(i,j)

nested loop:- loop with in loop is colled nested loop


question:-
1.
*
**
***
****
2
****
***
**
*
3
*
**
***
****
4
****
****
****
****
5
1111
2222
3333
4444
6
1234
1234
1234
1234
7
1
12
123
1234
8
1
22
333
4444
9
a
ab
abc
abcd
10
a
bb
ccc
dddd
11
d
du
duc
duca
ducat
12
p
py
pyt
pyth
pytho
python
13
*
* *
* * *
* * * *
* * * * *
14
* * * * *
* * * *
* * *
* *
*
15
*
**
***
****
*****
16
*****
****
***
**
*

use if zip() function:-

id=[1,2,3,4]
name=["uttam","rahul","sonu","monu"]
age=[22,25,21,28]
for i in zip(id,name,age):
print(i)

#F-string(formatted string)
formet specifier meaning
%d for display integer
%f for display float

%s for display string value

we can write f-string in three way


d=4
m="april"
y=2024.0
print("today is %d %s %f",(d,m,y))
print("today is {0}{1}{2}".format(d,m,y))
print(f"todat is {d} {m} {y}")

# dynamic list using loop

a=[]
for i in range(0,5):
no=int(input("enter the element"))
[Link](no)
print(a)

# dynamic dictionary using loop

d={}
for i in range(1,6):
x=int(input("enter the value of dictionary"))
d[i]=x
print(d)

#1
'''
for i in range(1,6,1):
for j in range(1,i+1,1):
print("*",end=" ")
print()

#2
for i in range(1,6,1):
for j in range(6,i,-1):
print("*",end=" ")
print()

#3
for i in range(1,6):
for j in range(5,i,-1):
print(" ",end=" ")
for k in range(0,i):
print("*",end=" ")
print()
# 4

for i in range(1,6):
for j in range(1,6):
print("*",end=" ")
print()

#5
for i in range(1,6):
for j in range(1,6):
print(i,end=" ")
print()

#6
for i in range(1,6):
for j in range(1,6):
print(i,end=" ")
print

#7
for i in range(1,6):
for j in range(1,i+1):
print(j,end=" ")
print()

#8
for i in range(1,6):
for j in range(1,i+1):
print(i,end=" ")
print()

#9
st="abcde"
for i in range(1,6):
for j in range(0,i):
print(st[j],end=" ")
print()

#10
st="abcde"
for i in range(0,5):
for j in range(0,i+1):
print(st[i],end=" ")
print()

#13
for i in range(0,5):
for j in range(0,5-i-1):
print(end=" ")
for j in range(0,i+1):
print("x",end=" ")
print()

'''
#14
for i in range(5,0,-1):
for j in range(0,5-i):
print(end=" ")
for j in range(0,i):
print("x",end=" ")
print()

You might also like