  Spring Semester 2007

Lab One: First Python Tutorial and Lab Assignment Here's a picture of Nat Hayes (Metal) who will be teaching the first lab (12:45-2:40pm, SE045).

Thanks also to Alexandra Botsford Michael Cox Doug Jessee Chadwick Jones Mike Jones Danny Marsh

and Aaron Ofengender for their help in lecture Wed 01/10.

Today's lab will be a tutorial along the lines of the list of questions distributed in lecture yesterday.

Your first lab assignment is the problem listed in Lecture Notes Two.

It is due in Oncourse next Thursday at the end of the day.

Here's the Python text used in A201 (here's the text's home page).

Our tutorial starts with IDLE.

1. Make sure you know how to start IDLE.

2. At the prompt type some expressions and try to explain the results.

The IDLE is especially good for evaluating expressions.

Some of the simplest expressions are numbers.

Numbers are of two different types: with and without a fractional part.

We start by looking at integers first.

So we evaluate simple expressions:

`1`
`2`
`5 - 3`
Expressions can use parens.

Compare:

`2 * 3 + 4` with `2 * (3 + 4)`
`2 - 3 + 4` with `2 - (3 + 4)`

Some operators might surprise us.

Evaluate:

`2 / 3`
`4 * 2 / 3`
`4 * 2 / 3`
`2 / 3 * 4`
`2 * (3 / 4)`

Now you may recall how in A201 you may have been told that Python, like Alice, offers a virtual world.

The world that Python offers is that of a names, for variables that could hold your values.

So instead of putting a chicken on the stage we assign a value to a variable.

Thus we need to discuss assignment statements such as these:

```n = 5
n = n + 1```

You will understand that the equals sign is an assignment operator, so information travels right to left.

This is a way to increment a variable by one.

3. Let's now discuss this expression:

`2 + abs(-3)`

It should be clear what `abs()` does, it receive an argument and it returns a value.

4. Another type that we need to study refers to strings.

Expressions, involving strings:

`"straw" + "berry"`
(concatenation)

`"alf" * 2 + "a"`
(replication and concatenation)

Strings are more complex than numbers, and that's where the analogy with Alice is stronger.

Useful string methods:

`upper()`
`lower()`
`swapcase()`
`capitalize()`
`title()`
`strip()`
`replace(old, new, [max])`

5. The function `raw_input( )` must be discussed by analogy with `abs()` mentioned earlier.

It produces strings, which need to be converted if they are to be used as numbers.

Now let's work out some problems:

Problem One Write a program that produces this pattern:

```     4
4
4
4   4
44444444
4
4```
Problem Two Do the same with a pattern made out of double quotes.
```     "
"
"
"   "
""""""""
"
"```
Problem Three What would it take to write a program that produces this patern:
```          .8.           8 888888888o          ,o888888o.
.888.          8 8888    `88.       8888     `88.
:88888.         8 8888     `88    ,8 8888       `8.
. `88888.        8 8888     ,88    88 8888
.8. `88888.       8 8888.   ,88'    88 8888
.8`8. `88888.      8 8888888888      88 8888
.8' `8. `88888.     8 8888    `88.    88 8888
.8'   `8. `88888.    8 8888      88    `8 8888       .8'
.888888888. `88888.   8 8888    ,88'       8888     ,88'
.8'       `8. `88888.  8 888888888P          `8888888P'```
Problem Four Write a program that asks the user for the lengths of the sides of a rectangle.

Then calculate and print the area of the triangle using Heron's formula.

(How do you know the three sides form a triangle?).

Problem Five Write an Eggy-Peggy translator: given a string, any string, the translator converts it to a new string by placing `egg` in front of every vowel. Problem Six Write a program that prompts the user for two integers and then prints

• The sum
• The difference
• The product
• The average
• The distance (absolute value of the difference)
• The maximum (the larger of the two )
• The minimum (the smaller of the two)

Here's how your program might work:

```frilled.cs.indiana.edu%java Two
3
6
3 + 6 = 9
3 - 6 = -3
3 * 6 = 18
avg(3, 6) = 4.5
dist(3, 6) = 3
max(3, 6) = 6
min(3, 6) = 3
frilled.cs.indiana.edu%```

You, of course, have to write a Python program.

Problem Seven (Giving change) Implement a program that directs a cashier how to give change.

The program has two inputs:

• the amount due and
• the amount received from the customer

Compute the difference, and compute the

• dollars,
• quarters,
• dimes,
• nickels, and
• pennies

that the customer should receive in return.

Here's how your program might work:

```frilled.cs.indiana.edu%java Six
Type the amount due then press enter.
3.72
Type the amount received then press enter.
5
Give 1.28 in change as follows:
5 quarters
0 dimes
0 nickels
3 cents
frilled.cs.indiana.edu%java Six
Type the amount due then press enter.

0.08
Type the amount received then press enter.

0.5
Give 0.42 in change as follows:
1 quarters
1 dimes
1 nickels
2 cents
frilled.cs.indiana.edu%```

A few more notes will be added today, also the source code for the problems above.

Updated by Adrian German for A202/A598