Lab 1
Lab 1
Spring 2021
Lab 1: Introduction to Python and the Thonny IDE
Due Date: Friday, April 9th at 9:59pm
Introduction
The purpose of this lab is to introduce you to Python and Thonny. In lecture, we’ve talked
about what programming is about and seen a few examples of very short Python programs. In
this lab, you’ll expand on upon that example, and get some hands-on experience in writing and
running your own Python program.
Since there’s a wide variety of backgrounds in this class, everyone will progress at different
speeds through this lab - that’s ok! If you finish quickly, hang around and help out your lab
partner if they could use it. My goal is to design these labs such that you have enough time
to complete them during the lab session. If you do not, be sure to upload your submission to
Canvas by the due date. If you have questions, be sure to ask the TA. Ask questions often.
Labs are your opportunity to get personalized help!
Collaboration in Lab
You are encouraged to work together with your lab partner in completing the labs. You and
your lab partner are free to discuss code freely, and help each other debug. However, you should
avoid copying code exactly - make sure that you are the one writing your own code. If by the
time you’re done, you could not sit down and write the code you’ve submitted by yourself
without help from anyone else, you have both violated academic honesty and done your future
self a disservice.
Please note that this mode of collaboration is specific to labs, and only allowed with your
lab partner. For programming assignments, you may not share your code with your classmates.
Keep in mind that copying code or allowing your code to be copied are both violations of
academic honesty.
Notation
There are two basic fonts in use in this document (this is common to many other help documents
and computer books you will use). One is the font that I have used so far in this document and
then there is this font which is a fixed width font that I will use to denote exact things you
should type, exact names of menus to use, website addresses and so on. If you have questions
about this notation ask your TA for clarification.
1
Figure 1: The Thonny IDE
Organization
Whether you’re storing things locally or in the cloud, it’s a good idea to keep your files organized.
I recommend creating a folder called csci141; inside that you can make subfolders for each lab
or assignment. For example, you can create a folder lab1 to store your code for this lab.
2
via Canvas, all instructions in labs and homework assignments will ask you to save a python
program to a file.
Select New from the File menu, which will begin a new file. Then select Save As from the
file menu, and save your file as lastName firstName lab1.py in your lab1 folder.
As a first step, you’ll recreate the quintessential Hello World program that we saw in class.
An important of programming well is placing comments throughout your code to document your
work. Comment lines among python code are ignored and not executed when the program is
run. To insert a comment, begin the line with a hash (or pound) symbol, #. You saw in lecture
how to use the print function to output text to the console.
3 Hello World
Type into your editor window of python (edit your file) the following (use your name and the
current date):
Notice how the IDE colors different parts your code differently. Comments are grey, and
the words Hello World, which are enclosed in double quotes, are colored green, to signify that
those words are a string literal. You’ll learn more about strings later in the course, but now
just think of it as text: the double quotes tell Python that what’s inside them shouldn’t be
interpreted as more code, but as a piece of data representing some text.
That’s it - your first python program. It contains comments (which will be ignored by the
python interpreter), and a single call to the print function, which will print the phrase Hello
World to the console.
Save your program to your lab1 folder (File -¿ Save as..., Ctrl+S), then run the program
by either selecting Run Current Script from the Run menu, clicking on the green circle with
the right-pointing arrow, or press the F5 button on your keyboard. If your code has no syntax
errors, you should see something similar to what is shown in Figure 2.
3
input()
If we provide a string as an argument to input, it will print the string as a prompt before
waiting for input.
Once the user provides input, you’ll often want the program to to store whatever the user
entered somewhere in memory so that you can refer to that data when needed. Programming
languages rely on variables as place holders that “remember” where a piece of data is stored in
the computer. The concept of a variable will be discussed extensively in lecture. For the time
being, think of a variable as an easy-to-remember name for a piece of data. To set the value of
a variable use the assignment operator, =. To remind yourself that = means the variable on the
left gets set to the value on the right, pronounce = as ‘gets‘.
You’ve learned the basic use of two functions so far: input and print. Here’s another one:
the len function, which is short for length, calculates the length (number of characters) of its
argument (i.e., whatever is inside the parentheses to the right of len).
Add the lines of code below to your python program. Type the text exactly—double quotes,
commas, parentheses, line breaks, etc. – all of it:
When executed, the four lines of code print to the console What is your name?, and then
wait for the user’s input. Once the user provides input and presses return (enter), then the
user’s input is placed into the variable name. The len function is invoked to calculate the length
of the data in the variable name. The output of the len function is placed into the variable
name length. The second-to-last print function causes the program to print the phrase Hello
followed by the value that is stored in the variable name. Lastly, the count of characters in the
user’s name (as calculated by the len function) is also output to the console.
Run your program. If your code has no syntax errors, you should get something similar to
what is shown in Figure 4.
5 Errors
Learning how to code means being able to find and fix syntax errors. Moreover, even if your
python code has correct syntax, there may be other errors that may cause the program to crash.
Knowing how to read error messages is an important skill because they inform you where in
the code there is an error.
In this section, you’ll introduce some syntax errors on purpose to get familiar with what
you’ll see when something goes wrong. Intentionally change print to printt, or len to LEN,
or omit the parentheses around What is your name in the call to the print function. Any of
these will generate an error when the program is run.
4
Save and run your program, and look at the red error message. A sample output is provided
in (Figure 4). Assuming you had mistyped your code unintentionally, what information does
the error message provide that you can use to troubleshoot your code? Notice that the error
message tells you what line of code has the problem. Moreover the python interpreter tells you
what exactly on a specific line of code it could not understand.
Play around with small modifications to your code and get a feel for the different error
messages. There are many, many rules about what is valid Python syntax. You will see lots
of examples of correct Python code (and you’ll encounter your fair share of incorrect code, as
well!), but many questions are best answered by trying things out. Get creative about adding
or removing spaces and newlines, changing capitalization, and so on.
When you get an error message, pay close attention to how to interpret what the error
messages say about where the error is coming from. This will come in very useful later, when
you need to locate an error you’ve made unintentionally! You’ll also notice that Thonny provides
some helpful suggestions for locating the error in the Assistant panel on the right.
Submission
That’s it for this week’s lab—don’t worry if this seemed simple: future labs will be more
involved.
Before you submit, undo the errors in your code so that your python program runs correctly.
Save your .py file, and upload it to Canvas. For this lab, Canvas has been configured to permit
only .py submissions. If you have never used Canvas, log into your canvas account, proceed to
CSCI141, and you should see an option for items that are open for submission. Click on Lab
1, and select to upload a file.
Rubric
Top of program file contains comments, including your name 3 points
The program does not contain any syntax errors and runs as intended 2 points
The program uses input, len, and print 5 points
Total 10 points