For loop in Python
FOR LOOP
Loops are nothing but a program which helps to do a specific work in a given number of a time period.
whereas, its directly helps to save time as well as man power.
Here if we print a list one by one in term as-
code👇
list1 = ["Harry", "Larry", "Carry", "Marie"]
print(list1[0], list1[1], list1[2])
Harry Larry Carry
Whereas, here we print list into the for loop term as-
code👇
list1 = ["Harry", "Larry", "Carry", "Marie"]
for items in list1:
print(items)
Harry
Larry
Carry
Marie
note: here we used the for loop, which iterate the list items one by one by giving less arguments.
note: We also can iterates the tuples by the "for loop", but not every datatype.
With list of lists numbers:
code👇
list1 = [["Harry",10], ["Larry",20], ["Carry",30], ["Marie",40]]
for item, lollypop in list1:
print(item, "is number", lollypop)
Harry is number 10
Larry is number 20
Carry is number 30
Marie is number 40
For loop by the Dictionary:
code👇
list1 = [["Harry",10], ["Larry",20], ["Carry",30], ["Marie",40]]
dict1 = dict(list1)
for item, lollypop in dict1.items():
print(item, "is number", lollypop)
Harry is number 10
Larry is number 20
Carry is number 30
Marie is number 40
Comments
Post a Comment