A202/A598
Spring 2007 Wed Jan 10 11:15-12:30
LH102 Lecture 2
- What is IDLE?
- What does raw_input(...) do?
- How does print work? What does print, (with a comma) do?
- You type ””” (three double quotes) at the
IDLE prompt, then hit Enter. What happens? You hit Enter two more times,
then type another set of three double quotes and hit Enter again. What
happens? Explain.
- What characters need to be protected in a string? How
do you protect them?
- What do + and * mean in the context of string expressions? Give
examples.
- Evaluate the following expressions:
2 + 3
2 / 3
2 * 3
“2” * 3
“2” + 3
“2” + “3”
2 * 3 / 6
3 / 6 * 2
- How do you find the length of a string?
- Write a short program that reads two integers from
the user and adds them up.
- What is casting?
- Illustrate the use of the string methods upper(), lower(), title(), replace().
- Write a program that reads a sentence and removes all
vowels in it.
- Write a program that reads two integers and prints
back the largest of the two. Did you use an if statement? Can you write the program without if
statements, just by using +,
-,
division by 2 and the abs(...) function?
- Write a program (called
Letter
) that translates a number between 0 and 4
into the closest letter grade. For example, the number 2.8 (which might
have been the average of several grades) would be converted to B-
. Break ties in favor of the
better grade; for example 2.85 should be a B
.
- How do you generate random integers in Python?
- What does range(...) do?
- Give an example of a list. How can you select random
elements from it? Does this work for strings too?
- Write a program that produces the first 20 multiples
of 3. Explain the program.
- How do you access individual characters in a string?
- Write a program that reads a string and prints back
its characters one per line.
- Write a program that reads a string and swaps the
first letter with the last.
- Write a program that reads a string, reverses it, and
prints that back.
- Write a program that reads a string and produces all
circular permutations of it; for example if the string read is pooh, the
program prints the following four strings: pooh, ooph, opho, and phoo.
- A bank account starts out with
$10,000
. Interest is compounded at
the end of every month at 6
percent per year (0.5
percent per month). At the beginning of every month, $500
is withdrawn to meet college
expenses after the interest has been credited. After how many years is the
account depleted?
- Now suppose the numbers (
$10,000
, 6
percent, $500
) were user-selectable. Are
there values for which the algorithm you developed would not terminate? If
so, make sure it always terminates.
- Write a program that chooses a random integer between
0 and 100 and lets the user guess it. Each user entry is given feedback
(if the user should try higher, lower, or if the guess is right). If the
user guesses the number in up to 10 tries the program ends, and the user
wins. Otherwise the user loses after the tenth wrong guess and the program
reports that too.