Instructions for Python-Based Homework Assignments

Fall 2015

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 + "!")

You can copy and paste that into IDLE and it will tell you perhaps the most important thing you'll learn in college.

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 normally, highlight the parts you want to comment out, then select Format > Comment Out Region to comment out all the stuff for you. Note that this option appears only once you have opened a new window. Also, for readability's sake, it's good style to break down large paragraphs of comments into multiple lines since IDLE doesn't automatically wrap lines for you.

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 that IDLE shows you to indicate that you can type code in 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.

Example Handin

The following is a snippet of a properly-formatted (but farcically incorrect) handin file for HW2-1:

# CSCI 0931 HW 2-1
# Steve Gomez
# 2014-02-26

# Task 1
mylist = [4, 18, 13, 7]

# Question 1.1
# I didn't get the answers I was expecting.  Could've sworn it was going to
# say "abra" and "cadabra".  Maybe IDLE is set up wrong on this machine?

# Question 1.2
mylist[3] + mylist[3] + mylist[3]
# D'oh!  Guess I made an off-by-one error.

# Question 1.3

# ... [skipping ahead a couple questions] ...

# Task 2

# Question 2.1
# The variable "answer" represents the number of stuffed animals named
# "bonkers".  That's an awesome name.

# Question 2.2
# Mine doesn't evaluate to 0, it's "abracadabra", which seems fine to me.  If
# I had to change it, though, I'd make it look like this...
answer = "the correct value"

# Question 2.3
# I gave up decimals for Lent, so I think I'll just leave it the way it is.

# ... [and so on, with a little less snark toward the graders] ...