CIT 591 Python Warm-Up
Exercises
Fall 2014, 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.
- [Write a function] Write a function named
powers that, given a single integer as a parameter:
- Prints the number, the square of the number, and the cube of the number, on a single line.
- Returns as its result, a tuple of the three numbers just printed. (A tuple is a parenthesized list of the three numbers.)
You can try out your function by entering, for example, powers(25) in the Python Shell window.
- [Write a complete program] Using the
powers function, write a program that prints
out the numbers 1 through 10, along with their squares and cubes. (Just print the tuples, don't try for neater formatting.)