Python Loops
A loop is a programming structure that repeats a sequence of instructions until a specific condition is met. A loop statement in Python are used to execute a block of statements or code repeatedly for several times as specified by the user.
Purpose
The purpose of loops is to repeat the same, or similar, code a number of times. This number of times could be specified to a certain number, or the number of times could be dedicated by a certain condition is met.
Working
Consider the flow chart for a loop execution in Python.
In the flow chart if the test condition is true, then the loop is executed, and if it is false then the execute breaks out of the loop.
After the loop is successfully executed the execution again starts from the loop entry and again checks for the test condition, and this keeps on repeating until the condition is false.
Advantages of loops
There are the following advantages of loops in Python.
- It provides code re-usability.
- Using loops, we do not need to write the same code again and again.
- Using loops, we can traverse over the elements of data structures.
Python programming language provides following types of loops to handle looping requirements. Python provides three ways for executing the loops. while all the ways provide similar basic functionality, they differ in their syntax and condition checking time.
1. for loop
2. while loop
3. Nested loop
No comments:
Post a Comment