Lab 1: Introduction to the Python/git workflow¶
Objectives
Set up your account.
Write your first Python program.
Gain experience using the
git
source control system.
Prelab Preparation¶
The goal this week is to become comfortable working with Python and Git.
We will spend some time writing short programs that are designed to
provide a gentle introduction to python3
. We will also gain some
experience using git
to check out and turn in our work.
During scheduled lab sessions, you will always work on the computers in our lab. They have been configured for you. If you have a personal computer, we encourage you to take some time configuring it so that you will be able to work on assignments on your own computer as well. Please see the Mac and Windows Setup Guides (found under Resources on the course webpage) for detailed instructions.
We will frequently use the Terminal
application as part of our lab
workflow. On the lab machines, the Terminal
application is identified
by a >
symbol and should be in the dock.
Establishing your Identity¶
We have sent you your Computer Science credentials: a username and password that allows you to access our facilities. Those credentials identify who you are with our servers. Please make sure you change your password, as described in the email you received. We also sent you a two digit code that will be your anonymous identifier throughout the semester. At times, we may ask you for your two digit id to identify who you are, with our graders.
Before you begin working on this week’s lab, you need to help your
machine identify who you are, as well. Let us establish your git
identity, which is used to digitally sign your submitted work.
Open the Terminal
application on your computer and type the following
commands into the shell, replacing Ephelia Williams’s name and
email with your own:
git config --global user.email 'ew5@williams.edu'
git config --global user.name 'Ephelia Williams'
git config --global push.default simple
git config --global pull.rebase false
git config --global core.editor nano
Organizing your Work¶
Unix directories correspond to folders on the Mac or Windows
operating systems. You will be creating many files on your computer for
this class. We suggest that you put all of your work in a single folder,
called cs134
, located in your home directory. From within the
Terminal
window, do the following:
Create your cs134 directory using the make directory (
mkdir
) command:mkdir cs134
Descend into the
cs134
directory using the change directory (cd
) command:cd cs134
Now we’re going to retrieve the files we need to complete this week’s lab. On a web browser, log on to
https://evolene.cs.williams.edu
. Enter your CS username and password to log in.On the homepage after you log in, under Projects, you should see a repository named
cs134-labs/22xyz3/lab01
where22xyz3
is your CS username. Click on it. You should see the starter files for this week’s lab.Now find the blue button that is a drop-down menu that says
Clone
. Click on it and click on the “clipboard” icon (Copy URL) option next toClone with HTTPS
. This copies the URL of the git repository for this lab.Return to the
Terminal
application and typegit clone
followed by the URL of the lab project you just copied (you can paste on Mac by pressingCommand-V
). The typed line should look exactly like the following:git clone https://evolene.cs.williams.edu/cs134-labs/22xyz3/lab01.git
Here,
evolene.cs.williams.edu
is the full name of thegit
server dedicated to holding all of our collective work. Thecs134-labs
reference is the CS134 lab directory on that server (many courses make use ofevolene
), andlab01.git
is the name of the repository. The identifier22xyz3
is a place holder for your CS username. It is the folder that contains all of your repositories on the server. If you are asked for your CS password, type it and press enter.The above step creates a new directory in your
cs134
folder with the namelab01
. Navigate to your lab01 subdirectory using thecd
command:cd lab01
Explore the contents of the lab01 directory by using the
ls
(list) command:ls
You should see the files
AboutMe.txt
,GradeSheet.txt
,README.md
,hello.py
, andhonorcode.txt
.Whenever you begin a session of work, you should make sure you get the latest copy of your work. This is called pulling the repository from the server. You should:
git pull
Since we just cloned the repository, it’s unlikely that anything is out-of-date. Still, we always perform this simple check.
If you’ve made it this far, congratulations! Now, you’re ready to begin work.
Hello and Goodbye¶
First, we’ll edit the
hello.py
file. To open this file on Atom, click on the Atom application icon in the dock (green circle with an atom symbol), go to File menu and Openhello.py
.In Atom, type the following Python command in the file
hello.py
:# My first python program! print("Hello, world!")
Anything after the
#
symbol in a Python script is a comment meant for human readability. We will make use of comments extensively when we write larger programs.Save the file. Now, in the Terminal, at your prompt (represented by the
$
, here) run the Python script by typing:$ python3 hello.py Hello, world!
Congratulations, you’re a programmer! Let us tell
git
that we changed the file:git add hello.py git commit -m 'I begin a life of programming...'
The
commit
command will only store the changes of files you’veadd
-ed.We can send these committed changed back up to
evolene
:git push
You might be asked for your CS password again. From now on, any time you pull the
lab01
repository, thehello.py
file will reflect the changes you just made.It is always a good idea to see if your work has been pushed to the server. In a web browser, log on to http://evolene.cs.williams.edu/ again using your CS credentials. Find the lab01 respository on the homepage. It should list the name of the files in it, the commit message from your last commit, and the time of your last update. You can also look at the file
hello.py
by clicking on it. It should now contain your changes.Sometimes you will create new files that you want to turn in for credit. In Atom, make and save a new file called
goodbye.py
. Then add the file and commit changes to our repository:git add goodbye.py git commit -m 'Another great program.'
You should commit every time you think you’ve made progress. Committing is an important part of managing the progress you make. It is also helpful for backing up your work. Note that since you did not push your work, your changes have not yet been copied back to our server. You will do that later.
To demonstrate some of Python’s arithmetic capabilities, we will add a few more lines to the file
goodbye.py
. First, let’s ask the user for input. To do this, we’ll use Python’sinput()
function and ask the user for two numbers. Make sure the lines are not indented in any way.num1 = input("Enter a number: ") num2 = input("Enter another number: ")
Now let us compute and print the sum of our numbers. Why do you think we need the
int
function around the inputsnum1
andnum2
? (Just something to think about, no need to give an answer.) Also, notice our use of comments to describe our code. Once again, be careful not to indent.# compute sum sum = int(num1) + int(num2) # format and print sum: print("Sum of {} and {} is {}".format(num1, num2, sum))
Save your work and run your program by typing
python3 goodbye.py
in the Terminal.Once you get that working, try experimenting with some other arithmetic operations (-, *, /).
You can also play around with these commands in Interactive Python. To enter interactive python, type
python3
in the Terminal.$ python3
This command starts an interactive python session. The three carrots
>>>
represent the interactivepython3
prompt where we can enter Python commands. Here you can see the output of different expressions immediately and interactively. For example, you may want to try the following:>>> num1 = input("Enter a number: ") >>> num2 = input("Enter another number: ") >>> num1 # what is in num1? >>> num2 # what is in num2? >>> int(num1) + int(num2) # what happens if we add? >>> num1 + num2 # what happens without int?
Once you are done experimenting with different arithmetic operations, you can exit out of interactive python by typing
exit()
or pressingCrtl+Z
.Whenever you’re finished with a work session, you should always commit one last time and push the changes to the server:
git commit -am 'Done with work today.' git push
The
-a
switch ingit commit -am
causes any file that has ever beenadd
-ed to be committed, which is a nice shortcut and avoids having to manually add them again.Now use Atom to write a paragraph about yourself in the file called
AboutMe.txt
. Who are you? What do you enjoy doing? Where are you from? Where can you get good food in your hometown? Add and commit this work:git add AboutMe.txt git commit -m 'Added a restaurant recommendation.'
Every week you will need to certify that your work is your own. This is a crucial part of every assignment! Edit
honorcode.txt
and, if appropriate, write your name below the Honor Code statement. If you received help from other students in the course, you must indicate who that was, as well. Now, commit that work:git add honorcode.txt git commit -m 'Signed the honor code.'
The
GradeSheet.txt
file describes the expectations we have for each lab, and is where you can find our comments after we grade your work. You might find it useful to look at this file before you turn in you work to make sure you have not missed an important part of the assignment.Submit your work. After you have completed all of the above tasks, you can push your work up to the server to be graded.
git push
Always Push!
Make sure that you always
push
your work up to the server before your designated due date. If you don’t do this, we cannot grade your work!Remember that you can check that the most up-to-date version of your work is on the server by logging on to https://evolene.cs.williams.edu/. We recommend doing this after you have completed everything to make sure all files have been successfully pushed to our server.
Deadline and Feedback¶
Your work is due on Wednesday (for Monday lab groups) or Thursday (for Tuesday lab groups) at 10pm. By next Wednesday, we will grade your work and provide feedback.
After we have graded your work, you can use
git pull
to download the graded feedback, which will be in the GradeSheet.txt
file.
Optional: Check out CS134 Resources¶
We would like to point you to some useful CSCI 134 resources:
Programmer’s Toolbox goes over all the tools we used in this lab.
In lectures, we will be using Jupyter Notebooks as a teaching aid. It is a good idea to familiarize yourself with them (they are installed on all the lab machines). To get started with Jupyter Notebooks, follow our How To Use Jupyter Notebooks guide.