WHAT ARE LOOPS ?:
Now, we are onto some cool stuff in programming. Guys this is a really important topic to learn when you are learning programming. We use loops when we want to iterate a series of commands over and over again. It makes all of our lives very much simpler. For instance, if you want to find if a number is even or odd, you can use the following code:
If x%2==0:
print(‘x is an even number’)
elif x ==0:
print(‘x is equal to zero’)
else:
print(‘x is an odd number’)
Seems simple enough, right? But what if I asked you to find all the even and odd numbers from 0 to 10? Confused? But why, I just gave you the code to find if a number is even, odd or zero, all you have to do is type this code ten more times (because zero is included). But let’s just give it a shot anyway.
THAT IS 94 LINES OF CODE, AND ALL OF THIS FOR WHAT? JUST TO
FIND TO FIND OUT IF A NUMBER IS EVEN OR ODD? It does work fine though.
But this doesn’t mean this is the most efficient way to do
it.
It is lengthy af and is probably going to take up all your
time. Now what if I asked you to find the even and odd numbers from 0 to
1000... Well maybe, it will take you a week to write that code or you will just
copy and paste the same code over and over, but still it will take a whole day
or two.
Now how do we get around this? We use Loops. Loops will
iterate through the code I wrote earlier as many time as you want. If you want
to find all the even and odd numbers from 0 to 10 or 0 to 100 or 0 to 1000. All
you have to do is set the limit and the loop will go through it 10 times, 100
times or 1000 times.
There are several kinds of loops and we will be studying the
ones that we use a lot when working with Python.
FOR LOOPS:
We can use for loops to iterate through a line of code. For
example:
Can you see that substantial decrease in the length of the
code? It’s crazy. And all we have is the original code that we designed for a
single number with an extra line.
And it works fine too
This is why we use loops to shorten these unnecessarily long
codes so that it is easier to read and execute.
range()
FUNCTION:
We can use the range()
function to create a sequence of integers and the for loop to iterate through
it. The range()
function takes two parameters. The number where the sequence starts and the
number where it stops. The sequence will
include the number where it starts but not the number where it ends. For
example: range(0,10) will include numbers from 0 to 9. It will exclude the last
number of the sequence. So if you want to find the numbers from 0 to 10 you
must set the limit to 11.
CONGRATULATIONS !!! You have learnt about for loops in Python. Now you know how to use Python to execute the same code as may time as you want. In the next lesson, we will be learning about another kind of loops, while loops, so stay tuned. Trust me this knowledge will help you create programs that are a lot easier to understand and execute. Have fun with it !!! Create some cool programs. Good luck !!!!
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER. IF IT DIDN'T feel free to ask any further queries in the comment section below.
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback