Week 1, Problem 1: Data!
[25 points; individual or pair]

 

Part 0: Logging in and running Python and IDLE

An easy way to start Python and IDLE is by clicking on the IDLE icon in the "dock" at the bottom of the screen. It's the one with the stylized yellow and blue pythons on it. If you can’t find it, click the Windows start button shown below:

windows-keyboard.jpg

and click on IDLE for Python. After that, open a new file and add a suitable comment at the top—for example:

# Homework 1, Problem 1 (Lab)

# 04/07/2012

 

Save your file as hw1pr1.py - perhaps on the desktop (easy to find) or in a new folder (better organized).

 

Part 1: working at the Python interpreter (or shell)

Arithmetic with numbers, lists, strings, and booleans.

To get started, try a few arithmetic, string, and list expressions in the Python interpreter, e.g.,

>>> 40 + 2
42

>>> 40 ** 2
1600

>>> 40 % 2
0

>>> 'hi there!'
hi there!   (notice Python's politeness!)

>>> 'who are you?'
who are you?   (though sometimes it's a bit touchy.)

>>> L = [0,1,2,3]   You can label data (here, a list) with a name (here, the name L)
(no response from Python)

>>> L
[0,1,2,3]   You can see the data (here, a list) referred to by a name (here, L)

>>> L[1:]
[1,2,3]   You can slice lists (here, using the name L)

>>> L[::-1]
[3,2,1,0]   You can reverse lists (or strings!) using "skip"-slicing with a -1 as the amount to skip.

>>> [0,1,2,3][1:]
[1,2,3]   You can slice lists using the raw list instead of the name (Not that this would be very useful, admittedly!)

>>> 100*L + [42]*100
(a list with 500 elements that I'm too lazy to type here)

>>> L = 42   You can reassign the name L to another value, even of a different type- now, L names the integer 42, instead of the list it used to represent.
(no response from Python)

>>> L == 42   Two equals are different than 1! This tests for equality.
True

>>> L != 42   This tests for "not equal."
False

Errors or Exceptions

If you didn't type things in perfectly, Python will reply with an error or an exception, as it is often called. See if you can make Python create the following exceptions, but don't spend more than a minute or so in total!

SyntaxError  
TypeError  (try slicing an integer for example!)
ZeroDivisionError  
IndexError  (try an out-of-bounds index)
OverflowError  (remember that integers will crash the machine before overflowing)

 

 

Part 2: Lists of integers

This problem will exercise your slicing-and-indexing skills. First, you should copy (or type) into your hw1pr1.py file the following lines:

# starting lists for part 2

pi = [3,1,4,1,5,9]

e = [2,1,7]

 

When you hit F5 (or click Run and then click Run Module F5), these two lists will be recognized by the Python shell.

The challenge is to create several lists using only the list labeled pi, the list labeled e, and the four list operations here --

list indexing pi[0]

list slicing e[1:]

list concatenation, + pi[:1] + e[1:]     (don't use + to add values numerically)

the list-making operator, [ , ] [ e[2], e[0] ]

For each one, place your answer into an appropriate variable in your hw1pr1.py file (see example below). Include a comment on the line above, as well. Though not mandatory, you might try to use as few operations as possible, to keep your answers elegant and efficient. For example,

Example problem Use pi and/or e to create the list [2,5,9]. Store this list in the variable answer0.

Answer to the example problem

# Creating the list [2,5,9]

answer0 = [e[0]] + pi[-2:]

 

Please leave a blank line or two between your answers (to keep the graders happy)!
Remember that you can use the python interpreter to try things out!

Here are the problems:

Use pi and/or e to create the list [7,1]. Store this list in the variable answer1.

Use pi and/or e to create the list [9,1,1]. Store this list in the variable answer2.

Use pi and/or e to create the list [1,9,5,1,4]. Store this list in the variable answer3.

Use pi and/or e to create the list [1,2,3,4,5,5,4,3,2,1]. Store this list in the variable answer4.

 

Part 3: Strings

This problem continues in the style of the last one, but uses strings rather than lists. So, it asks you to create specified strings that result from using only the following three string literals, which you should type into your hw1pr1.py file at this point:

n = 'northwestern'

u = 'university'

c = 'class_eecs_110_python'

 

You may use any combination of these four string operations:

String indexing, e.g., n[0]

String slicing, e.g., u[1:]

String concatenation, +, e.g., c + n

Repetition, *, e.g., 42*c (using integers is OK here)

Again, less is more: the number of operations in our shortest answers are in parentheses- you might find even more efficient ones! However, any answer is OK - there's no requirement to use fewer operations.

Example problem: Use n and c to create 'nano'. Store this string in the variable answer42.

Answer to example

# Creating the string 'nano'

answer42 = n[0] + c[2] + n[0] + c[-2]

 

Create northeasternuniversity  Store this string in the variable answer5.

Create east_river Store this string in the variable answer6.

Create nirvana Store this string in the variable answer7.

Create easterparty_110110 Store this string in the variable answer8.

Create we_have_clinteast_in_eecs110 Store this string in the variable answer9.

If you have gotten to this point, you have completed the first problem from Lab 1! You should submit your hw1pr1.py file at the Canvas.

Next

hw1pr2

Lab 1

Homework 1