Python For Loop Overview

For loop kisi bhi programming language ka most important topic hai. Yah iteration ka work sequential ways me perform karta hai. Iteration means liye gaye conditions ke hisab se one by one execution ki process ko handle karna.

Aap iska use List, Sets, Tuple, Dictionary, Strings etc kai tarah ki conditions me kar sakte hain. Example se iska use samajhte hain.

#For List
countries = [“usa”“india”“russia”]
forin countries:
    print(a)

#For Dictionary 
leader = {“usa”:”trump”, “india”:”modi”, “russia”:”putin”}
for x,y in leader.items():
    print(“country : “+x, ” leader: “+y)

#For String
city = “Bangalore”
for x in city:
    print(“x”)

For Loop ka range() function ke sath working

Range() function number sequence ko return karta hai. By default iski starting value ‘0’ hoti hai aur yah kisi bhi sequence ko +1 ke step me return karta hai. Agar aap chahe to iski starting value, end value aur step value change bhi kar sakte hain.

#Default Range() use
for y in range(9):
    print(y)

#User defined Range 
#start value = 2; end value=20; step=3
for y in range(2,20,3):
    print(y)

For Loop ka Else ke sath use

#For Loop + Else
for y in range(9):
    print(y)
else:
    print(“Range Function done its job”)

For Loop aur Nested Loop ka Combination

#For Loop + Nested Loop
state = [“delhi”,”rajasthan”,”madhyapradesh”]
city= [“delhi”,“jaipur”,“bhopal”]

for x in state:
    for y in city:
      print(x+,” : “,+y)