Lab 1: Introduction to the Python/git workflow

Objectives

  • Set up your computer for the CS 134 labs (optional, but recommended).

  • Write your first Python program.

  • Gain experience using the git source control system.

Setting up Your Computer


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.

We strongly encourage you to use our lab machines during your scheduled lab session. You may use these machines in the evenings as well. However, we also encourage you to take the time to configure your personal computer for use in CS 134. This will give you maximum flexibility. To this end, our lab instructions this week start with a tutorial for setting up your computer. Note that you will need a laptop or desktop to do this. Other devices, like iPads, won’t be sufficient.

If you have trouble with the setup, please talk to the TAs or instructors during your lab period.

Getting Started in Lab

You’ll need to follow a few steps to begin working on our lab machines regardless of whether or not you have setup your own computer. These steps are similar to what you did to set up your own machine, but simpler since all of the software has already been installed.

  1. Open the Terminal — on the lab machines, the Terminal application is identified by a > symbol and should be in the dock (i.e., the bar at the bottom of the screen).

  2. Type the following commands into the Terminal, 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
    
  3. Now we’ll set up your cs134 directory on our machines, similar to how you set up your machine, using the make directory (or mkdir) Unix command:

    mkdir cs134
    
  4. Descend into the cs134 directory you just created using the change directory (cd) command:

    cd cs134
    
  5. We must retrieve the files we need to complete this week’s lab from the CS server. Log on to https://evolene.cs.williams.edu in your browser (such as Chrome or Safari) using your CS username and password. Under Projects, you should see a repository named cs134-labs/23xyz3/lab01 where 23xyz3 is your CS username. This repository contains the starter files for this week’s lab assignment.

    Here, evolene.cs.williams.edu is the full name of the git server dedicated to holding our collective work. The cs134-labs reference is the CS134 lab directory on that server, and lab01 is the name of the repository holding the starter files for this week’s assignment. It is essentially the folder that contains all of your repositories on the server. You will be asked for your CS username and password whenver you access evolene. If you are off campus, you must connect to the Williams College VPN server before connnecting to evolene.

  6. Clone the repository: 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 to Clone with HTTPS.

    Return to the Terminal application and type git clone followed by the URL of the lab project you just copied (you can paste on Mac by pressing Command-V). This should look exactly like the following:

    git clone https://evolene.cs.williams.edu/cs134-labs/23xyz3/lab01.git
    

    where 23xyz3 is a place holder for your CS username.

Explore Your Repository

  1. The above step creates a new directory in your cs134 folder with the name lab01. Navigate to your lab01 subdirectory using the cd command in your Terminal:

    cd lab01
    
  2. 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, and hello.py.

  3. 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.

  4. Before continuing with Lab 1, we need to tell you how to open a file like hello.py using VS Code. To open this file on VS Code, simply click on the VS Code application icon in the dock (white square with a blue shape on it), go to File menu and Open hello.py.

If you’ve made it this far, congratulations! Now, you’re ready to (finally) begin programming.

Hello and Goodbye

  1. At this point, you should have set up your computer, cloned the Lab 1 repository, and opened hello.py in VS Code. If you had trouble with these steps, please ask the TAs or instructor to help you sort them out.

  2. In VS Code, 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.

  3. Save the file. Now, in the Terminal, at your $ prompt run the Python script by typing:

    python3 hello.py
    

    That command should print Hello, world! to the terminal.

  4. Congratulations, you’re a programmer! Let’s tell git that we changed the file. Type these lines in your Terminal:

    git add hello.py
    git commit -m 'I begin a life of programming...'
    

    The commit command will only store the changes of files you’ve add-ed.

  5. We can send these committed changed back up to our server, evolene. Type this in your Terminal:

    git push
    

    You might be asked for your CS password again. From now on, any time you git pull the lab01 repository, the hello.py file will reflect the changes you just made.

  6. It is always a good idea to see if your work has been copied (or 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.

  7. Sometimes in lab you will create new files that you want to turn in for credit. In VS Code, make and save a new file called goodbye.py. Then add the file and commit changes to our repository. Type this in your Terminal:

    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 git push your work, your changes have not yet been copied back to our server. You will do that later.

  8. 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’s input() function and ask the user for two numbers. Make sure the lines are not indented in any way. Add these lines to your file in VS Code.

    num1 = input("Enter a number: ")
    num2 = input("Enter another number: ")
    
  9. Now let’s compute and print the sum of our numbers. Why do you think we need the int function around the inputs num1 and num2? (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))  
    
  10. Save your work and run your program by typing python3 goodbye.py in the Terminal.

  11. Once you get that working, try experimenting with some other arithmetic operations (-, *, /).

  12. You can also play around with these commands in Interactive Python. When we run a program by typing python3 filename, we are running it as a script. Interactive Python is more like a calculator for trying out small snippets of code. To enter Interactive Python, type python3 in the Terminal at the $ prompt:

    python3
    

    This command starts an Interactive Python session. Notice the prompt >>> in the terminal. We can enter Python commands at this prompt and see the output of different expressions immediately. For example, you may want to try typing each of the following commands:

    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 pressing Crtl+D on a Mac or Ctrl+Z and Enter on a PC.

  13. 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 in git commit -am causes any file that has ever been add-ed to be committed, which is a nice shortcut and avoids having to manually add them again using two separate commands.

  14. Now use VS Code to write a short 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. Type this in your Terminal:

    git add AboutMe.txt 
    git commit -m 'Added a restaurant recommendation.'
    
  15. Every week we ask that you acknowledge any help you recieved from other students in the course. To do that, please edit the README.md file and enter the names of any such students on the Collaboration line. For practice, just write “Nobody” on that line and save the file. Now, commit that work. Type this in your Terminal:

    git add README.md
    git commit -m 'Added collaboration.'
    
  16. 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.

  17. 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!

  18. 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, we will notify you and you view our feedback using the steps outlined in our Viewing Feedback on Labs guide.

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 (Jupyter is installed on all the lab machines). To get started with Jupyter Notebooks, follow our How To Use Jupyter Notebooks guide.