HW1: Intro to Python

25 points; due Fri 4/4 @ 9am.

Goals

The goal of this assignment is to write your first few python programs, and to get started on the tools we use to write programs.

This is an independent assignment, which means that you must write the programs for this assignment on your own. You're free to talk with classmates, the prefect, or lab assistants, or to post on Piazza, but the work you submit must be your own.

Part 1: Your First (Non-Turtle) Python Program

Load up our in-browser Python editor; this is the thing that you used in HW0 to play with the turtle code. Now you're going to use it to write some programs of your own.

In the blank editor field (the big black box), start typing your program. For now, just type in this complicated program:

print "Hello, world!"

(Actually type it; don't just copy and paste!) After you've typed that in, run it by hitting ctrl + Enter. You should see the greeting message in the grey output box at the bottom of the page. If not, double-check your code carefully, and make sure that you've copied it over correctly. If you can't figure it out, ask a friend, prefect, or lab assistant, or post to the Piazza.

Exercise 1: Altering your program

Now, edit your program, so that the message that gets printed is “Hello, world! My name is [your name].”. (Don't actually make it print “[your name]”; make it print your own name, like “Sharla Smith” or whatever.)

Once you get this working, save it in the “hw1” directory, under your username, with the filename “hello”. This is the first part of your submission for this assignment. Make sure that you actually hit the “Save” button; you'll get a little confirmation message if it saves successfully. You can also double-check by opening your folder in the “Load” menus; you should see your program in there.

Part 2: Your Second Python Program

Now that you know how to create a simple Python program, let’s create one that’s a little more complicated. Refresh the editor page to get a new blank file, and then retype (not copy-and-paste) the following code into your editor:

# Record some info about the programmer.
name = "Jadrian Miles"   # Their name.
fave_int = 7             # Their favorite integer.

# Print out what we know about them.
print 'Hello,', name
squared_fave = fave_int * fave_int
print 'The square of your favorite integer is', squared_fave

Try running the program. You should get the following output:

Hello, Jadrian Miles
The square of your favorite integer is 49

Exercise 2: Altering your second program

Now modify this program so that it's about you: change the definitions of the name and fave_int variables so that when it runs, it greets you by name and prints out the square of your favorite integer. (You do have a favorite integer, don't you? You can't have 7 — clearly the best integer — I already claimed it!)

Once you have this program working, save it in the “hw1” folder, under your username, with the filename “programmer”. This will be a graded submission, so please double-check that you're putting it in the right folder and with the correct name.

Exercise 3:

Now continue modifying your program. On the line after the one that defines fave_int, define a new variable whose value is your birth year (give it a good, meaningful name!). On the line after that, define a new variable whose value is the current year. Try running your program; the things the program prints out should be exactly the same before.

One more change: make the program print a third line of output, so that the output is something like this:

Hello, Sharla Smith
The square of your favorite integer is 64
You are about 19 years old

Once you have this program working, save it in the “hw1” folder, under your username, with the filename “programmer_age”. Same deal as before: double-check that you're putting it in the right place!

Parting Notes

This assignment is worth 25 points. It is graded based on whether your two programs (“programmer” and “programmer_age”) produce appropriate output, and on the style of these programs: each variable should have a name that reflects the meaning of the value that it stores.

Start early, have fun, and ask questions!