By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
By the end of this topic, students will be able to:
for
while
A loop is a way to repeat a set of instructions over and over. Imagine you have a jar of cookies, and you want to take one out every time you walk by. You can write a loop to do this:
for i in range(5): print("Taking a cookie!")
This loop will print "Taking a cookie!" five times. The range(5) function generates numbers from 0 to 4, and the for loop iterates over these numbers.
range(5)
A function is a block of code that can be called multiple times from different parts of a program. Think of it like a recipe: you can write a function to make a cake, and then call that function whenever you want to make a cake.
def make_cake(): print("Mixing ingredients...") print("Baking the cake...") print("The cake is ready!") make_cake()
This function, make_cake(), prints three lines of text to the console. We can call this function multiple times to make multiple cakes.
make_cake()
A list is a collection of items that can be accessed and modified. Imagine you have a shopping list:
shopping_list = ["milk", "eggs", "bread"] print(shopping_list[0]) # prints "milk" shopping_list.append("cheese") print(shopping_list) # prints ["milk", "eggs", "bread", "cheese"]
This list, shopping_list, contains three items: "milk", "eggs", and "bread". We can access the first item using shopping_list[0], and we can add a new item to the end of the list using append().
shopping_list
shopping_list[0]
append()
Suppose we have a list of numbers, and we want to print each number on a new line. We can use a for loop to do this:
numbers = [1, 2, 3, 4, 5] for number in numbers: print(number)
This code will print:
1 2 3 4 5
Suppose we have a function that calculates the area of a rectangle given its length and width. We can use this function to calculate the area of a rectangle with a length of 5 and a width of 3:
def calculate_area(length, width): return length * width area = calculate_area(5, 3) print(area) # prints 15
This code will print the area of the rectangle, which is 15.
Suppose we have a list of numbers, and we want to double each number. We can use a for loop and the append() method to do this:
numbers = [1, 2, 3, 4, 5] for i in range(len(numbers)): numbers[i] *= 2 print(numbers) # prints [2, 4, 6, 8, 10]
This code will print the list of numbers, where each number has been doubled.
What is the output of the following code?
for i in range(5): print(i)
A) 1, 2, 3, 4, 5 B) 0, 1, 2, 3, 4 C) 5, 4, 3, 2, 1 D) None of the above
Correct answer: B) 0, 1, 2, 3, 4
Why the distractors fail: A) The loop starts from 0, not 1.C) The loop prints the numbers in ascending order, not descending order.D) The code will print the numbers, so this option is incorrect.
def calculate_area(length, width): return length * width area = calculate_area(5, 3) print(area)
A) 10 B) 15 C) 20 D) None of the above
Correct answer: B) 15
Why the distractors fail: A) The calculation is length * width, not length + width.C) The calculation is length * width, not length / width.D) The code will print the correct area, so this option is incorrect.
numbers = [1, 2, 3, 4, 5] for i in range(len(numbers)): numbers[i] *= 2 print(numbers)
A) [2, 4, 6, 8, 10] B) [1, 2, 3, 4, 5] C) [2, 4, 6, 8, 10, 20] D) None of the above
Correct answer: A) [2, 4, 6, 8, 10]
Why the distractors fail: B) The code will double each number, so this option is incorrect.C) The code will double each number, but it will not add 20 to the end of the list.
shopping_list = ["milk", "eggs", "bread"] shopping_list.append("cheese") print(shopping_list)
A) ["milk", "eggs", "bread", "cheese"] B) ["milk", "eggs", "bread"] C) ["cheese", "milk", "eggs", "bread"] D) None of the above
Correct answer: A) ["milk", "eggs", "bread", "cheese"]
Why the distractors fail: B) The code will add "cheese" to the end of the list, so this option is incorrect.C) The code will add "cheese" to the end of the list, but it will not move the existing items.
What is the purpose of a function in Python?
A) To repeat a set of instructions B) To organize and reuse code C) To create a list of items D) None of the above
Correct answer: B) To organize and reuse code
Why the distractors fail: A) A loop is used to repeat a set of instructions, not a function.C) A list is used to store and manipulate data, not a function.D) The code will be reused, so this option is incorrect.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.