Instructions for Python-Based Homework Assignments

Adapted from Brown University CSCI0931. Used with Permission.

Python Code

A program written in the Python language (henceforth known as a "Python program") is just a plain text file, often called the "source file" for that program. The only thing that's really different about it is that it has a ".py" extension rather than a ".txt", and of course that the text inside the source file is written in the Python language.

The text that indicates instructions for a program to follow is called "code". For example, here's a little bit of Python code:

question = "How much wood could a woodchuck chuck"
answer = "Ten small logs per hour, of course"
print(question + "?")
print(answer + "!")

Open up a new file in IDLE and paste that in. Save the file (call it woodchuck.py. Now try to run it.

Python Comments

The Python language, like most other programming languages, also lets you include text in a program's source file that is not code. Text like this is called a "comment", and it can be whatever you want. A comment is different from code in that it doesn't include instructions for the program to follow. Python indicates comments by starting them with the # symbol. So we can include a comment to explain what's going on at any point in the code:

# This program addresses the big questions in life.
question = "How much wood could a woodchuck chuck"
answer = "Ten small logs per hour, of course"
# Proper punctuation is essential.
print(question + "?")  # Plus sign concatenates.
print(answer + "!")

Notice that anywhere you find a # symbol, everything from there to the end of the line is a comment. IDLE changes the color of all comments to red, so they're easy to tell apart from regular code.

Note: If you're going to have large blocks of comments in your code, rather than have to type # before every comment, you can type it out like normal, highlight the parts you want to comment out, then select Format > Comment Out Region to comment out all the stuff for you. Also, since IDLE doesn't automatically wrap lines for you, it makes sense to break up long lines of comments into multiple lines as it's easier to read.

So What?

What all this means for your homework assignments is that you can mix together your answers that are written in Python code and those that are written in regular prose, which you would write as comments.

To start writing down your answers, open IDLE, then go to File > New Window, or hit Ctrl + N. This brings up an empty window. Enter your code and comments in this window, exactly as you'd like to pass them in.

You can copy and paste between the IDLE interactive command line and the window in which you're editing your homework handin file. Remember that the >>> at the beginning of each input line in IDLE is just a special symbol (the prompt that IDLE shows you to indicate that it is waiting for you to type in code on that line. It is not itself part of the Python code! Don't include any >>> s in your handin file.

For any question that requires an answer written in English prose rather than Python code, write it as a comment in your handin file. Also make sure to include comments that indicate which question is being answered by each block of comments and/or code.

Handing in

When you are ready to hand in, go to drive.google.com and click on New... File Upload. A window will pop up and you can choose which file you want to upload. When the upload is done, clicking Locate will show you the position in your Google Drive that your file got uploaded into. You can then move it to a subfolder, or whatever.

If you click on the uploaded file, you will get a dark window and Google will say "No Preview Available". This is because Google's built-in apps do not handle files with a .py extension. Since you (presumably) uploaded a working file that you tested out on your own computer, you do not need to edit it on Google and so you can go directly to handing it in, which we will talk about in the next paragraph. However, if you really would like to view or edit your file on Google drive, you can link in one of the suggested third-party apps. These are apps that were not written by Google, but they work with Google Drive. I use "Drive Notepad" myself, but you can try others.

When you are ready to hand in your assignment, you can go to Share by right-clicking on the name of the file in My Drive. You can then share it with us in the usual way.