NEW
Font size
WorksheetsMastering Python Looping Statements
Total questions: 15
Worksheet time: 8mins
Hey there, coding wizards! Aashi, Kabir, and Akhil are on a quest to master Python. Can you help them out? What is the syntax for a basic for loop in Python that will help them in their adventures?
for variable in list:
for sequence in variable:
for variable in sequence:
for each variable in sequence:
Hey coding wizards! 🌟 Which of the following is the magical structure of a valid while loop that our friend Dhruv would use in his programming adventures?
while (condition) { // code }
while { // code } (condition)
while (condition) // code {}
while (code) { condition }
Hey there, coding wizards! Ria, Dhruv, and Saisha are having a friendly competition to see who can guess the output of this Python code: for i in range(3): print(i). Can you join in on the fun and figure out what it will print?
4
3
5
0 1 2
Hey there, coding wizards! If Akhil is stuck in a loop while trying to solve a tricky problem in Python, how can he break free?
Remove the loop entirely to avoid execution.
Use the 'break' statement to exit a loop.
Replace the loop condition with 'False' to stop it.
Use the 'continue' statement to skip to the next iteration.
Hey there, coding wizards! 🌟 What do you think is the magical purpose of the continue statement in a loop?
It skips the current iteration of a loop and zooms straight to the next one, just like Ishika when she spots a snack!
It repeats the current iteration of a loop, like Prisha when she can't get enough of her favorite song.
It terminates the loop immediately, much like Kavya when she decides it's time to call it a day!
It pauses the loop for a specified time, just like when Ishika takes a break to stretch!
Hey there, coding wizards! 🌟 How can our friend Sanya loop through a list in Python? Let's help her out!
Use recursion: print_items(my_list) if my_list else None
Use a while loop: while my_list: print(my_list.pop())
Iterate with map: map(print, my_list)
Use a for loop: for item in my_list: print(item)
Hey there, coding wizards! Sneha, Avni, and Krish are having a friendly competition to see who can figure out the output of this list comprehension: [x**2 for x in range(5)]. Can you help them out?
[0, 1, 4, 9, 16]
[1, 2, 3, 4, 5]
[0, 1, 8, 27, 64]
[0, 1, 2, 3, 4]
Hey there, coding wizards! 🌟 Can you help Alisha understand what a nested loop is?
A nested loop is a loop that cannot contain any conditions.
A nested loop is a loop that contains another loop within it.
A nested loop is a loop that executes in parallel.
A nested loop is a loop that runs only once.
Hey there, coding wizards! If Sneha, Aditi, and Kabir are trapped in a loop of endless code, which magical statement should they use to escape immediately?
exit
return
break
continue
Hey there, Python enthusiasts! Imagine Aditi and Kabir are trying to figure out how to iterate over a dictionary in Python for their project. Can you help them out? Which of the following methods should they use?
for item in my_dict: # Iterate over items
for key in my_dict.keys(): # Iterate over keys only
for entry in my_dict.iteritems(): # Iterate over entries
for key in my_dict: # Iterate over keys for value in my_dict.values(): # Iterate over values for key, value in my_dict.items(): # Iterate over key-value pairs
During a coding class, Akhil, Tara, and Aashi were discussing a piece of code. Tara asked, "What do you think this code will print? for i in range(5): if i == 2: break print(i)". Can you help them figure it out?
4
3
1
2
Hey there, coding wizards! Let's help Akhil understand loops better. What is the difference between a for loop and a while loop?
A for loop iterates a known number of times, while a while loop continues until a condition is false.
A for loop runs indefinitely, while a while loop stops after a set number of iterations.
A for loop requires a condition, while a while loop does not need any condition.
A for loop is used for conditional checks, while a while loop is for fixed iterations.
Hey there, coding wizards! Divya, Arjun, and Aanya are on a quest to create a magical list of even numbers from 0 to 10 using the power of list comprehension. Can you help them out?
[0, 2, 4, 6, 8, 10]
[2, 4, 6, 8]
[0, 1, 2, 3, 4, 5]
[1, 3, 5, 7, 9]
Imagine Saisha is coding away, and she accidentally sets up a while loop with a condition that is always true. What do you think will happen next?
The program enters an infinite loop.
The loop runs a fixed number of times.
The program skips the loop entirely.
The program terminates immediately.
Hey there, coding wizards! 🌟 Imagine Avani and Krish are having a friendly coding competition. They wrote a little piece of code together: for i in range(2): for j in range(2): print(i, j). Can you guess what magical output will appear when they run it? 🤔
(0, 0) (0, 1) (1, 0) (1, 1)
(0, 2)
(2, 0)
(1, 2)
