Lab 1: Introduction to the Python/git workflow

Objectives

  • Set up your computer for the CS 134 labs.

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

Normally, we would all be working on our lab computers, which have been configured for you all to use. Since we’re remote, we’ll start the lab by setting up your own computer. You will need a laptop or desktop to do this. Other devices, like iPads, won’t be sufficient.

Set Up Your Computer

You will need to complete this set up before continuing. If you have trouble, please talk to the TAs or instructor at the beginning of your lab period.

Hello and Goodbye

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

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

  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 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’ve add-ed.

  5. 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, 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 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 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.

  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.

    num1 = input("Enter a number: ")
    num2 = input("Enter another number: ")
    
  9. Now let us 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. To enter interactive python, type python3 in the Terminal at the $ prompt:

    python3
    

    This command starts an interactive python session. In the terminal, you’ll see the prompt >>>. This is the interactive python3 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 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.

  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.

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

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