wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Programming Quiz

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What will this code output? numbers = [1, 2, 3] for n in numbers: print(n + n)

a)

1 2 3

b)

2 4 6

c)

1 4 9

d)

Error

2.

Which of these correctly loops through a list called data?

a)

loop data:

b)

for data in i:

c)

for item in data:

d)

data for item:

3.

What is printed here? fruits = ["apple", "banana", "cherry"] print(fruits[-2])

a)

apple

b)

banana

c)

cherry

d)

Error

4.

How many times will this print "Hi"? for i in range(1, 5, 2): print("Hi")

a)

1

b)

2

c)

3

d)

4

5.

What is the output of this code? nums = [1, 3, 5] for n in nums: print(n * 2)

a)

2 4 6

b)

1 3 5

c)

2 6 10

d)

1 6 25

6.

What does this function return? def add(x, y=2): return x + y print(add(3))

a)

Error

b)

5

c)

6

d)

"x + y"

7.

What is the final value of total? numbers = [2, 4, 6] total = 1 for n in numbers: total *= n

a)

12

b)

48

c)

0

d)

1

8.

Which of these is a valid way to reverse the list data?

a)

data = data.reverse()

b)

data[::-1]

c)

reverse(data)

d)

data = data[::-1]()

9.

What will this code print? nums = [5, 10, 15] for n in nums: if n % 10 == 0: print(n)

a)

5 10 15

b)

10 15

c)

10

d)

5 15

10.

What does this loop print? names = ["Tom", "Sue", "Max"] for i in range(len(names)): print(names[i].lower())

a)

Tom Sue Max

b)

tom sue max

c)

T S M

d)

Error

11.

How many times will this loop run? for i in range(1, 9, 3): print(i)

a)

2

b)

3

c)

4

d)

5

12.

What is the output? items = ["a", "b", "c"] result = "" for item in items: result += item.upper() print(result)

a)

A B C

b)

abc

c)

ABC

d)

['A', 'B', 'C']

13.

How can you remove duplicates from mylist and keep the order?

a)

mylist = list(set(mylist))

b)

mylist = sorted(set(mylist))

c)

Use a loop with a new list and not in

d)

mylist.remove_duplicates()

14.

What will be the output? nums = [1, 2, 3, 4] even = [n for n in nums if n % 2 == 0] print(even)

a)

[1, 3]

b)

[2, 4]

c)

[1, 2, 3, 4]

d)

[]

15.

What happens here? names = ["Ann", "Bob", "Cat"] for i in range(len(names)): names[i] = names[i].upper() print(names)

a)

['ANN', 'BOB', 'CAT']

b)

['Ann', 'Bob', 'Cat']

c)

['A', 'B', 'C']

d)

Error

16.

What will this code print? fruits = ["apple", "banana", "cherry"] print(fruits[len(fruits) - 1])

a)

apple

b)

banana

c)

cherry

d)

Error

17.

What does this code output? for i in range(2, 6): print(i, end=", ")

a)

2, 3, 4, 5,

b)

2 3 4 5

c)

2, 3, 4, 5

d)

Error

18.

What is the length of this list? colors = ["red", "green", ["blue", "cyan"], "yellow"] print(len(colors))

a)

3

b)

4

c)

5

d)

Error

19.

What does this range generate? print(list(range(10, 4, -2)))

a)

[10, 8, 6, 4]

b)

[10, 8, 6]

c)

[4, 6, 8, 10]

d)

[10, 9, 8, 7, 6, 5]

20.

What is printed here? lst = ["a", "b", "c"] for i in range(len(lst)): lst[i] = lst[i] + str(i) print(lst)

a)

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

b)

['a0', 'b1', 'c2']

c)

['a', 'b', 'c2']

d)

Error