Lab 01

Lab 1: Python, the Terminal, and You!

Objectives:

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. Finally, we will gain some experience using git to check out and turn in our work.

Getting Started

  1. 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). Alternatively, you can use ⌘-space to open spotlight, type “code” into the search bar, and press enter.

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

  3. To open a 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 press enter. You can use either during the course. In both cases, you should see a “command 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:

The command cd stands for change directory, and it is used to navigate your command prompt from one folder to another. The tilde (~) is a shorthand for your home directory. The command mkdir creates a new directory (i.e., a folder) with the given name. By executing these two commands, you have navigated to your home folder and created a new folder inside it called cs134.

  1. Now descend into the cs134 directory you just created using the change directory (cd) command again:

  2. 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 whenever you access evolene.

  3. Clone the repository. To do this, 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:

    where 23xyz3 is a place holder for your CS username. The first time you do this on any machine, you will be prompted to enter your username and password. You should use your CS username and CS password. As you type your credentials, you may not see any indication that your password is being typed. This is a “security feature”, so do not be alarmed!

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 folders in our current working directory (the “working directory” is a term for whichever directory we currently happen to be in):

    You should see your newly cloned repository, the directory lab01.

  2. Now descend into the lab01 directory using the cd command, and then list its contents with ls:

You should see the files AboutMe.txt, GradeSheet.txt, README.md, hello.py, and the directory learn-the-terminal.

  1. Open the file like hello.py using VS Code. In VS Code, go to File menu and Open hello.py. This file is currently empty but we will populate it by writing your first python program.

  2. In VS Code, type the following Python command in the file hello.py:

    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, run the Python script by typing:

    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 the following lines in your Terminal:

    You should see the file hello.py after some text that says “Changes not staged for commit:”. That text also has a note about using “git add” to “update what will be committed”. Let’s do that!

    The commit command makes a snapshot of the state of your repository so you can preserve the current versions of all of the files that you care about. However the commit command will only store the changes of files you’ve add-ed.

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

    You might be asked for your CS password again. From now on, any time you use git pull to sync the contents of your 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 https://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.

  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:

    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.

  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.

  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 (e.g., -, *, /).

  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:

    This command starts an Interactive Python session. Notice the prompt >>> that shows up 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:

    Once you are done experimenting with different arithmetic operations, you can exit out of interactive python by typing exit() or pressing Ctrl+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:

    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. In other words, if you first typed:

    then the -a switch would have instructed git to commit any file that appeared in the list of files under the heading “Changes not staged for commit:”, but it would not have committed any files that appeared in the list of files under the heading “Untracked files:”. Git is not aware of any files that are currently untracked; you must git add them first!

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

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

  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.

    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.

Scavenger Hunt (Optional: 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 optional (but 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).

    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/).

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

    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):

  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:

    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:

    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:

    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:

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

    To confirm that the file was successfully copied, type:

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

    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:

  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:

  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:

    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!

  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.