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.
To create a new Python project for a Microbit, open the website python.microbit.org.
This will open the code editor with a new project. It might have some example code already added such as:
# Imports go at the top
from microbit import *
# Code in a 'while True:' loop repeats forever
while True:
display.show(Image.HEART)
sleep(1000)
display.scroll('Hello')
You should delete this code except for the import
line that you will need. This imports the necessary libraries you will need to code a microbit.
# Imports go at the top
from microbit import *
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.
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.
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.
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.