NEW
Font size
WorksheetsPython for loop/lists
Total questions: 15
Worksheet time: 8mins
What does a for loop do in Python?
Repeats a block of code while a condition is true
Chooses a random element from a list
Repeats a block of code for each item in a sequence
Declares a function
What is the output of this code? python CopyEdit fruits = ['apple', 'banana', 'cherry'] for fruit in fruits: print(fruit)
apple banana cherry
['apple', 'banana', 'cherry']
fruit fruit fruit
Error
Which of the following is a valid list?
[1, 2, 3, 4]
(1, 2, 3, 4)
{1, 2, 3, 4}
"1, 2, 3, 4"
How many times will this loop run? for i in range(5): print(i)
4
5
6
Infinite
What is the index of 'dog' in this list? animals = ['cat', 'dog', 'lion']
0
1
2
3
What does this print? python CopyEdit numbers = [10, 20, 30] print(numbers[1])
10
20
30
Error
What is the purpose of range() in a for loop?
To shuffle the list
To create a loop that counts
To sort items
To reverse a list
What will be printed? for i in range(3): print("Hello")
Hello
Hello Hello Hello
Error
Nothing
Which statement adds 'elephant' to this list? animals = ['cat', 'dog']
animals.add('elephant')
animals.push('elephant')
animals.append('elephant')
animals.insert('elephant')
Which of the following is used to loop through indexes and values?
for index in list:
for i in range(list):
for i in range(len(list)):
for item, i in list:
What is the output? numbers = [1, 2, 3] for num in numbers: print(num * 2)
1 2 3
2 4 6
1 4 9
3 2 1
Which of these is the correct way to loop through a list backwards? nums = [1, 2, 3]
for i in nums[::-1]:
for i in reversed(nums):
for i in nums.reverse():
Both A and B
What will this print? letters = ['a', 'b', 'c'] letters[0] = 'z' print(letters)
['a', 'b', 'c']
['z', 'b', 'c']
['a', 'b', 'z']
Error
How do you check if 'cat' is in the list? animals = ['cat', 'dog', 'lion']
'cat' in animals
animals.has('cat')
animals.find('cat')
'cat' == animals
Which of the following removes 'banana' from a list? fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana')
fruits.delete('banana')
fruits.pop('banana')
del fruits('banana')
