Fatskills
Practice. Master. Repeat.
Study Guide: UK K12 GCSE A-Level Year 6 KS2 Computer Science Python Loops Functions Lists
Source: https://www.fatskills.com/dairy/chapter/uk-k12-gcse-a-level-year-6-ks2-computer-science-python-loops-functions-lists

UK K12 GCSE A-Level Year 6 KS2 Computer Science Python Loops Functions Lists

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~5 min read

Learning Objectives

By the end of this topic, students will be able to:


  • Understand the concept of loops in Python and be able to write simple loop structures using for and while loops.
  • Define and use functions in Python to organize and reuse code.
  • Create and manipulate lists in Python, including indexing, slicing, and modifying list elements.
  • Apply loops and functions to solve real-world problems and demonstrate an understanding of the concepts.
  • Evaluate the effectiveness of loops and functions in simplifying code and improving efficiency.

Core Concepts


Loops

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.

Functions

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.

Lists

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().

Worked Examples


Example 1: Looping through a list

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

Example 2: Using a function to calculate the area of a rectangle

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.

Example 3: Modifying a list

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.

Common Misconceptions

  • A loop is a way to repeat a set of instructions, but it can also be used to iterate over a list or other collection.
  • A function is a block of code that can be called multiple times, but it can also be used to organize and reuse code.
  • A list is a collection of items that can be accessed and modified, but it can also be used to store and manipulate data.

Exam Tips

  • Make sure to understand the concept of loops and how they can be used to repeat a set of instructions.
  • Practice using functions to organize and reuse code.
  • Be able to create and manipulate lists in Python, including indexing, slicing, and modifying list elements.
  • Apply loops and functions to solve real-world problems and demonstrate an understanding of the concepts.

MCQs


MCQ 1: [F]

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.

MCQ 2: [H]

What is the output of the following code?


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.

MCQ 3: [F]

What is the output of the following code?


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.

MCQ 4: [H]

What is the output of the following code?


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.

MCQ 5: [F]

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.

Short-answer questions

  1. What is the difference between a for loop and a while loop in Python?
  2. How do you use the append() method to add an item to the end of a list in Python?
  3. What is the purpose of a function in Python, and how do you define one?
  4. How do you use a for loop to iterate over a list in Python?
  5. What is the difference between indexing and slicing a list in Python?


ADVERTISEMENT