Published:
print("-----")
print("for loop example ")
for letter in 'Python': # First Example
print ('Current Letter :', letter)
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # Second Example
print ('Current fruit :', fruit)
#Output :
#print ('Good bye!')
#Current Letter : P
#Current Letter : y
#Current Letter : t
#Current Letter : h
#Current Letter : o
#Current Letter : n
#Current fruit : banana
#Current fruit : apple
#Current fruit : mango
#Good bye!
print("-----")
print("Iterating by Sequence Index")
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print ('Current fruit :', fruits[index])
#Output :
#Current fruit : banana
#Current fruit : apple
#Current fruit : mango
print("-----")
print("Using else Statement with Loops")
for num in range(10,20): #to iterate between 10 to 20
for i in range(2,num): #to iterate on the factors of the number
if num%i == 0: #to determine the first factor
j=num/i #to calculate the second factor
print ('%d equals %d * %d' % (num,i,j))
break #to move to the next number, the #first FOR
else: # else part of the loop
print (num, 'is a prime number')
#Output :
#10 equals 2 * 5
#11 is a prime number
#12 equals 2 * 6
#13 is a prime number
#14 equals 2 * 7
#15 equals 3 * 5
#16 equals 2 * 8
#17 is a prime number
#18 equals 2 * 9
#19 is a prime number