Lab: Getting Started With Python
Preparation
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.
You will need to begin by setting up your COURSES directory. You can find instructions in the Course Directory Setup page. Note that you will only be able to do this if you are enrolled in the course and have Moodle access.
Be sure your partner has a chance to follow these steps to set up their COURSES directory, too!
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. If you created an alias for it, you need only double click the alias on your desktop to mount it.
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 directory into the 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 application.”)
$ cd /Volumes/COURSES/cs111-03-f18
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 StuWork
directory, so change your directory one more time 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.
$ ls
e. Change directories into that folder by typing cd USERNAME
.
f. Create a folder called lab-09-12
to hold all of today’s code. You can do this by executing the command
$ mkdir lab-09-12
g. Finally, change directory into your newly created lab-09-12
folder.
$ cd lab-09-12
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 directories. These act as a workspace that is useful to create and edit files. Open your StuWork
directory by doing the following:
- Go to
File -> Open Folder
- On the left-hand-side panel, navigate to the
COURSES
drive. (You might need to find it undercmc
or something similar.) - Continue navigating to the folder by selecting
COURSES
, thencs111-03-f18
, thenStuWork
, thenUSERNAME
.
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-09-12
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, <YOUR NAME(S) GO HERE>
# 2018-09-12
#
# 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-09-12
directory by typing.
$ pwd
It should print your current 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 returns 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.)
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.
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-09-12
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-03-f18
directory in the Terminal
and execute the command
$ cp -r StuWork/USERNAME/lab-09-12 Hand-in/USERNAME
This copies all files in the StuWork/USERNAME/lab-09-12
directory into a new directory Hand-in/USERNAME/lab-09-12
.
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.
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!