Professional Documents
Culture Documents
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
for x in fruits:
print(x)
What about
range(0)? range(-1)?
A. Error:invalid syntax
B. 01234
C. 012345
D. 12345
Q1 Answer
A. Error:invalid syntax
B. 01234
C. 012345
D. 12345
Q2
A. abcd
B. abc d
C. abcdabcdabcdabcd
D. Error
Q2 Answer
A. abcd
B. abc d
C. abcdabcdabcdabcd
which prints a b c d
D. Error
A. No output
B. j j j j ... (never stop)
C. abc d
D. abcd
Q3 Answer
A. No output
B. j j j j ... (never stop)
C. a a a a … (never stop)
D. a
Q4 Answer
‘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
A. 1
B. 2 4 6 8 10
C. 123456
D. 1 3 5 7 9 11
Q5 Answer
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)
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)
Value constraint:
r[n] =start +step*n,
where, n >=0 and
r[n] <stop (for positive step)
r[n] >stop (for negative step)
Q6
A. Error:invalid syntax
B. Nothing will be printed
C. 54321
D. 0 -1 -2 -3 ...(never stop)
Q6 Answer
A. Error:invalid syntax
B. Nothing will be printed
C. 54321
D. 0 -1 -2 -3 ...(never stop)
Q6 Answer
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
A. 123
B. 1 0 -1
C. 1 0 -1 ... (never stop)
D. Nothing is printed
Q8 Answer
D. Nothing is printed
Q9
A. abcd
B. abc d
C. abcdabcdabcdabcd
D. Error
Q9 Answer
▶ For example:
▶ For example:
???
Q&A
▶ But don’t copy and paste your own code to the Moodle
forum.