Python | Loop | For Loop By. Shivansh Sir

PYTHON PROGRAMMING

PYTHON | LOOP | FOR LOOP

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

Syntax

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 = "WELCOME TO SUNRISE COMPUTER"

for a in str:

    print(a)

Output

W
E
L
C
O
M
E
 
T
O
 
S
U
N
R
I
S
E
 
C
O
M
P
U
T
E
R

PYTHON | LOOP | ELSE WITH FOR LOOP

For loop में जब sequence के elements का iteration ख़त्म हो जाएगा तब else का statement; execute होगा |

Source Code

name = ['Manish', 'Komal', 'Prince']

for i in name :

    print(i)

else:

    print("Outside of for Loop")

Output

Manish

Komal

Prince

Outside of for Loop


For Any Doubt Clear on Telegram Discussion Group

Join Us On Social Media

For Any Query WhatsApp Now

Comments

Popular posts from this blog

Python | All in One Program By. Shivansh Sir

Python | Data Types | Number & it's Conversion By. Shivansh Sir