sponsor

sponsor

Slider

Recent Tube

Business

Technology

Life & style

Games

Sports

Fashion

» » for in Loop

Loop का इस्तेमाल sequence के elements को iterate करने के लिए किया जाता है |
for Loop का इस्तेमाल कोई भी sequence के सभी elements को display करने के लिए किया जाता है |
sequence ये list, tuple, dictionary और string होता है |

Syntax for for_in Loop

for variable in sequence
 for_statement(s)

जब iteration शुरू होता है तब variable पर sequence का element assign किया जाता है और दिए गए statement को execute किया जाता है | जितने elements की संख्या होती है उस संख्या तक iteration होता रहता है और उसके बाद loop का control loop के बाहर आ जाता है |

Example for Iterating list sequence

Source Code :
a = [5, 6, 9, 8, 5, 7]

for n in a :
    print(n)
Output :
5
6
9
8
5
7


Example for Iterating tuple sequence

Source Code :
tuple = (1, 2, [3, 4, 5], 6, 7, 8)
for a in tuple:
    print(a)
Output :
1
2
[3, 4, 5]
6
7
8


Example for Iterating dictionary sequence

Source Code :
dict = {1:6, 2:5, 3:3, 4:8, 5:4, 6:2, 7:5, 8:4, 9:11, 12:45 }

for a in dict:
    print(a, dict[a])
Output :
1 6
2 5
3 3
4 8
5 4
6 2
7 5
8 4
9 11
12 45


Example for Iterating string sequence

Source Code :
str = "Hello World"
for a in str:
    print(a)
Output :
H
e
l
l
o
 
W
o
r
l
d


else with for Loop

for loop में जब sequence के elements का iteration ख़त्म हो जाएगा तब else का statement; execute होगा |
Source Code :
name = ['Rakesh', 'Ramesh', 'Suresh']

for i in name :
    print(i)
else:
    print("Outside of for Loop")

Output :
Rakesh
Ramesh
Suresh
Outside of for Loop

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply