NEW
Font size
WorksheetsPython Programming Quiz
Total questions: 20
Worksheet time: 10mins
What will this code output? numbers = [1, 2, 3] for n in numbers: print(n + n)
1 2 3
2 4 6
1 4 9
Error
Which of these correctly loops through a list called data?
loop data:
for data in i:
for item in data:
data for item:
What is printed here? fruits = ["apple", "banana", "cherry"] print(fruits[-2])
apple
banana
cherry
Error
How many times will this print "Hi"? for i in range(1, 5, 2): print("Hi")
1
2
3
4
What is the output of this code? nums = [1, 3, 5] for n in nums: print(n * 2)
2 4 6
1 3 5
2 6 10
1 6 25
What does this function return? def add(x, y=2): return x + y print(add(3))
Error
5
6
"x + y"
What is the final value of total? numbers = [2, 4, 6] total = 1 for n in numbers: total *= n
12
48
0
1
Which of these is a valid way to reverse the list data?
data = data.reverse()
data[::-1]
reverse(data)
data = data[::-1]()
What will this code print? nums = [5, 10, 15] for n in nums: if n % 10 == 0: print(n)
5 10 15
10 15
10
5 15
What does this loop print? names = ["Tom", "Sue", "Max"] for i in range(len(names)): print(names[i].lower())
Tom Sue Max
tom sue max
T S M
Error
How many times will this loop run? for i in range(1, 9, 3): print(i)
2
3
4
5
What is the output? items = ["a", "b", "c"] result = "" for item in items: result += item.upper() print(result)
A B C
abc
ABC
['A', 'B', 'C']
How can you remove duplicates from mylist and keep the order?
mylist = list(set(mylist))
mylist = sorted(set(mylist))
Use a loop with a new list and not in
mylist.remove_duplicates()
What will be the output? nums = [1, 2, 3, 4] even = [n for n in nums if n % 2 == 0] print(even)
[1, 3]
[2, 4]
[1, 2, 3, 4]
[]
What happens here? names = ["Ann", "Bob", "Cat"] for i in range(len(names)): names[i] = names[i].upper() print(names)
['ANN', 'BOB', 'CAT']
['Ann', 'Bob', 'Cat']
['A', 'B', 'C']
Error
What will this code print? fruits = ["apple", "banana", "cherry"] print(fruits[len(fruits) - 1])
apple
banana
cherry
Error
What does this code output? for i in range(2, 6): print(i, end=", ")
2, 3, 4, 5,
2 3 4 5
2, 3, 4, 5
Error
What is the length of this list? colors = ["red", "green", ["blue", "cyan"], "yellow"] print(len(colors))
3
4
5
Error
What does this range generate? print(list(range(10, 4, -2)))
[10, 8, 6, 4]
[10, 8, 6]
[4, 6, 8, 10]
[10, 9, 8, 7, 6, 5]
What is printed here? lst = ["a", "b", "c"] for i in range(len(lst)): lst[i] = lst[i] + str(i) print(lst)
['a', 'b', 'c']
['a0', 'b1', 'c2']
['a', 'b', 'c2']
Error
