wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python for loop/lists

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

What does a for loop do in Python?

a)

Repeats a block of code while a condition is true

b)

Chooses a random element from a list

c)

Repeats a block of code for each item in a sequence

d)

Declares a function

2.

What is the output of this code? python CopyEdit fruits = ['apple', 'banana', 'cherry'] for fruit in fruits: print(fruit)

a)

apple banana cherry

b)

['apple', 'banana', 'cherry']

c)

fruit fruit fruit

d)

Error

3.

Which of the following is a valid list?

a)

[1, 2, 3, 4]

b)

(1, 2, 3, 4)

c)

{1, 2, 3, 4}

d)

"1, 2, 3, 4"

4.

How many times will this loop run? for i in range(5): print(i)

a)

4

b)

5

c)

6

d)

Infinite

5.

What is the index of 'dog' in this list? animals = ['cat', 'dog', 'lion']

a)

0

b)

1

c)

2

d)

3

6.

What does this print? python CopyEdit numbers = [10, 20, 30] print(numbers[1])

a)

10

b)

20

c)

30

d)

Error

7.

What is the purpose of range() in a for loop?

a)

To shuffle the list

b)

To create a loop that counts

c)

To sort items

d)

To reverse a list

8.

What will be printed? for i in range(3): print("Hello")

a)

Hello

b)

Hello Hello Hello

c)

Error

d)

Nothing

9.

Which statement adds 'elephant' to this list? animals = ['cat', 'dog']

a)

animals.add('elephant')

b)

animals.push('elephant')

c)

animals.append('elephant')

d)

animals.insert('elephant')

10.

Which of the following is used to loop through indexes and values?

a)

for index in list:

b)

for i in range(list):

c)

for i in range(len(list)):

d)

for item, i in list:

11.

What is the output? numbers = [1, 2, 3] for num in numbers: print(num * 2)

a)

1 2 3

b)

2 4 6

c)

1 4 9

d)

3 2 1

12.

Which of these is the correct way to loop through a list backwards? nums = [1, 2, 3]

a)

for i in nums[::-1]:

b)

for i in reversed(nums):

c)

for i in nums.reverse():

d)

Both A and B

13.

What will this print? letters = ['a', 'b', 'c'] letters[0] = 'z' print(letters)

a)

['a', 'b', 'c']

b)

['z', 'b', 'c']

c)

['a', 'b', 'z']

d)

Error

14.

How do you check if 'cat' is in the list? animals = ['cat', 'dog', 'lion']

a)

'cat' in animals

b)

animals.has('cat')

c)

animals.find('cat')

d)

'cat' == animals

15.

Which of the following removes 'banana' from a list? fruits = ['apple', 'banana', 'cherry']

a)

fruits.remove('banana')

b)

fruits.delete('banana')

c)

fruits.pop('banana')

d)

del fruits('banana')