Python for Loop Tutorial with Examples to Practice - NareshIT
Python for Loop Tutorial with Examples to Practice

Python for Loop Tutorial with Examples to Practice - NareshIT

Introduction:

1. We know that Python is a more powerful language that offers great tools for data crunching and preparation, as well as for complex scientific data analysis and modeling.

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

3.?????Here Python?is used to provide us with numerous enriched functional libraries which may be getting 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 a 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 instructions repeatedly depending on the specified condition.

2.?????It has an almost similar syntax to other programming concepts 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 implement the various concept of programming when in Python we are going to implement the LIST, TUPLE, and DICTIONARY concepts.

5.?????The loop concept in Python is used to iterate over a sequence (list, tuple, string) or other iterable 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 a 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 of 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 the range function can be used with for loop.

Range in Python for Loop

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

2.?????It is basically used to return 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 that we can define at the time of use. These parameters are,

  • ??????Starting parameter value,
  • ??????Ending parameter value and
  • ??????The 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 whole execution it jumps 1 step after each item.

Python While Loop:

1.?????Like similar to the for loop Python also supports the concept of the 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 a 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 a statement repeatedly then it is used. A loop becomes an infinite loop if the condition given never becomes false. It keeps on running. Such loops are called infinite loops. In python, if we need to implement such a 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:

Similar as that the 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 statements based on specified conditions. Like a 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 skips the line, and the rest will execute as it is. So, the output of the program will be NARESHIT.

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 good hand on practice in terms of the practical industry-oriented environment which will definitely help you a lot to shape your future.

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

4.?????Our Expert trainer will let you know about every ins and out’s of 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 in us and our advice, and we assured you about your sure success.?

You can contact us anytime for your?Python Training?online as well as call us directly, or you can give us a missed call. And one of our customer care representatives will contact you asap.

Follow us for More Updates:?https://bit.ly/NITLinkedIN??

要查看或添加评论,请登录

社区洞察

其他会员也浏览了