DEV Community

Cover image for LOOPS - FOR LOOP
Sakshi
Sakshi

Posted on

LOOPS - FOR LOOP

HI AGAIN

PYTHON UNLEASH'D 04

Back with basics, grinding on loops in python. Life becomes really hard when you have to manage so many things

Anyways, focus on agenda!

We know about for loop, lets look at its basic syntax first.

for loop with list

for name in list1:
    print(name)
Enter fullscreen mode Exit fullscreen mode

for loop within range

list2 = [1,2,3,4,5,6]
for i in range(0,6,2):
    print(list2[i])

Enter fullscreen mode Exit fullscreen mode

for loop with dictionaries

d = dict()
d['a'] = 123
d['b'] = "hehe"

for i in d:
    print(i,d[i])
Enter fullscreen mode Exit fullscreen mode

zip() used to traverse two list together

fruits = ["apple", "banana", "cherry"]
colors = ["red", "yellow", "green"]
for fruit, color in zip(fruits, colors):
    print(fruit, "is", color)
Enter fullscreen mode Exit fullscreen mode

Tuple

tup = (1,2,3,4,"holy")
for a in tup:
    print(a)

Enter fullscreen mode Exit fullscreen mode
t = ((1, 2), (3, 4), (5, 6))
for a, b in t:
    print(a, b)

Enter fullscreen mode Exit fullscreen mode

Good examples
See you in next blog guys!
Happy reading...

Top comments (0)