The purpose of today's lab is to practice using variables and expressions. There are 3 components:
Read the instructions below carefully and complete each exercise as indicated. For today's exercises, if possible, use pair programming. Only one copy of your code will need to be submitted (with each of your names and login ids), but you will take turns typing. Make sure each of you understands each step before continuing. At the end of the lab you will send a copy of all exercises to your partner so you will each have a copy.
In Lab01 we worked with Thonny to write our programs. We wrote files and then asked Thonny to execute the commands in the file when we pressed the “Run” button. It is also possible to use Thonny to run python in “Interactive Mode”. Instead of writing an entire file of commands, we can instead give commands one by one and look at their result each time.
cd cs108
, and then mkdir lab02
. This is where we will save all of our
work from today. Open Thonny. If you have any files from last week still open,
now would be a good time to close them.
Shell
pane in the bottom half of the Thonny window, then in the menu system, select View → Shell.
The console at the bottom of the screen is now an interactive version
of Python. It begins by printing some information about the version of
Python that you are running, and then prints >>>
as a
prompt. At this prompt, you can enter any valid Python command, hit
enter, and the system will respond with the result and a new prompt.
3 + 4
2 + 3 * 5
8 - 4 - 2
(3 + 7) * 2
13 % 4
8.2 // 4
2**10
5.1 % 2
As you can see, Python understands mathematical expressions, including precedence and associativity. Are any results surprising? Why would this be? Try a few other expressions and see if Python behaves as you expect.
To save your work, createa New file in Thonny. Then, select everything in the Shell window and use Control-C (or
Edit → Copy). Paste the contents into the new file.
Now, Save the file as expressions.txt
.
In case you were wondering, as with most programming languages, Python does not need a fancy IDE such as Thonny to work. In fact, Python can be run directly in a terminal window.
There are many different number puzzles; this one is usually attributed to Albert Einstein. Apparently he would write a number on a piece of paper, give it to someone else, and then let that person (or anyone else) pick any 3 digit number where the first and last digits differed by at least 2. After some math, regardless of which number was chosen, the result was always the number written down by Einstein before any number was chosen. Today, we will write a program that simulates this puzzle.
lab02
folder with the name einstein.py.
Add header documentation for
the purpose of the program, the date, and you and your partner's names
and login ids. Now, implement the following algorithm:
number
. Note: For
our purposes, assume that the user of the program can correctly
choose a number.digit1
.digit2
. Hint: You can get this digit using this
expression: (number//10)%10
.digit3
.rev_number
.
number
and rev_number
in a variable called difference
.
We want this result to always be positive, so use the abs
function. e.g. abs(4 - 8)
is 4.difference
in a variable called rev_diff
. Note that this requires
you to do those four steps again, which this new number.difference
and rev_diff
Your program should always print a result of 1089.
In lab01
we used turtle graphics to draw pictures. One of the
limitations of the approach we used there was that there was no easy
way to scale our pictures to be bigger or smaller. In this exercise,
we will use expressions and turtle graphics to create an easily
scalable stick figure.
Create a new file called stick_figure.py
in your lab02
folder. Add appropriate documentation for your goal, the date, and
the authors.
Add appropriate import
statements to use turtle graphics and the math
library, which is helpfully called math
.
Often within a program there are values that are not intended to be changed. The convention (or “good idea”) within Python is to give such values a name, using all capital letters, to help future readers of the code understand that this value should remain constant throughout program execution. Variables with names that are all capital letters are then referred to as “constants”.
Use turtle graphics to draw a stick figure as
shown. Below are some tips:
|
![]() |
We will grade these exercises according to the following criteria:
Submit your solutions to these lab exercises using your appropriate submission environment.
If you worked with a partner, you can (and should) send a copy of the files using the instructions here: send copy instructions
If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!