While the decision control statements add some good power to the code and can help you to do a lot more than before, it is definitely a good idea to work with the loops inside of the Python program. These are helpful any time that you need the program to repeat something inside of the code, but you don’t want to write the code out over and over again. For example, if you would like the code to list all the numbers from 1 to 10, it probably wouldn’t be much fun to write out the code to tell the compiler to write out 1 and then write it again for 2, and so on until you reach 10. You could use the idea of looping in Python to get this all done in one block of code to save time and to make the code easier to read.
Loops are pretty easy to work with. They are basically going to tell the compiler to keep on reaidng the same block of code over and over until some condition is met. If you would like the code to count from 1 to 10, you would just need to tell the code to stop going once it got higher than 10 (we will look at some codes that show how this is done). You do need to be careful with this though because if you don’t set out the condition ahead of time, the code is going to end up in a continuous loop and will get stuck. So always make sure that you set out the break of the code, or the part that will make the loop stop and move on to another part of the code to read.
There are several types of loops that you can work with to help make the program work the way that you would like. The two we are going to talk about in this guidebook are the while loop and the for loop.
What is the while loop?
One of the loop types that you are able to work on is known as the while loop. This is the one that you will want to use whenever the code should go through the cycle a fixed amount of times. You don’t want this code to go through the cycle indefinitely, but you want to make sure that it goes through the right amount of times. Often this is the one that will repeat at least one time before it sees whether the code is true or false so it can be helpful if you would like to use it in this manner. To understand better how this is going to work, let’s look at a good example of the while loop:
#calculation of simple interest. Ask user to input principal, rate of interest, number of years.
counter=1
while(counter <= 3):
principal=int(input(“Enter the principal amount:”))
numberofyeras=int(input(“Enter the number of years:”))
rateofinterest=float(input(“Enter the rate of interest:”))
simpleinterest=principal * numberofyears * rateofinterest/100
print(“Simple interest=%.2f” %simpleinterest)
#increase the counter by 1
counter=counter + 1
print(“You have calculated simple interest for 3 time!”)
Once you have the time to put this in your compiler and execute the code, you will see that the output is going to come out so that your user is able to place in the information, any information, that they wish to have computed. The program is going to be able to figure out the rates of interest as well as the final amount based on the numbers that the user places into the system. Right now this loop is set up to go through three times, but you are able to change it around and get it to do more loops if you would like.
How is the for loop different?
There are many times that the while loop will be your friend and can help you to get a lot of things done, but there are other times when the for loop is useful. The for loop is considered a more traditional way to write out the loop, but there are many times when you will want to use it.
In this kind of loop, the user won’t be able to go in and give the right information or determine when the loop is able to stop. Rather, with the for loop, Python will go over the iteration in the order that they show up inside of your statement and it will show this information on the screen, without needing input from someone else, until it reaches the end. A good example of how this may work includes:
#Measure some strings:
Words=[‘apple’, ‘mango’, ‘banana’, ‘orange’]
for w in words:
print(w, len(w))
Now, when working on the example above, you will be able to put it into your code and when it is executed, the four fruits will come out onto the screen, in the exact order that they are already written. Any time that you would like to have them come out on the screen in a slightly different order, you will just need to go back to the syntax and change that up. Once they are in the syntax and ready to be used inside of the code, you will not be able to make these changes.
The nested loop
The final type of loop that you may find helpful to use inside of your Python code is the nested loop. When you are using a nested loop, you are basically taking one loop and placing it inside of another one and then allowing both of them to keep on running until they are done. There are several times when this can be useful to help you to get things done. A good example would be when you would like to write out a multiplication table that starts at 1 and goes all the way to 10. Here is an example of how that would work:
#write a multiplication table from 1 to 10
For x in xrange(1, 11):
For y in xrange(1, 11):
Print ‘%d = %d’ % (x, y, x*x)
When you got the output of this program, it is going to look similar to this:
1*1 = 1
1*2 = 2
1*3 = 3
1*4 = 4
All the way up to 1*10 = 2
Then it would move on to do the table by twos such as this:
2*1 =2
2*2 = 4
And so on until you end up with 10*10 = 100 as your final spot in the sequence.
There are so many reasons why you would like to use a loop inside of your code. You will be able to use it as a way to clean up your code while getting the compiler to go through the same block of code over and over again. The compiler will continue to go through this code until the condition is no longer considered through and then it will move on to either end the program, or move on to the next part if there is more. This can open up a lot of things that you are able to do with your code while keeping things easy to use.