#if you want iterate the list one by one you can simply use for loop
list1 = [1,True,"Meerab",8.9]
for i in list1:
print(i)
#if you want to iterate the list along with an index number
#The index value starts from 0 to (length of the list-1)
list2 = [3,9,"Ahmed",2.98,"Tom"]
#To find the len of list we can builtin function len()
size = len(list2)
#iterate a list
for j in range(1,size):
#you can give a range that you want to iterate
# print each item using index number
print(list2[i])
# a 'while' loop runs until the condition is broken
a = "apple"
while a == "apple":
a = "banana" # breaks loop as 'a' no longer equals 'apple'
# a 'for' loop runs for the given number of iterations...
for i in range(10):
print(i) # will print 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
# ... or through a sequence
array = [3, 6, 8, 2, 1]
for number in array:
print(number) # will print 3, 6, 8, 2, 1
import itertools
list_1 = [1, 2, 3, 4]
list_2 = ['a', 'b', 'c']
# loop until the short loop stops
for i,j in zip(list_1,list_2):
print(i,j)
print("
")
# loop until the longer list stops
for i,j in itertools.zip_longest(list_1,list_2):
print(i,j)
#if you want iterate the list one by one you can simply use for loop
list1 = [1,True,"Meerab",8.9]
for i in list1:
print(i)
#if you want to iterate the list along with an index number
#The index value starts from 0 to (length of the list-1)
list2 = [3,9,"Ahmed",2.98,"Tom"]
#To find the len of list we can builtin function len()
size = len(list2)
#iterate a list
for j in range(1,size):
#you can give a range that you want to iterate
# print each item using index number
print(list2[i])
# a 'while' loop runs until the condition is broken
a = "apple"
while a == "apple":
a = "banana" # breaks loop as 'a' no longer equals 'apple'
# a 'for' loop runs for the given number of iterations...
for i in range(10):
print(i) # will print 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
# ... or through a sequence
array = [3, 6, 8, 2, 1]
for number in array:
print(number) # will print 3, 6, 8, 2, 1
import itertools
list_1 = [1, 2, 3, 4]
list_2 = ['a', 'b', 'c']
# loop until the short loop stops
for i,j in zip(list_1,list_2):
print(i,j)
print("
")
# loop until the longer list stops
for i,j in itertools.zip_longest(list_1,list_2):
print(i,j)