You are on page 1of 66

Information Technology Concepts

Lists and Loops

School of Information Technology and Mathematical Sciences


The six basic components to programming:
Output
Variables
Input
Maths
Conditionals
Iteration
Output
Variables
Input
Maths
Conditionals
Iteration
To "iterate" is to perform a task repeatedly.
In programming, it allows you to repeat the
same instructions multiple times.
In programming, it allows you to repeat the
same instructions multiple times.
Repeat:
Print "Hello World"

Repeating a task
However, you don't want to repeat a task forever.
This would create an infinite loop.
Repeat: Hello World
Print "Hello World"Hello World
Hello World Hello World
Hello World Hello World
Hello World
Hello World
Hello World Hello World
Hello World Hello World
Hello World Hello World
Hello World
Hello WorldHello World Hello World
Hello World Hello World
Hello World Hello World
Hello World Hello World
Hello World Hello World
Hello World
Hello World

Repeating a task infinitely


Thus you need a way of ending the loop.
Two basic forms:
1. Perform a task while something is true.
2. Perform a task a certain number of times.
While loops continue while a statement is true.
x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

A while loop.
x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

First iteration: x = 0
x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

First iteration: x is less than 5, so it starts the loop.


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World

First iteration: prints "Hello World"


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World

First iteration: x is now 1


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World

Second iteration: x is still less than 5 ...


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World
Hello World

Second iteration: prints "Hello World"


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World
Hello World

Second iteration: x is now equal to 2


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World
Hello World
Hello World

Third iteration: At the end, x is increased to 3.


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World
Hello World
Hello World
Hello World

Fourth iteration: At the end, x is increased to 4.


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World
Hello World
Hello World
Hello World
Hello World

Fifth iteration: At the end, x is increased to 5.


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World
Hello World
Hello World
Hello World
Hello World

Fifth iteration: x is not less than 5.


x = 0
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Hello World
Hello World
Hello World
Hello World
Hello World
Done

Fifth iteration: Therefore the iteration is over, and it prints "Done"


x = 5
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Note that if the condition is never met (x is never less than 5) ...
x = 5
while (x < 5):
print('Hello World')
x = x + 1
print('Done')

Done

... it will never run the instructions in the loop.


x = 5
while (x > 0):
print('Hello World')
x = x + 1
print('Done')

But beware of loops that can never end!


The condition in a loop can be many things.
"Keep iterating until the game is over"
"Keep iterating until the user enters quit"
"Keep iterating until the end of the file is reached"
"Keep iterating until all of the data is complete"
For loops continue a certain number of times.
You provide a set of values, and the computer
will look at each one, ending when there
are none left.
For example, range(0,5) contains the values
0, 1, 2, 3 and 4.
So: for x in range(0,5): says
"continue looping, making x equal the numbers 0 – 4,
exactly 5 times".
for x in range(0,5):
print(x, 'Hello World')
print('Done')

0 Hello World
1 Hello World
2 Hello World
3 Hello World
4 Hello World
Done

The "for" loop counts from 0 to 4


For loops are safer than while loops,
as they are almost guaranteed to end.
Lists
In last week's workshop, we could create multiple names.
name0 = "Scott Stevens"

print(name0)

Creating one student.


name0 = "Scott Stevens"
name1 = "Corey Martin"

print(name0)
print(name1)

Creating two students.


name0 = "Scott Stevens"
name1 = "Corey Martin"
name2 = "Ben Martini"
name3 = "Gaye Deegan"
name4 = "Grant Wigley"

print(name0)
print(name1)
print(name2)
print(name3)
print(name4)

Creating five students.


What we need is a way of using iteration.
To do that, we store the values in a list.
animal0 = "Dog"
animal1 = "Tadpole"
animal2 = "Axolotl"
animal3 = "Turtle"
animal4 = "Chicken"

Old approach to storing values.


animals =
["Dog","Tadpole","Axolotl","Turtle","Chicken"]

Using a list.
Each value is stored in one variable.
They are each given a number to tell them apart.
animals =
["Dog","Tadpole","Axolotl","Turtle","Chicken"]

print(animals[0])

Dog

The first element is number 0.


animals =
["Dog","Tadpole","Axolotl","Turtle","Chicken"]

print(animals[1])

Tadpole

The second is number 1.


animals =
["Dog","Tadpole","Axolotl","Turtle","Chicken"]

print(animals[4])

Chicken

The last is number 4.


animals =
["Dog","Tadpole","Axolotl","Turtle","Chicken"]

for animal in animals:


print(animal)

Dog
Tadpole
Axolotl
Turtle
Chicken

For loop to print each item in the list


You can change a value by referencing it by number ...
animals =
["Dog","Tadpole","Axolotl","Turtle","Chicken"]

animals[1] = "Frog"

for animal in animals:


print(animal)

Dog
Frog
Axolotl
Turtle
Chicken

Reference the item to change it.


... add a new item to the end ...
animals =
["Dog","Tadpole","Axolotl","Turtle","Chicken"]

animals.append("Stick insect")

for animal in animals:


print(animal)

Dog
Tadpole
Axolotl
Turtle
Chicken
Stick insect

Appending an item to the end


... insert an item into a position ...
animals =
["Dog","Tadpole","Axolotl","Turtle","Chicken"]

animals.insert(2, "Snake")

for animal in animals:


print(animal)

Dog
Tadpole
Snake
Axolotl
Turtle
Chicken

Appending an item to the end


... and delete an item.
animals =
["Dog","Tadpole","Axolotl","Turtle","Chicken"]

del animals[2]

for animal in animals:


print(animal)

Dog
Tadpole
Turtle
Chicken

Appending an item to the end

You might also like