CIT 590 Python Warm-Up
Exercises
Spring 2010, David Matuszek
- [Get started] Log in. Start IDLE. Compute 2+2.
- [Write a program] From IDLE, open a new window (File
-> New Window, or ctrl-N). Write a program to compute and
print the result of adding 2 and 2. Save this program (File -> Save or ctrl-S) on a
file named
test.py, then run it (Run -> Run Module, or F5).
- [Edit a program] Edit your program to compute and print the result of 13
squared. (You can use
13 * 13, or 13 ** 2 to
compute this.) Run the program.
if IDLE asked you to save the program before
running it, do this:
Options -> Configure IDLE... ->
General -> At Start of Run (F5) -> No Prompt.
Change the program to compute 14
squared, and run it again.
else:
pass # do nothing
- [Use an if statement] Write a program to ask the user for a number, then
print "number
is
even" if
the number is even, or "number
is odd" if the number is odd. (Print the actual number instead
of number.) A
number is even if the remainder (%), when divided by two,
is zero. Save your program on a new file with any name you choose (but
it must have a .py extension) and run it.
- [Use a while loop] Write a program to repeatedly do the following: (1)
Ask for a number, and (2) print out a message that says what number you
entered, and the square of that number (for example,
5 squared is
25). Quit
when the number 0 is entered.
- [Use a for loop] Write a program to print out the numbers 1 through 25
and, for each number, print its square and its cube on the same line.
- [The "Buzz" program.] Write a program that prints
out the numbers 1 through 100, except that, if the number contains
a 7 or is divisible by 7, (or both), print the word
buzz instead.
Here are some programming hints:
- A number is divisible by 7 if the remainder, when divided by 7,
is zero.
- A number ends in a 7 if the remainder, when divided by 10, is 7.
- A two-digit number has a 7 as its first digit if the result of
dividing by 10 is 7.
- The (three digit) number 100 doesn't contain a 7, and that's as
high as you need to go.
- Remember the
if...elif...elif...else kind of statement.