Microbit Python
Advanced
60 mins
Teacher/Student led
215 points
What you need:
Chromebook/Laptop/PC
Microbit

Array Essentials

In this step-by-step lesson, you'll explore the essentials of arrays in MicroPython. You'll learn to create, retrieve, change, add, and remove elements from arrays. By the end, you'll apply your knowledge to create engaging LED light patterns on a micro:bit. Get ready to dive into the world of arrays!
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    In this lesson, you'll learn the basics of working with arrays in MicroPython. You'll create a simple list, retrieve elements from the list, change elements, add new elements, and remove elements from the list. By the end of this lesson, you'll have a solid understanding of how to work with arrays in MicroPython.

    To get started, open the micro:bit Python editor at https://python.microbit.org and create a new project.

    2 - What is an Array?

    An array is a collection of items, like numbers or strings, stored in a single variable. In MicroPython, arrays are often called lists. They are useful when you want to store and manipulate multiple values using a single variable.

    In this lesson, we'll use the terms 'array' and 'list' interchangeably.


    3 - Create a List

    Lists can store different types of data, such as integers, strings, or even other lists. For example:

    • Integer List: [1, 2, 3, 4, 5]
    • String List: ['apple', 'banana', 'cherry']
    • Mixed List: [1, 'apple', 3.14]

    Now, let's create a list called 'numbers' with five elements. The elements are the integers from 1 to 5. Add the following code to your editor:

    from microbit import *
    
    numbers = [1, 2, 3, 4, 5]
    
    This creates a list called numbers with five elements. The elements are the integers from 1 to 5.

    4 - Retrieve List Elements

    Now, let's retrieve elements from the list using their index. Add the following new code:

    from microbit import *
    
    numbers = [1, 2, 3, 4, 5]
    
    first_number = numbers[0]
    second_number = numbers[1]
    
    display.show(first_number)
    sleep(1000)
    display.show(second_number)

    This code retrieves the first and second elements of the list (at index 0 and 1) and displays their values.

    Try changing the index value in numbers[index_value] to retrieve different items from the list, and run your code again. e.g. numbers[3] will retrieve the forth item in the list.

    5 - Change List Elements

    In this step, we will learn how to change the value of an element in a list. We will update the value of the 3rd item in our list and display the updated value on the micro:bit screen.

    Update your code to the following:

    from microbit import *
    
    numbers = [1, 2, 3, 4, 5]
    
    # show the 3rd item
    display.show(numbers[2])
    sleep(1000)
    
    # change the value of the 3rd item
    numbers[2] = 9
    
    # show it again with the updated value
    display.show(numbers[2])
    This code first displays the original value of the 3rd item in the list (which is 3) on the micro:bit screen. Then, it changes the value of the 3rd item to 9 by using the assignment operator (=). Finally, it displays the updated value (which is now 9) on the screen. By doing this, you can see how easy it is to change the value of an element in a list.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more