In this article will talk about Python for Loop. If you didn’t see our last article How to Use If and If-Else Statements in Python then click the link and know more about python.
The for loop is one of Python’s most used control structures. But first, let’s go through some of the new terminologies we’ll be utilizing in the for loop examples.
Python range() function
The range() function returns an integer sequence. The range takes the following general form:
range(start, end, increment)
It indicates that the range function returns an object that generates a sequence of integers in stages of increment from start to finish (but not including end). If no increment is specified, the increment is set to one by default.
Consider the following scenario:Function Sequence of values produced range(1,7) 1,2,3,4,5,6 range(7) 0,1,2,3,4,5,6 range(1,7,2) 1,3,5
- The range function returns the number up to 6 in the first example. Because the range function does not include the last number.
- If the start is not define in the range function in the second example, the default start value is 0.
- The start, finish, and increment are all defined in the third example. As a result, the number will begin at 1 and increase by 2 steps until it reaches 5.
Now that everything is clear, let’s move on to the for loop.
Python for loop statements
Let’s pretend I’m asking you to add the first five integers in the number system. Isn’t it extremely simple? Now, if I ask you to develop a program that sums the first five integers in the number system, you can certainly do it. Because we already know how to read numbers and add and subtract them.
But what if you had to sum the first 100 numbers of a number system or even the first 1000 numbers? Dealing with these kinds of difficulties will be much more difficult if we use an older form of summing. We’ll utilize the for loop to cope with these kinds of issues.
What exactly is a loop in this situation? A loop is a process of repeatedly executing a statement or a sequence of statements. The sequence of statements that will be executed repeatedly only has to be written once. Iteration of the loop refers to the execution of a set of statements in a loop.
When we wish to run a set of statements a certain number of times, we utilize the control statement. For instance, suppose we wish to add the first 100 numbers in the number system. The number 100 is set here. The number of times a sequence of statements must be processed is known in advance in the for a loop. The for the statement in Python offers a way to keep track of how many times a set of statements has been run.
Enough with the theory; let’s look at an example. We’ll develop a Python program to print out the sum of the first n odd numbers in this example. The n is the user’s input, which must be an integer.
Example of for loop
def OddSummation(n): total = 0 for i in range(1,n,2): total+=i return total def main(): number = int(input("Enter the number: ")) print("The summation of all the odd numbers between 1 and ",number, " is ", OddSummation(number)) main()
Output:>>> Enter the number: 100 The summation of all the odd numbers between 1 and 100 is 2500 >>> Enter the number: 10 The summation of all the odd numbers between 1 and 10 is 25 >>> Enter the number: 7 The summation of all the odd numbers between 1 and 7 is 9
As you can see, we’ve achieved our objective/output.
To summarise the code, we accept the user’s number as input in the main function and use the int function to convert numbers to integers. The input integer is then pass to the OddSummation function.
We first create a variable total and give it the value 0 in the OddSummation function. The summation was then compute using the for loop. The sum of an odd number up to the stated value of n is what we’re looking for. Odd numbers include 1,3,5,7,9,… and so on. That is why we use the term “range” (1,n,2).
The initial value of i is now to be 1 inside the for loop. Because the number’s range begins at one,total +=i can also be written as total = total + i
The i has a default value of 1 and the total has a default value of 0. As a result, the sum will be 1 (0+1), and this 1 will be save in the total variable, or the value of the total will be 1.
As i increase with the value of 2, the new value of i will be 3. The new total value will be 4 (1 + 3).
The new value of i is going to be 5. (increment of 2). The total we obtained from the previous procedure now has a new value of 4. As a result, the total will be 9 (4+5). And this process will continue till the last value of i is execute. After that, it will return the total’s final value.
One thing to keep in mind is that the range function does not take into account the most recent value. If we want to display the result of a sum of integers from 1 to 100, we’ll need to utilize the range function, which looks like this: range (1,101). As a result, whenever we discuss summation, we always refer to range as range(1, n+1).
General Format of for Statementfor variable in sequence: < block S of statements >
Flow diagram of for loop structures
If you want to learn more about python then visit the official documentation of python.