If you are here, it means you are going to be a coder soon. You will solve real-world computer problems, make your own programs, and a lot more. Yeah, sounds good!
Now let's talk about
for. It is also a loop like
while. Let's see how it works.
Python for Loop
Python for loop Syntax
for iter in sequence: statement statement ...
sequence is a sequence like list, tuple, string, etc.
iter is a variable that takes the value of each item in the sequence.
The body of the for loop consists of all the indented statements below
for iter in sequence:.
Python for loop Examples
colors = ["Blue", "Green", "Yellow", "Orange", "Red", "Brown"] for color in colors: print(color)
By looking at the output, you might have got some idea of what the
for loop does. Let’s understand the code.
colors is a list here containing six items.
for color in colors →
color is a variable which goes to each element in the list
colors and takes its value.
So, in the first iteration,
color is equal to the 1st element of the list. In the second iteration,
color is equal to the 2nd element of the list, and so on.
Therefore, in the first iteration, the value of
color is “Blue” and so
print(color) printed Blue. Similarly, in the second iteration, Green got printed, and so on.
forloop is also represented by equal indentation (margin) from the left.
Python Example of Sum of Marks using Loop
marks = [78, 98, 50, 37, 45] sum = 0 for m in marks: sum = sum + m print(sum)
You must have understood the code. If not, then let us explain a bit.
sum = 0 → We are taking a variable
sum and assigning it an initial value of 0.
In the 1st iteration,
m is 78. So,
sum + m (0 + 78 = 78) makes the value of
sum as 78.
In the 2nd iteration,
m is 98. So,
sum + m (78 + 98 = 176) makes the value of
sum as 176.
After all the iterations,
sum becomes 308.
forloop with lists, strings, tuples and other sequences in detail in later chapters.
Python range() function
range() function is used to generate a sequence of numbers.
Python range Syntax
range(start, stop, step_size)
start - Integer from which the sequence starts. It is optional. Its default value is 0.
stop - Integer before which the sequence stops. It is mandatory.
step_size - Increment between each integer in the sequence. It is optional. Its default value is 1.
range(10) generates ten integers starting from 0 to 9. Here, start = 0, stop = 10, step_size = 1.
range(1, 10) generates nine integers starting from 1 to 9. Here, start = 1, stop = 10, step_size = 1.
range(1, 10, 2) generates the integers 1, 3, 5, 7, 9. Here, start = 1, stop = 10, step_size = 2.
Python range Examples
In the last chapter, we printed the first 10 natural numbers using a
while loop. Let’s do that using a
for n in range(1, 11): print(n)
The next example prints the multiplication table of 14 using a
for i in range(1, 11): print(i*14)
Now let’s print all the even numbers from 10 (included) to 20 (included).
for num in range(10, 21, 2): print(num)
That was cool, right? We could print the desired results using just 2 lines of code. We suggest you practice more questions on loops from our practice section.