Introduction
Python loops are important in terms of automation in programming because they help you do repetitive tasks without having to write the same line of code several times. Would you find it a pleasant experience writing a hundred printed statements just to say,"Hello!" to one hundred different people? It’s tedious, isn't it? This is where Python loops have to come in, allowing you to browse a sequence of numbers or elements without the tedious task of repetition.
So that you understand Python loops better, let's say you have a list of your popular fruits (apples, bananas, and strawberries). Instead of writing separate printing instructions for each item/fruit, you can easily use a loop to iterate through this list.
Check this example⬇️
Do you see how clean and organized the loop above is? This Code instructed Python that: " for each fruit of this list, print it!".
It's direct and efficient. You can even customize it. If you want to count the number of fruits, you can wrap it in a function and keep track of something as simple as a counter.
Now, let's add some speed. If you wanted to mention the number of fruits you have, you can update your code like this⬇️
Using this approach above, you have not only printed your fruit, but you have also numbered them. And this is where the versatility of the loops really start to get interesting!
While Loop
Now the While loop works a little differently. The while loop keeps running as long as a specified condition is true. It's very useful when you don't know in advance how many times you will have to complete the loop.
Here is an example⬇️
In this case above, the While loop checks if the counter is less than 3.
As long as this is true, it will print "work ..." and increment the counter by 1.
Ps: This loop would work three times before stopping. So, if you are in a situation where the number of iterations depends on certain dynamic factors, such as the entry of the user or a total during execution, it is your reference loop and remains super useful in many contexts.
Applying loop in practice with range functions
For example, if you wanted to print numbers from 1 to 10, you can do it succinctly with⬇️
What is happening above is that the range (1, 11) generates a sequence of numbers from 1 and ending just before 11th. It's so simple!
Do you rather want to count in 2's,check here⬇️
Here we started at 0, finished at 20 years old and counted by 2s.
Ps: Try this and print your result on your python dashboard
The practical applications of these loops cannot be underestimated. Loops are fundamental to exploiting the full power of Python. They give life to your ideas by automating repetitive tasks, effectively managing data and offering flexibility in the way you perform code. So, the next time you are having trouble with repeated tasks in coding, remember loops are there to simplify your life in the most fun way!
Thats not all there is to your coding, if you want more great tips and tricks,join to our newsletter and Fundamentals of Data Science and AI community to get access to special Python tutorials and inside knowledge on how to become a python pro. It only takes a minute, and you will be glad.Click here to Join now.