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.
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.
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.
The following is a snippet of a properly-formatted (but farcically incorrect) handin file for HW2-1:
# CSCI 0931 HW 2-1 # Jadrian Miles # 2013-02-23 ### # Task 1 mylist = [4, 18, 13, 7] # Question 1.1 mylist[1] mylist[2] # 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] ...