NEW
Font size
WorksheetsMastering Python Loops, Functions, and Lists
Total questions: 8
Worksheet time: 4mins
Which of the following is the correct syntax for a for loop that iterates over a list named `numbers` in Python?
`for numbers in i:`
`for i in numbers:`
`for i to numbers:`
`foreach i in numbers:`
What is the correct way to define a function in Python that takes no arguments and prints "Hello"?
`function hello(): print("Hello")`
`def hello(): print("Hello")`
`def hello: print("Hello")`
`function hello: print("Hello")`
Which of the following is a valid way to declare a single-dimensional array (list) of integers in Python?
`arr = [1, 2, 3, 4]`
`arr = (1, 2, 3, 4)`
`arr = {1, 2, 3, 4}`
`arr = <1, 2, 3, 4>`
Which of the following statements correctly creates a 2D array (list of lists) in Python?
`matrix = [[1, 2], [3, 4]]`
`matrix = ((1, 2), (3, 4))`
`matrix = {1, 2, 3, 4}`
`matrix = [1, 2, 3, 4]`
What is the output of the following code? ```python def add(a, b): return a + b print(add(2, 3)) ```
`2`
`3`
`5`
`23`
Which of the following loop constructs will correctly print numbers from 10 down to 1?
for i in range(10,0,-1):
print(i)
for in range(1,10):
print(i)
for in range(10):
print(i)
for i in range(10,1-1):
print(i)
Given the list `arr = [10, 20, 30, 40]`, which statement will change the second element to 25?
`arr[1] = 25`
`arr[2] = 25`
`arr[0] = 25`
`arr[3] = 25`
Given a 2D array `matrix = [[1, 2], [3, 4]]`, how would you access the element `4`?
`matrix[1][1]`
`matrix[0][1]`
`matrix[1][0]`
`matrix[0][0]`
