Exercise 2 - Importing Modules
Goals
- Open, edit, save, and run code
- Import modules to bring in additional functionality
Preparation
- Connect your board to the computer and open the
code.py
file. Delete any pre-existing code from the board. See getting started for help with this step.
Concepts & Vocabulary
- import
- module
dir()
Background Info
Building on your printing exercise, let's take a look at the board the way the computer sees it.
To complete this exercise, you're going to import the board
module and print out the contents of the board. The board
module is one we'll use a lot, because it saves us a lot of work. Not all boards are set up the same way - a built in LED might be on pin 12 on one board, pin 13 on another, and pin 4 on a third board. Instead of modifying the code everytime you use a different style of board, you can use board.LED
to refer to that pin no matter what microcontroller pin it's actually on.
The dir()
function (which stands for directory) will return (not print) a list all the contents of a folder or any other object in Python. Because it returns the list as a value, it can be used as an argument for another function, like the print function.
Try It
Look at the list that is printed out. Can you tell what any of these terms mean?
code
output
Show me your code in action
To get credit for this exercise, I will need to see your code running. This will be scored for completion. If I am not available to check your work when you're ready, make sure you use Save As
to save a copy on your computer (not only on the board!) so you can continue on and show me the code running later.