In the next activity, you will be adding a third LED light, and then turning them on/off in custom sequences. It’s true that you could extend the coding approach you used for two LEDs, but there’s a better way: lists. (We'll practice lists using a serial terminal first, so there is no circuit needed for this activity).
A list is a sequence of items contained between brackets, like this:
clist = ['green', 'yellow', 'red']
A script can access an item in a list by using the item's index value, starting with 0 from left to right. So, the index of 'green' is 0, 'yellow' is 1, and 'red' is 2. The instruction print(clist[2]) would display the word "red" in a serial terminal. A script can also change items a list using index values. For example, to change clist[1] from 'yellow' to 'blue' you would use clist[1] = 'blue'.
In this activity, you will use lists to display different sequences of printed colors in the terminal. In the next activity, you will instead use the lists to make the colored lights display in various sequences.
There are different ways to display items in a list. This activity will start by displaying items in a list “the hard way” because it’s good to get familiar with how to access items in a list by their index values. The Try This section introduces “the easy way” to access each item in a list with a loop that does the indexing for you. Last, but not least, the Did You Know and Your Turn sections show how to use your script to change, append, and display the contents of lists.
This first script repeatedly displays this in the python.microbit.org Serial terminal.
color = green
color = yellow
color = red
# first_list from microbit import * clist = ['green', 'yellow', 'red'] index = 0 while True: color = clist[index] print('color =', color) sleep(1000) index = index + 1 if index >= len(clist): index = 0 print()
In the script, clist = ['green', 'yellow', 'red'] creates a list with three items: clist[0] is 'green', clist[1] is 'yellow', and clist[2] is 'red'. The value of the index variable starts at zero. Inside the while True loop, color = clist[index] stores 'green' in color when index is 0, 'yellow' in color when index is 1, or 'red' in color when index is 2. Each time through the loop, index = index + 1 increases the value of index by 1. Before repeating the loop, if index >= len(clist) statement compares the value of index to the number of elements in the list. When index reaches 3, the statement evaluates to true, and index is set back to 0.
Yes, there is an easier way! A for color in clist: loop will automatically copy each successive item in clist into the color variable. If your script just needs to cycle through items in a list, this is the way to go!
Even though you have been working with lists of strings, lists can contain other data types, like int, and a microbit.pin object. Different types can even appear in the same list.
my_list = [‘yellow’, 1000, pin14]
Lists can be changed after they are created. In Python-speak, the term is mutable. Here are some ways to change lists in a script:
clist[1] = 'blue' # replaces 'yellow' to 'blue' clist.append('yellow') # adds 'yellow' as the last item in the list clist.index('red') # finds the first instance of 'red' in the # list and returns the index
Your application can even use both list items and their index values in a list; here is an example that uses a for loop. Note how it uses len(clist) instead of 3. The len(clist) call returns the length of the list. It is currently 3, but if you added items to the list, the result of a call to len(clist)would automatically increase.
for index in range(len(clist)): color = clist[index] print('index:', index, ', color:', color) sleep(1000) print()
Here is another example that uses enumerate instead of in range. Enumerate returns 2 values (a tuple) that contains the index number in the list and the item. In this example, the index number gets copied to the variable named index, and the color string (‘green’, ‘yellow’, or ‘red’) gets copied to the variable named color.
for index, color in enumerate(clist): print('index:', index, ', color:', color) sleep(1000) print()
Here is the script from the Try This section on the previous page [3] with statements from the Did You Know section above added. It replaces 'yellow' with 'blue', adds 'yellow' after 'red', and gets the index of 'red'.
In this activity, you used lists to display different sequences of color names in the serial terminal.
There’s an easy way and a hard way to cycle through items in a list. Using the variable city and the list name c, write the code for a 4-item list of cities with a while True loop that will print them the ‘easy’ way.
Solution:
clist = [‘city1’, ‘city2’, ‘city3’, ‘city4’] while True: for city in clist: print(‘city = ’, city) sleep(1000) print()
Links
[1] https://python.microbit.org/v/2
[2] https://learn.parallax.com/sites/default/files/content/Python/LED/list-how-it-works.mp4
[3] https://learn.parallax.com/tutorials/language/python/led-lights/intro-lists/how-it-works