For loop iteration ke liye use hota hai , iteration ka dusra mtlb ho skta hai repetition , agar mai poochu ki kis chiz pe iteration to answer hai , iteration over a sequence like string, list , dicitionary , tuple , sets.
Syntax:
for iterator_name in iterating_sequence:
Statement......
Aayiye ise example ki madad se aur clear krte hai.......
------------------------------------------------------------------------------------------
#program of for loop in string
r = 'rishabh'
for x in (r):
print(x)
output:
r
i
s
h
a
b
h
------------------------------------------------------------------------
#program to print items in list
l =["ram" , "shyam" , "rishabh" , "shyamlal"]
for y in l:
print(y)Output:ram shyam rishabh shyamlal# Ap dekh skte hai ki kaise ye loop work kr rha hai, hmne list ke saare element print karaye hai#list 'l' ke saare element baari baari se 'y' mai ja rahe hai aur fir hm 'y' ko print kara de rahe hai.#sbse pahle 'y' mai ram aaya to hamne 'y' ko print kara diya , fir uske baad 'y' mai shyam aaya to hamne shayam ko print karadiya, fir isi tarah chlta rha aur baki saare items bhi print karadiye.#for loop mai hame proper indentation ka dhyan rakhna hai.-----------------------------------------------------------------------------------------------#program to print numbers in list
l= [5,7,89,76,56]
for x in (l):
print(x)Output:5 7 89 76 56--------------------------------------------------------------------------------------------#Program to print the square of given number (2,4,5,6)l=[2,4,5,6] for x in l: y=x*x print(y)Output:4 16 25 36--------------------------------------------------------------------------------------Range() function in for loopRange function sirf integer ke sath kaam krta hai , hm isme string ,float kuch bhi pass nhi kr skte haiRange function ka use hm kisi bhi code ko specific number of times execute karane ke liye krte hai.3 Variation of range function:range(stop) - start from Zero till (stop-1)range(start, stop) -End at (stop-1)range(start, stop, step) - Step kabhi bhi zero nhi ho sktaAayiye example ki madad se ise aur clear krte hai...----------------------------------------------------------#Variation 1for x in range (10): print(x)
Output:
0 1 2 3 4 5 6 7 8 9-------------------------------------------------------------------------------------# Variation 2for x in range (10,20): print(x)Output:10 11 12 13 14 15 16 17 18 19# dhyn rakhna isme 10 include hai , aur 20 exclude hai.-------------------------------------------------------------------------------------# Variation 3for x in range (10, 20,2): print(x)
Output:
10 12 14 16 18#basically 2 ka yaha mtlb hai ki ek-ek number ko skip kro jaise , 10 ke baad 11 skip hua aur 12 print hua , usi tarah 12 ke baad 13 skip hua aur 14 aaya.....--------------------------------------------------------------------------------------
While LoopsWhile loop ka bhi use iteration ke liye hota hai. While loop bhi for loop jaisa hi hai , pr inme ek difference hai ki for loop ka use hm tb krenge jb hme pata hoga ki kitne time repetion hoga aur while loop ka use hm tb krenge jb hme ye pata na ho ki kitne time iteration possible hai.Let's begin it with simple example:---------------------------------------------------------------------------------------------#Program to print the number from 1 to 10 using while loop.i=1 while(i<=10): print(i) i=i+1
Output:
1 2 3 4 5 6 7 8 9 10#Ise hm for loop ki madad se bhi kr skte hai the pr yaha pr specifically mention thaki ise hme while loop se krna hai.---------------------------------------------------------------------------------------#Program to print the even number between 1 to 10.i =0 while(i<10): i=i+2 print(i)
Output:
2 4 6 8 10#ANOTHER WAY OF DOING THISi =2 while(i<=10): if(i%2==0): print(i) i=i+1
Output:
2 4 6 8 10-------------------------------------------------------------------------------------While loop ke kuch or examples....-------------------------------------------------------------#Program to print the factorial of number.#Jaise agar mujhe 5 ka factorial chahiye to 5 ka factorial hoga 5*4*3*2*1=120n =int(input("Enter a number ")) fac=1 while(n>0): fac =fac*n n=n-1 print(fac)Output:Enter a number 5 120#Isme hme proper indentation ka khyal rakhna hai , nhi to error aa jayegi#I hope program ki working ap ko samajh aa gyi hogi , agar nhi aayi to comment kre.---------------------------------------------------------------------------------# Program to find a reverse of a number.n = int(input("Enter a number: ")) r=0 while(n>0): d = n%10 r =r*10+d n=n//10 print("The reverse of a number is :", r)
Output:
Enter a number: 123 The reverse of a number is : 321# '//' this is basically a division operator in python.# '%' ye modulus operator hai , iska use remainder find krne ke liye hota hai.# If you cannot solve this , don't feel bad this is quite tough one.-------------------------------------------------------------------------------------# Program to add the sum of natural number.#natural number start from 1 to infinity....num = int(input("Enter a number ")) s=0 while(num>0): s = s+num num=num-1 print("Sum of number is " , s)
Output:
Enter a number 10 Sum of number is 55# Feel free to ask question in comment section if you find it difficult.--------------------------------------------------------------------------------------
Comments
Post a Comment