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

Advanced Array Tactics

Delve into advanced operations on Python lists using MicroPython. Learn to sort lists in ascending and descending order, find the length of a list, and count occurrences of items in lists. Apply these skills to create a strong password generator project. Suitable for students with basic Python knowledge.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

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

    Lists are powerful tools in Python that allow us to store, retrieve, and manipulate collections of items. Today, we'll explore advanced operations you can perform on lists using MicroPython such as how to sort a list, find the length of a list, and count occurrences in lists.

    Create a new project.

    2 - Sorting a List

    In this step, you will learn how to sort a list in ascending order using the sort() method. Sorting a list means arranging its elements in a specific order, such as from smallest to largest for numbers or alphabetically for strings.

    Here's an example of sorting a list of numbers:

    from microbit import *
    
    numbers = [5, 2, 9, 1, 5, 6]
    numbers.sort()
    
    for num in numbers:
        display.show(str(num))
        sleep(1000)  # Wait for 1 second before showing the next number
    

    In this code, we first create a list called numbers with some unsorted elements. Then, we use the sort() method to sort the list in ascending order. Finally, we use a for loop to display each number in the sorted list on the micro:bit's LED display, waiting for 1 second between each number.

    Add and run the code to your editor to see it sort the values in the array and then display them.

    3 - Sorting a List (Descending)

    In this step, you will explore sorting a list in descending order. Instead of arranging elements from smallest to largest, you'll arrange them from largest to smallest.

    The sort() method can also accept an argument named reverse. When set to True, it sorts the list in descending order.

    Here's how to sort a list of numbers in reverse:

    from microbit import *
    
    numbers = [5, 2, 9, 1, 5, 6]
    numbers.sort(reverse=True)
    
    for num in numbers:
        display.show(str(num))
        sleep(1000)  # Wait for 1 second before showing the next number
    

    In the code above, we first define a list called numbers. We then sort it in descending order using the sort(reverse=True) method. Lastly, we display each number in the sorted list on the micro:bit, pausing for 1 second between numbers.

    Try adding this code to your editor. When you run it, you'll see the numbers displayed in reverse order on your micro:bit's display.

    4 - Finding the Length of a List

    In this step, you'll learn how to determine the number of items in a list. This can be especially useful when working with dynamic data where the list length can change or when performing operations that require knowledge of the list's size.

    To find out how many items a list contains, you'll use the len() function.

    Here's how you can get the length of a list:

    from microbit import *
    
    numbers = [5, 2, 9, 1, 5, 6]
    list_length = len(numbers)
    
    display.show(str(list_length))
    

    In the provided code, we've created a list named numbers. The len() function then calculates the number of elements in the list, and the result is stored in the list_length variable. Finally, the length of the list is displayed on the micro:bit's LED display.

    Enter the above code into your editor and run it. Your micro:bit will display the number of items in the list, which, in this case, is 6.

    5 - Counting Occurrences in Lists

    Now let's dive into understanding how often a particular item appears within a list. This can be helpful in many scenarios, such as data analysis, pattern recognition, and more.

    You can achieve this using the count() method associated with lists. This method returns the number of times a specific value appears in the list.

    Here's how you can count occurrences of a particular item in a list:

    from microbit import *
    
    numbers = [5, 2, 9, 1, 5, 6]
    occurrences_of_five = numbers.count(5)
    
    display.show(str(occurrences_of_five))
    

    In the provided code, we've initialized a list named numbers. The count() method then determines how many times the number 5 appears in our list, and the result is stored in the occurrences_of_five variable. We then display this count on the micro:bit's LED display.

    Input the code into your editor and run it. Your micro:bit will display the number '2', indicating that the number 5 appears twice in the 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