0% found this document useful (0 votes)
30 views5 pages

Lab 1

This document introduces Python and the Thonny IDE. It guides the reader through writing a simple Hello World program, taking user input, and calculating input length. It also discusses collaboration policies and introduces common errors to help familiarize the reader with debugging.

Uploaded by

phnam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Lab 1

This document introduces Python and the Thonny IDE. It guides the reader through writing a simple Hello World program, taking user input, and calculating input length. It also discusses collaboration policies and introduces common errors to help familiarize the reader with debugging.

Uploaded by

phnam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSCI 141

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 Getting ready to program


If you’re using your own computer and can install software
You can download and install Thonny for free from https://thonny.org. It runs on Windows,
Mac, or Linux computers. If you’re having any trouble getting it installed, ask for help and
we’ll help you troubleshoot.

1
Figure 1: The Thonny IDE

If you’ll be using several computers, or can’t install on your software on your


computer
If you will be using a variety of different computers, that’s fine. Install Thonny on the computers
you can, and be sure to save your code somewhere in the cloud (e.g., save to Google Drive,
OneDrive, Dropbox, etc.) every time you stop working for the day. If you aren’t sure how to
do this, just ask - we can help. This way you can access the latest version of your code from
whichever computer you’re working from.
If you are working from a single computer but you can’t install Thonny, you can use a free
online Python programming app, such as https://repl.it, which allows you to write, run, and
store code all using a web browser so you don’t need to have any special software installed
locally. If you need help getting set up with repl.it or similar, let us know!
If you are using a computer you can’t install software on and which doesn’t have reliable
internet, ask the TA for assistance. Whatever your tech situation, we’ll work with you to help
you be successful in this class.

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 Getting Started with Thonny


Once it’s installed, open up Thonny; a screen similar to what is shown in Figure 1 should
appear. The version number after ”Python” in the Shell tab will likely be different; as long as
the major (first) version number is 3, you’re in good shape.
There are two different ways that Thonny can be used. You can use the interpreter (or
shell) directly, which will cause each line of code that you type into the Shell section to be
executed after you press return, or you can create and save a python program to a file, and
then run (execute) the file in its entirety. Because you’ll be submitting your python program

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):

# Author: Scott Wehrwein


# Date: April 3, 2021
# Description: Code for CSCI 141 Lab 1
print("Hello World")

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.

Figure 2: Hello World Output

4 Input, len, and variables


One important feature of most programs is that they somehow interact with the user and allow
the user to input data. The input function in Python is one way to accomplish this: the
program to prompts the user and then waits for some input to be entered using the keyboard.
You saw in lecture that we can call input function with no arguments to effectively pause
the program until the user presses enter, as in

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:

name = input("What is your name? ")


name_length = len(name)
print("Hello", name)
print("You have", name_length, "characters in your name.")

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.

Figure 3: Example output for the full program.

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.

Figure 4: Sample Error Message output by python

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

You might also like