E21 Computer Engineering Fundamentals
September 10, 2026
code.py on your CIRCUITPY drive is automatically run whenever the board is powered on with a USB cable or battery.code.py is paused unless you click code.py runs once whenever the board is reset. You can reset by:
time packagePython comes with several packages that need to be imported into programs. These packages can be:
random, math, calendar and time.numpy, for numerical computing, or matplotlib, for plotting.It is good practice to insert a time.sleep() command into every while True: loop to prevent the program from getting out of hand.
from adafruit_circuitplayground.express import cpx imports the class ‘Express’ which is known as cpx.cpx class posssess the function cpx.play_tone(440,1)cpx class possess the following attributes
touched — this attribute is set internally based on which pins are being touched.
cpx.red_led — this attribute can be set by the user
if statementsIn Python, if statements are followed by indented blocks of code, similar to while.
No end is needed.
elif stands for “else, if”
The code following an if or elif statement must be a conditional
True or False.cpx.light < 100 will be True.if else and elifThe following code is equivalent:
Using else followed by if:
Using elif
elifs in one blockif:elif:s as you wantelse:if statementsIf statements can be nested indefinitely:
It’s up to you to check if your nested if statements make logical sense. For example, run and compare the following programs
Option A:
# First, check if light < 200
if cpx.light < 200:
# If yes, then ...
cpx.play_tone(440,1)
# check if light < 150
if cpx.light < 150:
# if light is less than 150:
cpx.play_tone(330,1)
else:
# if light is not less than 150:
cpx.play_tone(330,2)
else:
# if no, light is not less than 200:
cpx.play_tone(440,2)Option B:
# First, check if light < 200
if cpx.light < 200:
# If yes, then ...
cpx.play_tone(440,1)
# check if light > 350
if cpx.light > 350:
# if light is more than 150:
cpx.play_tone(330,1)
else:
# if light is not less than 150:
cpx.play_tone(330,2)
else:
# if no, light is not less than 200:
cpx.play_tone(440,2)In this example, lines 5-8 will never execute.
Print statements are commonly used in programs, especially for debugging purposes.
The print() function takes as argument a string.
In Python, strings are enclosed in single quotes or double quotes.
"This is a string"'This is also a string'Try out this code and shine a flashlight:
We often want to embed variables into a Python string, before printing it.
Variables can be directly printed
Variables can be embedded into a string by enclosing in {} and preceding the string with the letter f like so:
When using f-strings, the precision of numerical variables can be specified using the notation :.nf where n is the number of significant figures after the decimal point.
Write a program that prints the temperature to 2 decimal places at 1 second intervals
Counter variables are used inside a loop to increment each time the loop is run.
A simple counter variable
In this example, k is a counter variable.
Counter variables can be conditional:
break keyword terminates a loopThe break keyword terminates the lowest-level loop in which it is found, immediately.
Compare the following
Option A
Option B
When using nested loops, break terminates the innermost loop only.
from adafruit_circuitplayground.express import cpx
import time
k = 0
while True:
time.sleep(1)
k += 1
print(f"Outer iteration {k}")
if k % 3 == 0: # k ÷ 3 has remainder 0
m = 0
while True:
time.sleep(1)
m += 1
print(f"Outer iteration {k}, inner iteration {m} ")
if cpx.touch_A2:
print("terminating")
breakModify this so that if pin A5 is touched, the outer loop terminates
continue keywordThe continue keyword immediately exits the current iteration of a loop and moves on to the next one.
Run and compare the following code
Option A
from adafruit_circuitplayground.express import cpx
import time
k = 0
while True:
time.sleep(1)
k += 1
print(f"Outer iteration {k}")
if k % 3 == 0: # k ÷ 3 has remainder 0
m = 0
while True:
time.sleep(1)
if cpx.touch_A2:
print("skip this iteration")
continue
m += 1
print(f"Outer iteration {k}, inner iteration {m} ")Option B
from adafruit_circuitplayground.express import cpx
import time
k = 0
while True:
time.sleep(1)
k += 1
print(f"Outer iteration {k}")
if k % 3 == 0: # k ÷ 3 has remainder 0
m = 0
while True:
time.sleep(1)
m += 1
print(f"Outer iteration {k}, inner iteration {m} ")
if cpx.touch_A2:
print("skip this iteration")
continueIn Option A, the inner loop is terminated upon touching A2 before the print statement, so line 16 is not executed if A2 is touched.
In Option B, the inner loop is terminated upon touching A2 after the print statement, so line 13 is executed even if A2 is touched.
pass keywordpass is used inside a loop (if, while, for, etc.) as a placeholder that doesn’t do anything.
for loopA for loop runs a specified number of times.
The iterating variable in a for loop has a different value each time the loop is run — like a built-in counter variable that you don’t need to increment.
A for loop iterates over an iterable object. These include:
List
Tuple
Range
String
Like any other block of code, you can nest a for loop inside another for loop.
A common use of nested loops in engineering is a ‘grid’ of variables
cpx.button_acpx.button_bcpx.switchcpx.start_tone(440) starts playing the tone 440 Hz.cpx.stop_tone() terminates whichever tone is being played.cpx.pixels.fill() takes as argument a 3-element tuple and fills all pixels with that color.
cpx.pixels.fill((0,0,0)) to switch off all neopixels.Write a program and save to code.py. Your program should:
x seconds, where x is a small number less than one.E21 • Fall 2026 • Lecture 04 • September 10, 2026 • ↩︎