Lab: Getting Started with Python

5 minute read


These exercises should be completed with your assigned partner. Both of you will take turns playing the role of the driver (the one currently screen sharing) and the navigator. The driver’s responsibility is to type into Thonny, etc., when completing each exercise, and the navigator’s responsibility is to support the driver by reading the exercises, looking up relevant readings, and identifying errors. At all times you should be working together on the exercises rather than completing them independently.

Before you start working through the exercises, you should do the following:

  1. Introduce yourselves to one another and exchange preferred contact information for collaborating on Assignment 2 that is due next week.

  2. Make sure both of you have Thonny installed. If you do not, follow the instructions on the Installing Thonny page.

  3. Determine whether you or your partner’s name comes alphabetically before the other. The partner whose name is alphabetically first is assigned the role of the driver today and should share their screen. During the next lab, you and your partner will switch roles.

Warning! Since Python files are programs and could be malicious, Microsoft Outlook does NOT allow you to send Python files via email. You will have to share Python files via Zoom or Microsoft Teams.

Exercise 1

Now we will write our first Python program. As is tradition in CS, we will start with the “Hello, world!” program that simply says “hello” to the user.

a. Since we will be creating many files in class throughout the semester, I suggest you create a directory called cs65 somewhere on your computer to store class files.

b. Then create a subdirectory inside cs65 named lab-02-04 to store today’s work.

c. In Thonny, make sure that you are editing a new file by verifying that the only open tab is <untitled>. If you are not editing a new file, click File -> New to create one.

d. Save the blank file as in your lab directory by clicking File -> Save and navigating to the directory you just created.

e. Copy and paste the following code into your newly created file and add your names where the placeholder is.

# A simple "hello world" program
# Author:
print("Hello, world!")

Note: The # character starts defining a “comment”. Every character on a line following a # is ignored by Python, so the only code being executed is the print statement.

f. Be sure to save your file by clicking File -> Save.

g. Execute your program by doing one of the following:

  1. Click Run -> Run current script,
  2. Click the green “Run” button on the toolbar, or
  3. Press the F5 key.

When you run a program in this way, the currently opened file in the editor will be executed and the output is printed into the shell.

h. Verify that Hello, world! is printed to the shell.

Exercise 2

This exercise is designed to get you familiar with the Python shell environment. Recall that the shell allows you to give commands to the Python interpreter interactively. Every line you type into the shell is immediately executed, and any value it produces is automatically printed to the display.

a. Type the following into the shell to see what happens.

>>> print("Hello from the shell!")

Note: When I include the >>> prompt in front of the command, I intend for it to be run in the shell. Do NOT type the >>> preceding the command into the shell—it is included by the shell automatically.

b. Now try the following:

>>> "Hello from the shell!"

Notice that the shell automatically prints the value back to you. Because the shell automatically prints the result of Python code, we can experiment with all sorts of things without needing to type print(...) every time.

c. Execute the following commands one after another to see what the result is.

>>> 2 + 3
>>> 2 + 3.0
>>> "2" + "3"
>>> 2 * 3
>>> 2 ** 3
>>> 7 / 3
>>> 7 // 3

d. What do you think ** and // do? Discuss your ideas with your partner and try experimenting with them on a few more inputs to see if you can figure them out.

Tip: In the shell, the Up and Down arrow keys navigate through previous commands that you’ve executed.

Exercise 3

Let’s take a closer look at a program named This program is an example from John Zelle’s book Python Programming.

a. Copy and paste the following code into a new file in Thonny and save it in your lab directory as

# A simple program illustrating chaotic behavior
# Author:
#   John Zelle, from Python Programming, Third Edition
def main():
    print("This program illustrates a chaotic function")
    x = float(input("Enter a number between 0 and 1: "))
    for i in range(10):
        x = 3.9 * x * (1 - x)


Note: Whenever you use code written by someone else, you should always cite.

b. Run the program.

c. In the shell, you should be prompted to enter a number between 0 and 1. Type in a number of your choice (e.g. 0.1415) and press the Enter key. You should see 10 new numbers printed.

d. Run the program again and type in a different number.

e. What do you think def main(): does on the first line? What about main() on the last line? Discuss with your partner.

f. For each of the following code fragments, discuss with your partner what you think they do and what their role is in the program. If neither of you are confident, ask the instructor.

  • input("Enter a number between 0 and 1: ")
  • float(...)
  • for i in range(10):
  • x = ...

Note: We will discuss all of the components of this program in much more detail later, so don’t worry if they are confusing at this time!

g. What happens if you type a number outside the range 0 and 1? First, predict what you think will happen, then run the program to verify your prediction.

h. What if you type in something that is not a number at all?

i. Try modifying the program to see what the changes do. What if you change the 10 to 20 in the range function?

How to Submit Your Lab Work

  1. Once you are finished with the lab, you should go to codePost and log into your account with your Drake email address.
  2. Go to the CS 65 course.
  3. Go to Lab 1 and upload your and files.
  4. After you submit, open up your submission again. In Partners tab at the top of the submissions window, there will be a link. Send this link to your partner and have them open it while logged into their account.
  5. If all goes well, you should see both yourself and your partner as authors of the submission.
  6. That’s it! You’ve just submitted your first lab.

Finished Early?

Start working on Assignment 2 together!