LOOPS - FOR LOOP
Sakshi

Sakshi @bellatrix

About: Loves programming! Web Dev Enthusiast. Getting my hands dirty with ANGULAR. Available for hire

Location:
India
Joined:
Sep 11, 2021

LOOPS - FOR LOOP

Publish Date: Mar 29 '24
0 0

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...

Comments 0 total

    Add comment