Loops in Python
Hello, I am Şeyma, one of the participants of the SistersLab Women in Tech Academy project supported by Toplum Gonulluleri Vakfı (https://www.tog.org.tr/en/).
The project aims to empower people between the age of 20–28 who identify as women, through 3 months long software training and supporting trainings to increase their participation in the workforce in the IT sector.
You can access detailed information about the project using this link: https://sisterslab.co/women-in-tech-academy/
I will share with you what I learned about python as part of this beautiful project. I wish you good reading.
Let’s start with a short description of the first loops. To put it simply, loops mean that a process repeats a certain number of times. To explain why we need this, let’s take a computational process. After we write the code that will do a mathematical operation, we make the calculation and we are done. This block of code is scheduled for one-time execution. What if we need to do the same operation more than once? For example, a teacher is required to take the average of all the exams they take in a semester for all students in a class for that student. In this case, it would be a very long and laborious task to use the single-use code block every time. However, we can do this in the shortest and easiest way with loops. So we have to learn the loops.
For Loop:
In order to understand the for loop, it is necessary to talk about the definition of range first. Range is simply used literally and is used to show numbers that are in a certain range.
For example:
As shown in the example, unless the number written in the range is specified, it goes up one by one, starting from the zero index to the entered number index, including this index. However, it is possible to determine from which value to start in the range and how many times it will increase.
In the above example, we started our range from the second index and increased it by two up to the eighth index.
Now that we have learned the range definition, we can now move on to the for a loop.
What is a for loop in Python?
A for loop can iterate over every item in a list or go through every single character in a string and won’t stop until it’s passed every character.
Get İclal Şeyma Koca’s stories in your inbox
Join Medium for free to get updates from this writer.
Python For Loop Syntax:
- For start for a loop.
- Item is a separate value during each iteration. A temporary arbitrary variable is called.
- In distinguishes each item from the other(s).
- Is: what we want to repeat in the sequence.
- Colon: instructs to execute the following body of code.
With the for loop, the desired range can be printed, a certain mathematical calculation can be made in the desired range, or beautiful shapes can be drawn as something a little more fun :).
While Loop
As long as a condition we specify in while loops are true, we can repeat the lines of code we defined in the while block.
As long as i is less than 5 and this condition is true (True), the value i continues to be printed on the screen. However, we need to pay attention to the fact that we have to change the value of i at every step, otherwise, we will enter an endless loop.
Therefore, after the value of i is printed on the screen, we need to increase the value of i by 1. When the value of i is equal to 5, the while condition returns False and the loop is exited.
Break and Continue
Break ends the loop, continue ends that round of the loop and continues in the next loop.
As long as x is less than 5, it is printed to the screen, but when the continue command is executed in the round where x is 2, x is not printed to the screen, but the loop continues from the next round.
As long as x is less than 5, it is printed on the screen, but when the break command is executed in the round where x is 2, the loop ends and only 1 is printed on the screen.
My post about loops in Python ends here. I hope the post was helpful to you. See you in my next post.










