Intro
This tutorial will teach you about iteration, which is how you can repeat instructions in Python. In this tutorial I will be using lists, strings and integers to illustrate iteration.
Requirements
- Know basics of Python 3 if not read here
- Know about the lists data-structure, if not read here
- Knowledge of iteration for computer science will help
What are they?
In Python you can create loops using the keywords “while” and “for”.
While Loop
Lets start with a while loop. To define a while loop you need the “while” keyword and a expression to check, this will be used to define whether to run the loop.
Infinite
Python allows us to create a loop that will always run we can do this using the code below.
|
|
When run you will just see “hello” being outputted multiple times. This loop will never end.
Counter
Another way of running a while loop is to use a counter to determine when to “breakout” of the loop.
|
|
This loop will check whether the counter variable not is equal to 5, if it is the loop will “break”.
When run this code should output:
|
|
You can think of the expression in the while loop as a if statement and if the statement is true the loop will run; if not it will “break”.
For Loop
Another form of iteration found in Python is a for loop. this will use the “for” keyword.
Range
The first way of using a for loop is by using the range function. This allows for a start, stop and step value to be entered. This loop will also not require a separate counter variable.
As you can see in the code below the current value is being stored in the counter variable. The range function is also saying it will start from 0 and iterate until it reaches 5.
|
|
When this code is run you should see:
|
|
This range function can also do some smart things. For example if you want to start from 0 you can omit the 0 and just have 5. This will mean it will start from 0 and iterate until 5.
As spoke about earlier can also define a step. Which you can see in the code below, the step is the last argument.
|
|
When run you should get the following output:
|
|
Iterable
The next way of using a for loop is to omit the range function and use a variable that has a iterable data-type like a list.
You can see this being used below:
|
|
When run you should see the following output:
|
|
A string is also counted as a iterable
Extras
Here are some extras that will work with both loops.
Break
The “break” keyword will stop the iteration of a loop. You can put this anywhere inside your loop.
|
|
As you can see, the code will only output 0 and then stop.
|
|
Continue
The “continue” keyword allows for skipping to the start of the loop.
|
|
As you can see this code will iterate through the range from 0 to 4. Because the “continue” keyword is placed before the print("hello")
. It will instead go to do the next iteration of the loop.
When this code is run you should see the output shown below:
|
|
Both “break” and “continue” can be used in the same loop
Simple Calculator
We will now resume our simple calculator from the if statement tutorial.
We now want to allow unlimited values to be entered. Here is what you will need:
- Ask user what operation to use (+, -)
- Get number (store in list) (float)
- Ask whether another number is needed?
- If yes go to step 2 (while loop)
- Calculate the numbers using chosen operation
- Output the answer
Try to code your own version before looking at the completed code below.
|
|
Lets test our application by adding 2 and 4. We should get an answer of 6.
|
|
As you can see my code output indicates it works. If yours works well done. If it doesn’t have a look at mine and see where you went wrong.
Further Expansion
You should probably make sure that the calculator will work with more than 2 numbers.
If you have read my tutorial on functions, have a go and convert your code to use them. You could also add more operators like multiply and divide.
Research
Python has a lot of features, you should probably know about some more data-types like dictionaries and sets. Also learning about Python’s standard library will help you in future projects.