wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Mastering Python Loops

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

What is the syntax for a basic for loop in Python?

a)

for variable in sequence:

b)

for variable in (sequence)

c)

for (variable : sequence)

d)

foreach variable in sequence:

2.

How does a while loop differ from a for loop?

a)

A for loop can only iterate over arrays, while a while loop can iterate over any data type.

b)

A while loop checks a condition before each iteration, while a for loop is used for a known number of iterations with initialization, condition, and increment in one line.

c)

A while loop is always faster than a for loop.

d)

A while loop requires a counter variable, while a for loop does not.

3.

What will be the output of the following code: for i in range(3): print(i)?

a)

3

b)

0 1 2

c)

-1

d)

1.5

4.

Explain the purpose of the break statement in loops.

a)

The break statement is used to skip the current loop iteration.

b)

The break statement continues to the next iteration of the loop.

c)

The break statement pauses the loop for a specified time.

d)

The purpose of the break statement in loops is to exit the loop prematurely.

5.

What is the output of the following code: while True: print('Hello')?

a)

The output is 'Hello' printed once.

b)

The output is an error message.

c)

The output is 'Hello' printed indefinitely.

d)

The output is 'Hello' printed with a delay.

6.

How can you loop through a list in Python?

a)

Use a while loop: while item in list: print(item)

b)

Use a for loop: for item in list: print(item)

c)

Iterate using list comprehension: [print(item) for item in list]

d)

Access elements by index: for i in range(len(list)): print(list[i])

7.

What is a nested loop and when would you use it?

a)

A nested loop is used to create single-dimensional arrays.

b)

Nested loops are only applicable in object-oriented programming.

c)

You should avoid using nested loops as they are inefficient for all tasks.

d)

A nested loop is used when you need to iterate over multi-dimensional data or perform operations that involve combinations of multiple sets.

8.

What does the continue statement do in a loop?

a)

The continue statement pauses the loop for a specified time.

b)

The continue statement ends the loop immediately.

c)

The continue statement skips the current iteration of a loop.

d)

The continue statement restarts the loop from the beginning.

9.

How can you iterate over a dictionary in Python?

a)

for dict in keys():

b)

for key in dict.keys()

c)

Use 'for key in dict:', 'for value in dict.values()', or 'for key, value in dict.items()'.

d)

for item in dict:

10.

What is the result of using a for loop with the zip function?

a)

The result is a single list of all elements combined.

b)

The result is an error due to incompatible input types.

c)

The result is an iterable of tuples, where each tuple contains elements from the input iterables.

d)

The result is a dictionary mapping the first iterable to the second.