‘Loops in Python’
Python makes use of loops, control, and conditional statements to overcome this hurdle. This article will help you understand loops in Python and all the terminologies that surround loops.
- What are Loops in Python?
- What is for loop and while loop?
What Are Loops In Python?
Loops in Python allow us to execute a group of statements several times. Let’s take an example to understand why loops are used in Python.
Suppose, you are a software developer and you are required to provide a software module for all the employees in your office. So, you must print the details of the payroll of each employee separately. Printing the details of all the employees will be a tiresome task, instead, you can use the logic for calculating the details and keep on iterating the same logic statement. This will save you time and make your code efficient.
The illustration below is the flowchart for a loop:
The execution starts and checks if the condition is True or False. A condition could be any logic that we want to test in our program. If it’s true it will execute the body of the loop and if it’s false, It will exit the loop.
Note: python relies on indentation, other programming languages use curly brackets for loops.
What Is ‘for’ Loop and ‘while’ Loop
A for loop is used to execute statements, once for each item in the sequence. The sequence could be a list, a Dictionary, a set, or a string.
A for loop has two parts, the block where the iteration statement is specified and then there is the body which is executed once every iteration.
Unlike the while loop, we already specify the number of times the iterations must execute. for loop syntax takes three fields, a Boolean condition, the initial value of the counting variable, and the increment of the counting variable.
Look at the example to understand this better:
Here we were iterating through the list. To iterate through a code for a specified number of times we could use the range() function.
for loop with string :
for loop with List :
for loop with Tuple :
for loop with dictionary :
Range Function
Range function requires a specific sequence of numbers. It starts at 0 and then the value increments by 1 until the specified number is reached.
For example:
Note: the range (3) does not mean the values from 0–3 but the values from 02.
While Loop :
In an easy-to-understand example, we can see how the while commands work, by dictating to the program that we would like for it to continue to print out values until certain conditions are met. In this case, we will tell our program to continue printing values as long as it is less than or equal to fifteen.
ex.1
ex.2
Using While Loop With a List :
We have a list of numbers called number_list with elements [1, 2, 3, 4, 5].
We initialize a variable index to 0, which will be used as an index to access elements in the list.
The while loop is used to iterate over the list as long as the condition index < len (number_list) is true.
For each iteration, the element at the index is printed using the print(number_list)index.
After each iteration, we increment the value of the index by 1 with index +=1.
Using While Loop With a Tuple :
Using While Loop With a Dictionary :
Using While Loop With a Set :
Let’s meet on the next Article,
THANK YOU,
#vevcodelab