|
First Summer 2008 |
The best approach to this lecture is: taking the Python textbook along for a ride, in front of your computer.
In the beginning we should be using the IDLE (IDE) interface as much as possible.
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 Pages 30-33 discusses the numeric operators we start with in this class.
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 recall how on Tuesday we said 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.
So now 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.
We then asked for ways in which we could swap the values of two variables.
One way was to use a third variable:
n = 3 m = 5 k = n n = m m = k
The other one was more arcane, clever, and also number specific:
n = 3 m = 5 m = n + m n = m - n m = m - n
Was this working for negative values too?
The time dimension was important here in proving that this is indeed functioning correctly.
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.
Another type that we need to study refers to strings.
Chapter 2 starts with strings, pages 18-28.
Expressions, involving strings:
"straw" + "berry"
"alf" * 2 + "a"
Strings are more complex than numbers, and that's where the analogy with Alice is stronger.
Useful string methods are listed on page 41:
upper() lower() swapcase() capitalize() title() strip() replace(old, new, [max])
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.
Conversion functions are listed on page 46.
We will soon discuss functions, to match the development of the queen problem in Alice.
Meanwhile 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
Here's how your program might work:
frilled.cs.indiana.edu%java Two Please enter your first integer number, then press Enter. 3 Please enter your second integer number, then press Enter. 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:
Compute the difference, and compute the
that the customer should receive in return.
Here's how your program might work:
You, of course, have to write a Python program.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%
All these programs will be covered in class, together.
The only think not covered here is: print. But I think you can get the information from the text.