Editor choice

Looping statements in python | For loop | Range in python

What are loops?

Generally, loops are known as where the starting and ending points are the same and there is a complete path

so that we can travel on that loop as many times as we can

An infinite number of iterations can be done

We can use the concept of loops when we want to repeat a set of codes a certain number of times

let’s see how we can do it and how the loops are useful for us

Firstly there are 2 types of loops in python

The two kinds of loops are: for loop for counting loop and while loop to for conditional loop.

For loop:

In some cases, we want to repeat the condition many times at the case we can use for loop

The for loop in Python is used to process the items of any sequence, such as a list or a string,

one by one. so it is called as counting loops

Syntax

for <variable_name> in <sequences_type>:
    statements_to_repeat

Explanation

For keyword should be given followed by the Variable name followed by in and then the type of sequence ending with a colon :

The loop will be repeated till it reached the end of the sequence given

how for loop works

the for loop statement will have a variable name and the sequence in which it should go through

Initially, the 1st item will be taken as the value for the given variable and then it will do the operations given below the statements

after the operations are completed then the given variable will take the 2nd item in the sequence and proceeds for the further operations

this will be repeated until the end of the sequence given to it

Flowchart of For loop

EXAMPLE

for i in [10, 15, 20, 25] :
    print(i+2)

Output

12
17
22
27

Explanation

Firstly the variable is taking the value at 1st in the given sequence and then it is performing the operations with the statements given below the loop.

and then after printing the values it will get updated with the 2nd value in the given sequence

and it repeats till it reached the last value of the given sequence

Range function in For loop:

For the number-based lists, we can specify the range() function to represent a list like the following example

EXAMPLE

for val in range(3, 18,2) :
    print(val)

Output

3
5
7
9
11
13
15
17

Explanation

In the above loop, range(3, 18) will first generate a list [3,4,5,…,16,17] with which for loop will

work. we need not define loop variable (Val above) beforehand in a for loop

As mentioned, a range() produces an integer number sequence. There are three ways to define a range

  • Method1
    • range (stop)

elements from 0 to stop-1, incrementing by 1

  • Method2
    • range (start, stop)

elements from start to stop-1, incrementing by 1

  • Method3
    • range (start, stop, step) elements from start to stop-1, incrementing by step

Conclusion

The start value is always part of the range. The stop value is never part of the range. The step shows the difference between two consecutive values in the range,

If start value is omitted, it is supposed to be 0. If the step is omitted, it is supposed to be 1.

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!๐Ÿ‘‹
Can we help you?