Lecture 3 Flashcards
1
Q
What are the ways of terminating a for
loop?
A
1
Q
What is the output from the following code?
~~~
x = 4
for j in range(x):
print(“Iteration of outer loop”)
for i in range(x):
print(“-> Iteration of inner loop”)
x = 2
~~~
A
Iteration of outer loop -> Iteration of inner loop -> Iteration of inner loop -> Iteration of inner loop -> Iteration of inner loop Iteration of outer loop -> Iteration of inner loop -> Iteration of inner loop Iteration of outer loop -> Iteration of inner loop -> Iteration of inner loop Iteration of outer loop -> Iteration of inner loop -> Iteration of inner loop
The number of iterations of for loop is evaluated once, when Python first encounters the for
statement.
Neither the number of iterations nor the range of elements is re-evaluated during looping.