Iteration - Python

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Types of Iteration / Loop

Programming languages tend to have different types of iteration available, for example you have: For, Repeat, While.

  • For – will run a set number of times
  • Repeat – will run at least once and then check
  • While – will check first so may never run

For

Should be used when you need to repeat a certain number of times. This could be a known value such as ten times or a value we can calculate (for example 10 items a list, a bubble sort will definitely sort the list with a loop of 10 iterations and we need to repeat the loop 10 times).

In Python, for loop is usually used with the range() function, so to print a number from 1 to 10:

for i in range(1,11):
     print(i)

The value of i will initially be 1 (specified in the range command), it will then increment this by 1 each time the loop is repeated. So the fourth time through this loop i will equal 4. Essentially the value 11 is the stopping condition, ie repeat while i is less than 11.

Alternatively you could give range a single value, in this case it will start from 0. So the code below will print out the numbers from 0 to 9:

for i in range(10):
     print(i)

For Loop & Lists

A for loop can be used to cycle through each item in a list. This could be extremely useful in many situations:

num_list = [1,3,5,7,9,11]
total=0
for num in num_list:
     total = total + num # could use total += num instead
print("total is "+total)

Now Try

1) Create a program to print out the 12 times table (using a for loop)

2) Create a program to draw the number of stars entered by the user (these should be on a single row)

3) Adapt the line of stars program above to print the stars the number of times the user entered (ie 2 lines of 2 stars)

4) Create a program to draw a triangle of stars

While

while is usually used to perform some repetitive tasks that may be executed one or more times based on some condition. The key to a while loop is that we probably don't know how many times it will need to repeat, or if it will need to run at all. The code below shows a sample while loop in Python:

i=0
while i<10:
     i=i+1 # could use i++ instead
     print("i is "+i)

Break

Break can be used to stop the execution for a loop, for example:

i=0
while i<10:
     i=i+1 # could use i++ instead
     if i>5:
          break
     print("i is "+i)

In this program i will start at 0 and iterate until i becomes 6, and the loop will stop. This means i will never be 7,8,9 or 10.

Continue

Break will stop the iteration, continue can be used to skip the rest of the current iteration. For example the code below will print out the numbers from 1 to 10, but will skip the number 5:

i=0
while i<10:
     i=i+1 # could use i++ instead
     if i=5:
          continue
     print("i is "+i)

Repeat

Python doesn't really have a repeat loop, however we can create one using a while:

while true:
     print("Password System")
     password = input("Please enter the password")
     if password == "itsjustme":
          break

Having 'while true:' means this will ALWAYS run, then internally we need an if statement to check the condition for stopping. Alternatively we could use a boolean for this instead:

loopcontrol = true
while loopcontrol:
     print("Password System")
     password = input("Please enter the password")
     if password == "itsjustme":
          loopcontrol = false

Now Try

1) Create a program to ask a user to enter a number between 10 & 50

  • The program should use a while loop so that the user will be asked again if an incorrect value was entered
  • The should inform the user that what was entered is out of the range specified
  • The number enter should be displayed if it is in the range specified

2) The sequence 1,4,9,16,25 is made up of square numbers (i.e.1=1,2= 4, 3=9 etc.). Write a program that writes out this all the square numbers under 5000.

3)The factorial of a number is written using “!”. So the factorial of 5 is written 5!. By factorial we mean what do we get when we multiply all the numbers up to and including that number. So 5! = 5 x 4 x 3 x 2 x 1 and 7! = 7 x 6 x 5 x 4 x 3 x 2 x 1. Write a program to calculate the factorial of the number entered.