Preparation

a. We will always using the Mac OSX side of the computer. If you are booted into Windows, please reboot the machine and boot up Mac OSX.

b. Once you are logged in, double click the icon named courses.ads.carleton.edu on your desktop. If you encounter any errors, have me or the prefect contact Mike Tie to set up your account.

Finally, keep in mind that we will be using the courses directory a lot. Therefore, it will be advantageous to get into the habit of mounting it immediately after you log on!

Exercise 1

In this course, we will be primarily using two applications to write and run our Python programs: (1) Brackets for writing the source code of our Python programs, and (2) Terminal for executing our programs and displaying the output.

a. Open the Terminal application. On the Mac labs, you can press Cmd + Space to open the application launcher, type Terminal into the prompt, and press Enter.

b. Change your working directory to course directory by executing the following command in the Terminal. (Note that the $ symbol represents the “prompt” and does not need to be typed in. It is there as a visual hint that “this command should be typed into the Terminal.”)

$ cd /Volumes/courses/cs111-01-w19

If the above command failed, ask your instructor or prefect to verify the courses directory is mounted properly.

c. We will be working in your personal StuWork directory, so change your directory again by executing

$ cd StuWork

d. Inside your StuWork directory should be a directory named USERNAME where USERNAME is your Carleton ID. Verify this by listing the directories present using the ls command.

$ ls

Note that all the files and directories in StuWork will be displayed.

e. Change directories into that folder by typing cd USERNAME where USERNAME is your Carleton ID.

f. Create a folder called lab-01-09 to hold all of today’s code. You can do this by executing the command

$ mkdir lab-01-09

g. Finally, change directory into your newly created lab-01-09 folder.

$ cd lab-01-09

Exercise 2

In this exercise, you will write your first Python program. As is tradition in CS, we will start with the “Hello World!” program that simply says hello to the user.

a. Open the Brackets application. You can do this in two ways: (1) by clicking the icon that looks like two brackets[ ], or (2) by pressing Cmd + Space to open the application launcher and typing in “Brackets”.

b. The Brackets application allows you to open a working directory which acts as a workspace for creating and editing files. Open your StuWork/USERNAME directory by doing the following:

  1. Go to File -> Open Folder
  2. On the left-hand-side panel, navigate to the courses drive. (You might need to find it under cmc or something similar.)
  3. Continue navigating to the folder by selecting courses, then cs111-01-w19, then StuWork, then USERNAME.

c. Create a new file by going to File -> New File or by pressing Cmd + N.

d. Save the blank file as hello.py and place it in your lab-01-09 directory.

e. Type the following into your newly created hello.py file and add your names where the placeholder is. I prefer that you actually type all the characters rather than copying and pasting!

# hello.py
# Titus Klinge, ADD YOUR NAMES HERE
# 2019-01-09
#
# A simple "hello world" program
print("Hello world!")

f. Be sure to save your file!

g. Switch back to your Terminal and make sure that you are in the lab-01-09 directory by typing.

$ pwd

It should print your current working directory.

h. Execute your program by typing

$ python3 hello.py

You have just written and executed your first Python program! Congratulations!

Exercise 3

This exercise is designed to get you familiar with the Python REPL environment. REPL stands for Read, Evaluate, Print, Loop and it allows you to interact with the Python interpreter more directly. Every line you type into the REPL is immediately executed and any value it produces is then printed to the display.

a. In your Terminal window, type the following command to enter the Python REPL environment.

$ python3

This should give you a prompt that looks something like

Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

b. Type the following into the REPL to see what happens.

>>> print("Hello from REPL!")

(When I include the >>> prompt in front of the command, I intend for it to be run in the REPL environment. Do not type the >>> preceding the command into the REPL.)

c. Practice exiting the REPL by typing

>>> exit()

You can also exit the REPL by pressing Ctrl + C.

Exercise 4

Open up the REPL again by typing python3 into the Terminal and experiment with Python by executing the following commands one after another. Note that the REPL will print something immediately after each command.

>>> print("Hello", "world!")
>>> print(3)
>>> print(3.0)
>>> print(2 + 3)
>>> print("2" + "3")
>>> print(2 * 3)
>>> print(2 ** 3)
>>> print(7 / 3)
>>> print(7 // 3)

What do you think the ** and the // operators do? Experiment with them on a few more inputs and see if you can figure it out.

PRO TIP: In the Terminal, pressing the Up arrow key will navigate through previous commands that you’ve executed. This also works inside the REPL environment and may help you avoid typing print(..) so many times!

Exercise 5

In this exercise we will practice turning in a file through the Hand-in directory.

a. Modify your hello.py program to say "Hello from YOUR NAME(S) HERE.

b. Copy your StuWork/USERNAME/lab-01-09 into your Hand-in/USERNAME directory. I recommend that you do this visually through the Finder app, but an alternative way is to navigate to your cs111-01-w19 directory in the Terminal and execute the command

$ cp -r StuWork/USERNAME/lab-01-09 Hand-in/USERNAME

This copies all files in the StuWork/USERNAME/lab-01-09 directory into a new directory Hand-in/USERNAME/lab-01-09.

The -r in the copy command stands for “recursive” and makes the command copy all sub-folders and files contained in the folder, too.

That’s all there is to it! I will be able to access your Hand-in folder to retrieve your lab submission from my account.

Finished Early?

If you have extra time, start working on Assignment 2 with your partner!

Before You Leave

Get in the habit of emailing the code written during class to your partner. Since only one account is being used at a time, the files created will only be accessible from the user that is logged in. Later in the course, having these in-class files will be essential for reviewing and studying!

You should also consider exchanging contact information with your partner so that you can set up a meeting to work on the assignment.