Python for loop

Python for loop

Now, we are going to explore a very interesting loop, which is the for loop. Basically, the for statement is used to iterate over a sequence, like some string, list, tuple, or some other iterable object.

For example, Let’s consider a simple program, where we would be iterating through every character in the string ‘GyaniPandit’. With the program, you would also have an idea about how we can write a for loop here.

As you can see, in the above program, we have the name1 variable, with the value assigned as ‘GyaniPandit’. After this, we have the for loop, in which, the character is a variable, and the name1 refers to the sequence that we are iterating through. Within the loop, we are printing the character. The thing is that, with the variable character, we have a single character from the string, in every iteration. For example, Let’s have a look at the output –

G
y
a
n
i
P
a
n
d
i
t

As you can see, we have the output, character by character. So, this makes things a bit clear, that we are iterating through a sequence, going through the different elements in the sequence.

So, we can understand from the above code, that into the variable (in this case, the variable is character), there is one value from the sequence, in each iteration. Here, the loop continues till we reach the last element in the sequence.

Python for loop

Now, Let’s try to use the for loop with some list. Well, the list is a mutable sequence of elements. We will have a look at the concept of lists in a detailed way, but here, we are going to use it with looping, so we will simply see how we can create a list, and some other essential things, about the list. Have a look at the below example, where we are iterating through a list, which contains some names of Fruit’s. Have a look –

As you can see, in the above program, we have a reference variable fruit’s, to which, we have assigned a list, which is a list of fruit names basically (this is just a list, which contains some strings). After that, we have a for loop, where we have a fruit variable, which is basically going to have one fruit name in every iteration, from the fruit’s list. In the loop, we are simply printing the fruit. The output of the above program comes out to be something like this.

Apple
kiwi
Guava
Mango

As you can see, we got the names of the fruit’s one by one, from the list. So, in this way, we can make use of the for loop, to iterate through some sequence.

Now, Let’s have a look at the range function, since as we move ahead, you would come across a lot of situations, when you would see and use the range function, due to which, it becomes important for us, to understand the range function.

The range function that we are going to look at now, is going to return a sequence of numbers, between the given range.

First of all, Let’s have a look at a simple program, which demonstrates the range function, and then we would understand that what it does.

As you can see in the above program, we simply have the for loop, and at the place of some sequence, we have used the range function this time. Basically, the range function is going to return the sequence of the given numbers in the range. First of all, Let’s have a look at the output of the above program and then we will head to explanation of some things.

0
1
2
3
4
5
6
7
8
9

As you can see, as an output of the above program, we had the values from zero to nine, into the number, in each iteration. Now, Let’s understand the range function in a greater detail, so that it becomes easy for us, to use it later.

Basically, the range function takes in three arguments, which is start, stop, and step. We can simply say that the range function allows us to have a series of numbers in the given range, depending on the arguments. Let’s have a look at these different parameters that we have here.

  • start → This is the integer starting from which, the sequence is to be returned.
  • stop → This is the integer up to which the sequence is to be returned. The stop it’self is excluded, so we get the sequence up to stop – 1.
  • step → This is the integer, which determines the increment between each integer in the sequence to be returned.

Now, in the previous program, when we used the range function, we only passed one argument. So, when you are passing only one argument, you are actually passing a stop value, and the start value defaults to zero. So, it depends on what are we passing the arguments, accordingly we are going to get the sequence. Let’s have a look at another program, where we are specifying two arguments, and this time the first argument would be the start, and another argument would be the stop. Let’s have a look now –

As you can see, in the above program, this time, we have specified the start and the stop. So, we can make a simple guess, that the sequence that we are going to get, is from 1 to 10, since the stop it’self is excluded. Also, in the print function, you might be observing the end argument. We have specified the value as space to the argument, due to which, in the output, after the print function executes, instead of a newline, a space would be added, and the output would look something like this –

1 2 3 4 5 6 7 8 9 10

As you can see, we could get the integers from 1 to 10. The stop here was specified as 11, so we got the final integer as 10.

Also, here we have not specified the third argument, which is the step. So, if the step is not specified, it defaults to 1.

Now, Let’s also add the step argument, and let’s see how is the sequence that we get.

As you can see, in the above program, we have specified all the three arguments, which is the start, stop, and the step as well.

Now, the sequence that we are going to get is going to start from 1, and the step this time is of 2, so after one integer, the next integer will be 2 steps. So, if the first integer is 1, then the next integer is 3. Now, if the step was not provided, it would have considered it as 1. But since we are giving the step here, we are getting the things accordingly. The output looks something like this –

1 3 5 7 9

As you can see, we got the first integer in the sequence, as 1, and then the next integer is 3, since the step is given as 2. This may seem quite confusing in the very start, but once you start using this, you would simply get familiar to this, and be comfortable in using the range function, and the for loop as well.

Now, Let’s have a look at a very simple program, in which we are going to calculate table of some given number. Let’s have a look at the program first, and then we will explain the things.

As you can see in the above program, we have taken a number as user input, and then we have used a for loop, where we have the sequence from 1 to 10(you can get your desired sequence). Within the loop, in every iteration, we are multiplying the my_number, with the value of i, and remember that the value of i is going to be changing with the iterations. So, in the first iteration, i would be 1, then 2, then 3, and so on, and the last value would be 10. The output looks something like this –

Please enter the number:12

12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120

As you can see, we could simply get the required table. You can also try this program, so that you can become familiar with the concept of for loop, and also with the range function.

Let’s perform another simple program, in which we would simply print the even numbers from 1 to 20.

As you can see in the above program, we directly have the for loop, within which we have used the range function, with the start as 1, and the stop as 21(we have the stop as 21, so that we can get 20 as output). then, for every number in the loop, we are checking that if the number is divisible by 2, we are simply printing it because it is even. The output is something like this –

2 4 6 8 10 12 14 16 18 20

As you can see, we got the even numbers from 1 to 20. So, doing these simple programs can make you familiar with the for loop, and once you get familiar with the for loop, you would be able to comfortably use it in your programs.

As we move ahead, we would make use of the for loops, as and when needed. But you can do some more programs, related to the for loop, and the looping concept overall, so as to get familiar and get a better understanding of the concept.