You are on page 1of 39

COMP1117A Tutorial 4

LOOPS / ITERATIONS
About this tutorial

▶ Introduction to loops/iterations.
▶ Some multiple choices questions to check your understanding.
▶ Hints to tutorial exercises.
While Loops
For loops

A for loop is used for iterating over a sequence


(that is either a list, a tuple, a dictionary, a set or
a string).
Example
fruits = ["apple", "banana", "cherry"]

for x in fruits:
print(x)

• Key word for


• A sequence for iteration
• Loop body
The range() function

▶ range(start, stop, step)


▶ generates a series of numbers within a given range
▶ start: integer starting from which the sequence of integers is to be
returned
▶ stop: integer before which the sequence of integers is to be
returned, the stop integer is not included.
▶ step: integer value which determines the increment between
each integer in the sequence
Using the range() function (1)

▶ With 1 argument: range(stop)


▶ By default, start =0 and step =1
▶ E.g. range(5) is equivalent to range(0, 5, 1)

What about
range(0)? range(-1)?

Returns an empty sequence


if stop is negative or 0.

!The range of integers end at stop-1. !


Q1

▶ What is the output of the following program?

A. Error:invalid syntax
B. 01234
C. 012345
D. 12345
Q1 Answer

▶ What is the output of the following program?

A. Error:invalid syntax
B. 01234
C. 012345
D. 12345
Q2

▶ What is the output of the following program?

A. abcd
B. abc d
C. abcdabcdabcdabcd
D. Error
Q2 Answer

▶ What is the output of the following program?

To iterate every character in a


string:

A. abcd
B. abc d
C. abcdabcdabcdabcd
which prints a b c d
D. Error

NameError:name ‘j' is not defined


Q3

▶ What is the output of the following program?

A. No output
B. j j j j ... (never stop)
C. abc d
D. abcd
Q3 Answer

▶ What is the output of the following program?

‘j’ is not in ‘abcd’.


A. No output That is j in x is False.
So nothing is printed.
B. j j j j ... (never stop)
C. abc d
D. abcd
Q4

▶ What is the output of the following program?

A. No output
B. j j j j ... (never stop)
C. a a a a … (never stop)
D. a
Q4 Answer

▶ What is the output of the following program?

‘a’ is in ‘abcd’.
That is j in x is True.
A. No output
B. j j j j ... (never stop) As the value of j or x isn’t
changing, the condition will
C. a a a a … (never stop)
always evaluate to True,
D. a that is the loop never ends.
Q5

▶ What is the output of the following program?

A. 1
B. 2 4 6 8 10
C. 123456
D. 1 3 5 7 9 11
Q5 Answer

▶ What isthe output of the following program?

i =1, i%2==0 ➔ False


print 1
i < 12 ➔ Ture
i =3print 1
A. 1
i=3
B. 2 4 6 8 10 …
C. 123456
D. 1 3 5 7 9 11
Using the range() function (2)

▶ With 2 arguments: range(start, stop)


▶ By default, step =1
▶ E.g. range(2, 5) is equivalent to range(2, 5, 1)
Using the range() function (3)

▶ With 3 arguments: range(start, stop, step)


▶ E.g. range(1, 10, 2)

Value constraint:
r[n] =start +step*n,
where, n >=0 and
r[n] <stop (for positive step)
r[n] >stop (for negative step)
Using the range() function (3)

▶ With 3 arguments: range(start, stop, step)


▶ E.g. range(10, 5, -2)

Value constraint:
r[n] =start +step*n,
where, n >=0 and
r[n] <stop (for positive step)
r[n] >stop (for negative step)
Using the range() function (3)

▶ With 3 arguments: range(start, stop, step)


▶ What about step =0? E.g. range(1, 5, 0)

ValueError:range() arg 3 must not be zero

Value constraint:
r[n] =start +step*n,
where, n >=0 and
r[n] <stop (for positive step)
r[n] >stop (for negative step)
Q6

▶ What is the output of the following program?

A. Error:invalid syntax
B. Nothing will be printed
C. 54321
D. 0 -1 -2 -3 ...(never stop)
Q6 Answer

▶ What is the output of the following program?

A. Error:invalid syntax
B. Nothing will be printed
C. 54321
D. 0 -1 -2 -3 ...(never stop)
Q6 Answer

▶ What is the output of the following program?

r[n] =0 +(-1)*n,
A. Error:invalid syntax where, n >=0 and
B. Nothing will be printed r[n] > 5 (for negative step)
Constraint not met, empty
C. 54321 sequence is returned.
D. 0 -1 -2 -3 ...(never stop) Value constraint:
r[n] =start +step*n,
where, n >=0 and
r[n] <stop (for positive step)
r[n] >stop (for negative step)
Q7

▶ What is the output of the following program?

A. 0 1 2 3 4 ... (never stop)


B. 0123
C. 01
D. 34
Q7 Answer

▶ What is the output of the following program?

range(x) is only evaluated for


ONE time when the program
enters the for loop!

A. 0 1 2 3 4 ... (never stop) So, range(2) returns [0, 1].


That is the for loop becomes:
B. 0123
C. 01
D. 34
when i is 0, x ➔ 3, which prints 3;
when i is 1, x ➔ 4, which prints 4
Q8

▶ What is the output of the following program?


for i in range(1,4):
print(i, end = ' ')
i=i-1

A. 123
B. 1 0 -1
C. 1 0 -1 ... (never stop)
D. Nothing is printed
Q8 Answer

▶ What is the output of the following program?


for i in range(1,4):
print(i, end = ' ') range(1, 4) returns [1, 2, 3]
i=i-1
That is the for loop becomes:
A. 123
for i in [1,2,3]:
B. 1 0 -1 print(i, end = ' ')
C. 1 0 -1 ... (never stop) i=i-1

D. Nothing is printed
Q9

▶ What is the output of the following program?

A. abcd
B. abc d
C. abcdabcdabcdabcd
D. Error
Q9 Answer

▶ What is the output of the following program?

x is a string, so range(x) gives


error!

To iterate every character in a


A. abcd string:
B. abc d
C. abcdabcdabcdabcd
D. Error
which prints a b c d

TypeError: 'str' object cannot be interpreted as an integer


Tutorial 4 Worksheet
Tutorial Exercises 4.1

▶ For example:

▶ The above program will output:


Tutorial Exercises 4.2

▶ For example:

▶ The above program will output:


True
Tutorial Exercise 4.3
Tutorial Exercise 4.3
“Out of memory”

storage one value

storage 10000 values


Tutorial Exercise 4.4
Tutorial Exercise 4.4

▶ To check whether 6 is a perfect number:


▶ Set aSum =0
▶ Go through each of the number i in [1, 2, 3, 4, 5]
▶ If 6 is divisible by i (remainder is 0), add i to aSum
▶ 6 is divisible by 1, aSum =1
▶ 6 is divisible by 2, aSum =1+2 =3
▶ 6 is divisible by 3, aSum =3+3 =6
▶ 6 is not divisible by 4, nothing done
▶ 6 is not divisible by 5, nothing done
▶ After going through all the numbers, as aSum is 6, then output
‘Yes’.
Tutorial Exercise 4.4

???
Q&A

▶ Please feel free to post on Moodle Forum if you have


questions.

▶ But don’t copy and paste your own code to the Moodle
forum.

▶ You may also contact your Student TA for help.

You might also like