Lab 1

Lab 1: Python, the Terminal, and You!

Objectives:

Setting up Your Computer

The goal this week is to become comfortable working with the Terminal, Python and Git. We will first spend some time getting familiar with the Terminal and how to navigate it. We will then write some short programs that are designed to provide a gentle introduction to python3. We will also gain some experience using git to save our work. Finally, we will use Gradescope to submit our lab assignments.

We highly encourage you to complete this first assignment using the Mac computers residing in the second floor computer science labs (TCL 216/217a). The lab computers are already equipped with all of the tools necessary to complete the assignment. After completing the lab within the CS lab environment, we encourage you to explore setting up your own programming environment if that is something that you wish to do.

Getting Started

  1. Log into the Mac computer using your OIT “Unix” account credentials.

  2. Open Visual Studio Code (often referred to as “VSCode”) by clicking on the VS Code application icon in the dock (white square with a blue shape on it).

  3. Close the “Get Started” tab (near the upper left of the window) using the X.

  4. To open the Terminal within VSCode, select Terminal -> New Terminal from the top menu bar. To open the Terminal Application provided by Mac OS, type Terminal in the Spotlight search and enter. You can use either during the course. In both cases, you should see a “prompt” that looks something like:

    ew5@tcl216-14 ~ %

Retrieving This Week’s Starter Code

  1. Now we’ll set up your cs134 directory on our machines. Type this in the Terminal:

    cd ~
    mkdir cs134
  2. Now descend into the cs134 directory you just created using the change directory (cd) command:

    cd cs134
  3. 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/24xyz3/lab01 where 24xyz3 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 whenever you access evolene.

  4. 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 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 like the following:

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

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

Hello and Goodbye

  1. Cloning the repository creates a new directory in your cs134 folder with the name lab01. We can double-check that this directory exists by using the ls command, which lists the files and subdirectories of our working directory (the “working directory” is a term for whichever directory we currently happen to be in):

    ls

    You should see a single directory, the lab01 directory that was created by your git clone command. Let’s enter that directory and explore it:

    cd lab01
    ls

    Since cd lab01 changed your working directory to your newly created lab01 folder, the ls command should display the contents of your newly cloned repository. In other words, you should see the files AboutMe.txt, README.md, hello.py, and the directory learn-the-terminal.

  2. Open the file hello.py using VS Code. Inside VS Code, go to File menu (located along the top of the screen on the desktop) and Open hello.py. This file is initially empty, but it will soon contain your first python program.

  3. In VS Code, type the following Python code in the file hello.py:

    # My first python program!
    print("Hello, world!")

    Anything after the # symbol in a Python script is a comment meant to improve human readability. We will make extensive use of comments when we write larger programs.

  4. Save the file inside VS Code. After doing so, you should see the “blue dot” at the top of the hello.py tab disappear, indicating that the contents in your window match the saved version. Now, in the Terminal, run the Python script by typing:

    python3 hello.py

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

  5. Congratulations, you’re a programmer! Let’s tell git that we changed the file. Type the following line in your Terminal:

    git status

    This should print several lines of text, but the most relevant is the section starting with “Changes not staged for commit:”, followed by “modified: hello.py”. This indicates that you have a file in your repository that is different than the version found in the most recent snapshot. We want to add your changes (called staging them) to the list of changes that will be included in the next snapshot. Type the following lines in your terminal:

    git add hello.py
    git status

    You should see slightly different output. Instead of showing red text, “modified: hello.py” should be displayed in green, and it should be in a section that starts with “Changes to be committed”. This means that the current version of “hello.py” will be included in the next snapshot that you create. So let’s do it; let’s create a snapshot (called committing) by typing the following command:

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

    The -m flag is needed to “name” your commit, and the text quoted after is the message that will be forever associated with the changes in this snapshot. Also note that the commit command will only store the changes of files you’ve explicitly add-ed.

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

  7. It is always a good idea to confirm that 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 repository 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.

  8. Sometimes in lab you will create new files. In VS Code, make and save a new file called goodbye.py. Then add the file and commit changes to our repository. To commit your new file to your repository, 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.

  9. 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: ")
  10. 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 " + num1 + " and " + num2 + " is " + str(sum))
  11. Save your work and run your program by typing python3 goodbye.py in the Terminal.

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

  13. 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:

    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.

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

  15. 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? After you’ve done so, add and commit this work. Type this in your Terminal:

    git add AboutMe.txt 
    git commit -m 'Added a personal touch to my lab assignment.'
  16. Every week we ask that you acknowledge any help you received 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.'
  17. The Assignment Expectations heading near the bottom of the README.md file describes the general expectations we have for each lab. 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.

  18. After you have completed all of the above tasks, you should 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.

Scavenger Hunt (Learn More About the Terminal)

  1. The Terminal provides us with an easy fully-text-based interface to navigate and manipulate the files stored on a computer. In this fun part of the lab, you will learn some new Terminal commands and get more comfortable navigating the files on your computer without using a mouse. Enter the learn-the-terminal directory within lab01 and examine its contents. (If lab01 is not your working directory, navigate to it using cd).

    cd learn-the-terminal
    ls

    Notice that now your working directory is learn-the-terminal. You can confirm this using the pwd command (“print working directory”). Note: pwd will show you the “absolute path” of your working directory, e.g. something like /home/ew5/cs134/lab01/learn-the-terminal/).

    pwd

    If at any point you want to return to lab01, you can do so by typing:

    cd ..

    This instructs the Terminal to change the working directory to be the parent of the current working directory. We can check that we’re back in the lab01 by typing pwd.

  2. The learn-the-terminal directory contains a file called scavengers.txt. As we’re about to embark on a small scavenger hunt, it’s probably a good idea to learn a bit more about scavengers. The file extension .txt indicates that the file just consists of so-called “plain text”. We can look at this text by using the cat command (cat is short for “concatenate”, but no need to understand why at this point):

    cat scavengers.txt
  3. Sometimes files can get very long, and thus it is sometimes helpful to look only at the first and last lines of a file using the head or tail commands:

    head -7 scavengers.txt
    tail -4 scavengers.txt

    The command head -7 instructs the Terminal to show us the first 7 lines of a file, while the command tail -4 instructs the Terminal to show us the last 4 lines of a file. We can choose any number of lines to display (not just 7 and 4).

  4. If we want to scroll through the file, we can use the less command. Try typing the following and see what happens:

    less scavengers.longer.txt

    Use the arrow keys and the space bar to navigate the file. When you’re finished, type q to return to the Terminal.

  5. There are several other helpful Terminal commands for finding out information about files. For instance, if we want to know the word count or the number of lines in a file, then we can use the wc command:

    wc -w scavengers.txt
    wc -l scavengers.txt

    These should respectively tell us that scavengers.txt has 92 words and 12 lines.

  6. Some files are not human-readable using cat, head, tail, or less. For instance, using these commands on image files like lizard.png produces something that looks like gibberish:

    head -3 lizard.png

    These commands are designed to work on plain text files, which is why they are so useful for this course: all of our programs are just code (text) saved inside plain text files!

  7. We can copy a file using cp and remove the original file using rm:

    cp scavengers.longer.txt longerscavengers.txt
    rm scavengers.longer.txt

    The cp command takes two arguments: the first argument is the name of the file that we want to copy (the source), and the second argument is the name of the new file that we want to create (the destination). To confirm that the file was successfully copied, type:

    cat longerscavengers.txt

    To confirm that the original file scavenger.longer.txt has been removed, type:

    ls

    It should no longer appear in the directory listing.

  8. Recall that we can run Python programs through the Terminal by typing python3 followed by the name of a Python program (ending with the extension .py). Here we run a Python program called backwards.py, which will ask us to type our name, then it will spell it backwards for us:

    python3 backwards.py
  9. Some Python programs require one (or more) “arguments”, which provide the program with additional information that it might require. For example, the Python program capslock.py requires two arguments: the name of a file that you want to convert to capital letters, and the number of lines you want to display:

    python3 capslock.py scavengers.txt 3
  10. Now let’s use our new Terminal skills on a scavenger hunt! We’ve compressed the files/directories for the scavenger hunt into a ZIP file called hunt.zip. To uncompress these files, use the unzip command:

    unzip hunt.zip

    The unzipped directory, called hunt, should now appear in our working directory. You can confirm this by typing ls.

  11. Time to enter the hunt directory and start the scavenger hunt!

    cd hunt
  12. Find the following, using the Terminal skills you have acquired during this lab (Note: the point here is to exercise your Terminal skills; you should be able to find all of the following without ever using the mouse!).

  13. Add the answers to these questions at the end of AboutMe.txt. Remember to commit and push your changes.

Submit Your Assignment!

  1. Verify that the current versions of all of your files are visible on evolene.
  2. Download a .zip archive of your work. Download your assignment files for submission by going to your lab repository on Gitlab, selecting the Download source code icon (a down arrow), and select zip. Doing so should downloaded all of your lab files into a single zip archive, and placed it inside your Downloads folder (or to whichever folder is set as your browser’s default download location).
  3. Submit your work. Navigate to the CS134 course on Gradescope. On your Dashboard, select the appropriate Lab Assignment (Lab 1 should be the only assignment that you see). Drag and Drop your downloaded zip archive of the Lab Assignment from the previous step, and select ‘Upload’.