wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Mastering Python Loops, Functions, and Lists

Total questions: 8

Worksheet time: 4mins

Name
Class
Date
1.

Which of the following is the correct syntax for a for loop that iterates over a list named `numbers` in Python?

a)

`for numbers in i:`

b)

`for i in numbers:`

c)

`for i to numbers:`

d)

`foreach i in numbers:`

2.

What is the correct way to define a function in Python that takes no arguments and prints "Hello"?

a)

`function hello(): print("Hello")`

b)

`def hello(): print("Hello")`

c)

`def hello: print("Hello")`

d)

`function hello: print("Hello")`

3.

Which of the following is a valid way to declare a single-dimensional array (list) of integers in Python?

a)

`arr = [1, 2, 3, 4]`

b)

`arr = (1, 2, 3, 4)`

c)

`arr = {1, 2, 3, 4}`

d)

`arr = <1, 2, 3, 4>`

4.

Which of the following statements correctly creates a 2D array (list of lists) in Python?

a)

`matrix = [[1, 2], [3, 4]]`

b)

`matrix = ((1, 2), (3, 4))`

c)

`matrix = {1, 2, 3, 4}`

d)

`matrix = [1, 2, 3, 4]`

5.

What is the output of the following code? ```python def add(a, b): return a + b print(add(2, 3)) ```

a)

`2`

b)

`3`

c)

`5`

d)

`23`

6.

Which of the following loop constructs will correctly print numbers from 10 down to 1?

a)

for i in range(10,0,-1):

print(i)

b)

for in range(1,10):

print(i)

c)

for in range(10):

print(i)

d)

for i in range(10,1-1):

print(i)

7.

Given the list `arr = [10, 20, 30, 40]`, which statement will change the second element to 25?

a)

`arr[1] = 25`

b)

`arr[2] = 25`

c)

`arr[0] = 25`

d)

`arr[3] = 25`

8.

Given a 2D array `matrix = [[1, 2], [3, 4]]`, how would you access the element `4`?

a)

`matrix[1][1]`

b)

`matrix[0][1]`

c)

`matrix[1][0]`

d)

`matrix[0][0]`