WorksheetsPython Data Structures and loops
Total questions: 20
Worksheet time: 10mins
What method would you use to add an element to a list?
Use the 'append()' method.
Use the 'extend()' method.
Use the 'add()' method.
Use the 'insert()' method.
Which method would you use to retrieve a value from a dictionary using a key?
Use parentheses to access the value
Use square bracket notation or the get() method.
Use the index method to retrieve the value
Call the dictionary with the key as an argument
What is the output of the following code: for i in range(5): print(i)?
10
5
0 1 2 3 4
-1
How do you check if a variable is equal to a specific value using if-else?
if (variable == value) { // code for true } else { // code for false }
if (variable = value) { // code for true } else { // code for false }
if (value == variable) { // code for true } else { // code for false }
if (variable != value) { // code for true } else { // code for false }
What is a nested list and how can you access its elements?
A nested list is a flat structure; elements can be accessed in any order.
A nested list is a type of dictionary; access elements using keys.
A nested list is a single list with no sublists; access elements using a single index.
A nested list is a list containing other lists; access elements using multiple indices.
How can you remove an item from a list by its value?
Use list.pop(index) to remove an item by its value.
Use list.remove(value) to remove an item by its value.
Use del list[index] to remove an item by its value.
Use list.clear() to remove all items from the list.
What is the syntax for creating a tuple in Python?
A tuple is created using square brackets, e.g., [1, 2, 3].
A tuple is created using angle brackets, e.g., <1, 2, 3>.
A tuple is created using parentheses, e.g., (1, 2, 3).
A tuple is created using curly braces, e.g., {1, 2, 3}.
How do you iterate over the keys of a dictionary using a for loop?
for key in my_dict.items():
for key in my_dict.values():
for key in my_dict.keys():
for key in range(len(my_dict)):
What will be the output of the following code: if x > 10: print('High') else: print('Low')?
Always 'Low'
Always 'High'
Prints 'Error' if x is not a number
Depends on the value of x
How can you check if a key exists in a dictionary?
Use 'dictionary.contains(key)'
Use 'key in dictionary' or 'dictionary.get(key)'.
Use 'dictionary.has_key(key)'
Check 'key in list'
What is the difference between a list and a tuple in Python?
Lists can contain only numbers; tuples can contain any data type.
Tuples are mutable; lists are immutable.
Lists are faster than tuples in all cases.
Lists are mutable; tuples are immutable.
How do you access the first element of a nested dictionary?
nested_dict['inner_key']['outer_key']
nested_dict.outer_key.inner_key
nested_dict['outer_key'].inner_key
nested_dict['outer_key']['inner_key']
What method can you use to get all the values from a dictionary?
dictionary.values()
dictionary.allValues()
dictionary.getAll()
dictionary.items()
How can you use a for loop to iterate through a list of tuples?
Use 'for item in list_of_tuples.items():' to iterate through the list.
Use 'for i in range(len(list_of_tuples)):' to iterate through the list.
Use 'for tuple in list_of_tuples:' to iterate through the list of tuples.
Use 'for tuple in list_of_tuples.values():' to iterate through the list.
How can you create a dictionary from two lists?
Use dict(zip(list1, list2))
Use list(zip(list1, list2))
Use dict(list1, list2)
Use zip(list1, list2)
How do you check if a list is empty in Python?
if not list:
if list == []:
if len(list) == 0:
All of the above
What is the correct way to create a set in Python?
set = [1, 2, 3]
set = (1, 2, 3)
set = {1, 2, 3}
set = <1, 2, 3>
count = 0 while count < 3: print("Hello!") count += 1
Hello! (printed 1 time)
Hello! (printed 2 times)
Hello! (printed 3 times)
Infinite Hello! (keeps printing without stopping)
x = 5 while x > 0: print("Counting down!")
True
False
Complete the code so that it only prints numbers from 1 to 5.
number = 1
while _______:
print(number)
number += 1
number <= 5
number >= 5
number != 5
number == 5
