Python For Loop
for loop is very popular and widely used loop. In general when loop concept coming to the picture then immediately for loop concept come to our mind.
A for loop in Python is such type of loop which is especially used for iterate over a sequence(thar is either a list, or a tuple, or
a set, a string, or a dictionary)
With the for loop we can execute a set of statements, once for each item in a list, tuple, set, dictionary etc.
Print each car in a fruit list:
for k in cars:
Looping Through a String
We know strings contain a sequence of characters:
The range() Function
Loop through the letters in the word "Maruti":
for x in "Maruti":
#Python program to find the sum of numbers
for i in x:
In python the range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 by default , and ends at a specified number.
Using the range() function:
for x in range(5):
Range() function can take 2 arguments. However it is possible to specify the starting value by adding a parameter: range(2, 5), which means values from 2 to 5 (but not including 5):
for x in range(2,5):
The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 40, 4):
for x in range(2,40,4):
When a loop is embedded in another loop is known as nested loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
Print each color for every car:
for x in colors:
for y in cars: