E21 Computer Engineering Fundamentals
September 3, 2026
See Google Sheet sent out by Prof. Zucker!



* Drag-and-drop UF2 file onto CPLAYBOOT drive; if you see CIRCUITPY as an external drive, you have succeeded. Find instructions on Lecture 1 page.
Also:


code.py on board the file
code.py

The CPX can do some things without loading any libraries. e.g., try:

This uses the computer on board the CPX to calculate 2+2.
Python features
Unlike MATLAB, semicolons are irrelevant. Don’t use them!
The CPX comes with a library of programs to control it, but you have to load it. Enter:
Find the documentation for this library here.
Tip
Keep this snippet of code handy if you’re slow to type; you’ll have to copy-and-paste it many times!
cpx.lightcpx.accelerationcpx.temperaturecpx.touch_A4, cpx.touch_A3 etc.
Play a tone at x Hertz for y seconds: cpx.play_tone(x,y)
Light up pixel number N with a light made up of Red, Green, and Blue elements:
cpx.pixels[4] = (30,100,20)
This will produce light on pixel number 4 with the following components:
Red: 30, Green: 100, Blue: 20
Variable Assignment
Assigning a value to a variable in Python occurs with =. Thus a = 5 takes the value 5 and assigns it to the variable named a.
A conditional is a statement that evaluates to True or False.

Conditional operators — comparing two numbers
>: Greater than<: Less than==: Equal to!=: Not equal to>=: Greater than or equal to<=: Less than or equal toTry the following code:
cpx.light > 100
Next, try this with a cell phone flashlight shining on the CPX.

Try the following lines of code inside Thonny Code Editor in interactive mode.
A conditional statement of the form cpx.light > 100 checks whether the variable cpx.light is more than 100 and returns an answer: True or False
This is a special type of variable called a Boolean
Boolean variables
are either True or False. There are no maybe’s!
True (without quotes)False (without quotes)(2 > 3) with or without parantheses(cpx.light < 100)Python has two main binary logical operators that operate on two Boolean variables on either side:
x and ya or bThere is also a logical operator that operates on only one Boolean variable:
not cTry the following code line by line:
x = True
y = False
z = True
Then try using and, or and not on the three variables x, y and z and observe the results.
for the and operator
Variable A |
Variable B |
Result of A and B |
|---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
for the or operator
Variable A |
Variable B |
Result of A or B |
|---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
| Type | Name | Purpose |
|---|---|---|
| String | str |
String of text |
| Integer | int |
Whole numbers |
| Floating-point numbers | float |
Decimals |
| Boolean | bool |
Truth value |
| List | list |
Ordered list of variables |
| Tuple | tuple |
Un-alterable list |
| Range | range |
A sequence of numbers |
Check the type of a variable x using type(x)
Python features
Parantheses around a single variable don’t do anything.

float and intints and floats.What’s a floating-point number?
We will get to this later in E21!
list and tuple typesLists and tuples in Python are zero-indexed.
Lists can be modified.
Tuples, once set, cannot be changed.
range typeRefers to a range of whole numbers.
You can access different elements of a range the same way you would access elements of a list.
Your computer saves memory if you use range instead of list.

| Usage | Code |
|---|---|
From 0 to N-1 |
range(N) |
From a to b-1 |
range(a,b) |
From a to b in steps of c but excluding b |
range(a,b,c) |
while loop makes code run for ever… while the loop’s condition is Truecpx.play_tone(440,1)px.play_tone(880,0.5)But be consistent.
Nested Loops
Two loops at the same indentation level are not nested:
E21 • Fall 2026 • Lecture 02 • September 3, 2026 • ↩︎