Python For Loop Tutorial With Examples To Practice

Related Courses

Introduction:

  1. As we know that the Python is more powerful language which offer great tools for data crunching and preparation, as well as for complex scientific data analysis and modelling. 

  2. In most of the cases we often require parsing the data which is being written in different languages like C, C++ and Java.

  3. Here the Python is used to provides us a numerous enriched functionable libraries which may be get used to parse the data which is being written in other languages. 

  4. Here I am going to discuss the Python loop concept, which will let you know to learn how to use the loop concept in Python.

  5. Python has multiple loop concepts such as for loop, while loop and infinite loop mechanism.

  6. Most of the Python programming can be done using these loop mechanisms and they are more relevant in practical sense also.

  7. Here as stated earlier above, I am going to discuss the Python loop concepts, which will let you know how to use these concepts in programming.

What is Python for Loop?

  1. The loop in Python is basically used when we need to iterate a set of instruction repeatedly depending on the specified condition. 

  2. It is having almost similar syntax like other programming concept in its appearance when we are going to make the code.

  3. It has a simple structural approach so that it can be easily understood by the programmer.

  4. It is mostly used to implements the various concept of programming when in Python we are going to implement the LIST, TUPLE and DICTIONARY concept.

  5. The loop concept in Python is used to iterate over a sequence (list, tuple, string) or other iterables objects.

  6. Iterating over a sequence is called traversal.

  7. Loop continues until we reach the last item in the sequence.

  8. The body of for loop is separated from the rest of the code using indentation.

Let us consider the following example which will let you know how to write a loop  in Python. Mostly in Python 3 when we need to write an loop mechanism then it has to go through the following manner.

Python for Loop Syntax:

for val in sequence:

Let us understand the for-loop syntax with an example when we are going to create the list as below:

a=[10,20,30,40,50] // creation of list

for i in a: //applying the loop concept

print(i) 

When we are going to print the output will be 10,20,30,40,50.

In the above example, the execution started from the first item in the list, and it went on until the execution reached 5. It is a very simple example of how we can use a for loop in python. 

Similarly, when we are going to apply the range in for loop then we can access the index of the list. Let us consider another example to know how range function can be used with for loop.

Range in Python for Loop

  1. As you know that in python, the range is an Built-in function. 

  2. It is basically used to returns a sequence when it is used along with the List or Tuple application. 

  3. When we are going to apply the range function then it has three parameters which we can define at the time of use. These parameters are,

  • Starting parameter value, 

  • Ending parameter value and 

  • Step parameter value. 

Syntax:

for i in range (Starting parameter value, Ending parameter value, Step parameter value)

let us understand this with an example as discussed below.

a=[10,20,30,40,50] // creation of list

for i in range(0,len(a),1): //applying the loop concept

print(i) 

When we are going to print the output will be used to access the index value such as 0,1,2,3,4.

But if we need to access the value of the list using the range then the same program will be written as 

a=[10,20,30,40,50] // creation of list

for i in range(0,len(a),1): //applying the loop concept

print(a[i]) 

When we are going to print the output will be used to access the index value such as 10,20,30,40,50.

In the above example, the sequence starts from 0 and ends at 5 because the ending parameter is 5 and the step is 1, therefore the while execution it jumps 1 step after each item.

Python While Loop:

  1. Like similar to the for loop the Python also support the concept of While loop mechanism. 

  2. The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.

  3. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Syntax:

while (condition): 

 body of while 

 

let us consider the following example to implement the concept of while loop. 

a=[1,2,3,4,5] 

i=0 

sum=0 

while i<len(a): 

sum=sum+a[i] 

 i=i+1 

print(sum) 

When we are going to print the output then it will print 15.

Infinite Loop:

It is another approach in Python where if we need to execute statement repeatedly then it is used. A loop becomes infinite loop if the condition given never becomes false. It keeps on running. Such loops are called infinite loop. In python if we need to implement such concept then it can be done as follows.

Let us consider the following example.

a=1 

while (a==1): 

n=int(input("enter the number")) 

print("you entered:" , n) 

When this program will run then the output will be get appeared as 

Enter the number 10 

you entered:10 

Enter the number 12 

you entered:12 

Enter the number 16 

you entered:16 

Break in Loop:

  1. The Break in python is a control flow statement. 

  2. It is used to exit the execution as soon as the break is encountered. 

  3. Let us understand how we can use a break statement in a for loop using an example.

In the above example, as soon as the loop encounters the string “R” it enters the if statement block where the break statement exits the loop. Similarly, we can use the break statement according to the problem statements.

Example:

company = [‘N’,’A’,’R’,’E’,’S’,’H’,’I’,’T’]
 
for x in company:
    if x == "R":
        break
    print(x)

When the above program will run then, as soon as the loop encounters the string “R” it enters the if statement block where the break statement exits the loop. So, the output of the program will be NA.

Continue in Python for Loop:

Like similar as that of break statement, the continue can also be used. It should be get noted that we will use the continue statement when we need to skip some statement based on specified condition. Like break statement It is also a control statement, but it will only skip the current iteration and execute the rest of the iterations anyway.

Let us consider the same example as stated above as

company = [‘N’,’A’,’R’,’E’,’S’,’H’,’I’,’T’]
 for x in company:
    if x == "R":
        continue
    print(x)

When the above program will run then, as soon as the loop encounters the string “R” it enters the if statement block where the continue statement skip the line and rest will execute as it is. So, the output of the program will be NAESHIT.

Scope @ NareshIT:

  1. At Naresh IT you will get a good Experienced faculty who will guide you, mentor you and nurture you to achieve your dream goal.

  2. Here you will get a good hand on practice in terms of practical industry-oriented environment which will definitely help you a lot to shape your future.

  3. During the designing process of application, we will let you know about the other aspect of the application too. 

  4. Our Expert trainer will let you know about every in’s and out’s about the problem scenario.

Achieving your dream goal is our motto. Our excellent team is working restlessly for our students to click their target. So, believe on us and our advice, and we assured you about your sure success.