< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204172034243133000097237097

Exercises

The exercises in this chapter may first be implemented as applications. Once full functionality and correctness have been verified, we recommend that the reader convert his or her code to functions which can be used in future exercises. On a related note, one style suggestion is to not use print statements in functions. Instead, have the functions return the appropriate value and have the caller perform any output desired. This keeps the code adaptable and reusable.

1:

Integers. Name the differences between Python's plain and long integers.

2:

Operators. (a) Create a function to take two numbers (any type) and output their sum.

(b) Write another function, but output the product of two given numbers.

3:

Standard Type Operators. Take test score input from the user and output letter grades according to the following grade scale/curve:

A: 90 – 100

B: 80 – 89

C: 70 – 79

D: 60 – 69

F: < 60

4:

Modulus. Determine whether a given year is a leap year, using the following formula: a leap year is one that is divisible by four, but not by one hundred, unless it is also divisible by four hundred. For example, 1992, 1996, and 2000 are leap years, but 1967 and 1900 are not. The next leap year falling on a century is 2400.

5:

Modulus. Calculate the number of basic American coins given a value less than 1 dollar. A penny is worth 1 cent, a nickel is worth 5 cents, a dime is worth 10 cents, and a quarter is worth 25 cents. It takes 100 cents to make 1 dollar. So given an amount less than 1 dollar (if using floats, convert to integers for this exercise), calculate the number of each type of coin necessary to achieve the amount, maximizing the number of larger denomination coins. For example, given $0.76, or 76 cents, the correct output would be "3 quarters and 1 penny." Output such as "76 pennies" and "2 quarters, 2 dimes, 1 nickel, and 1 penny" are not acceptable.

6:

Arithmetic. Create a calculator application. Write code that will take two numbers and an operator in the format: N1 OP N2, where N1 and N2 are floating point or integer values, and OP is one of the following: +, -, *, /, %, **, representing addition, subtraction, multiplication, division, modulus/remainder, and exponentiation, respectively, and displays the result of carrying out that operation on the input operands.

7:

Sales Tax. Take a monetary amount (i.e., floating point dollar amount [or whatever currency you use]), and determine a new amount figuring all the sales taxes you must pay where you live.

8:

Geometry. Calculate the area and volume of:

(a) squares and cubes

(b) circles and spheres

9:

Style. Answer the following numeric format questions:

(a) Why does 17 + 32 give you 49, but 017 + 32 give you 47 and 017 + 032 give you 41, as indicated in the examples below?

								
>>> 17 + 32
49
>>> 017+ 32
47
>>> 017 + 032
41

							

(b) Why does 56l + 78l give you 134L and not 1342, as indicated in the example below?

								
 >>> 56l + 78l
134L

							
10:

Conversion. Create a pair of functions to convert Fahrenheit to Celsius temperature values. C = (F - 32) * (5 / 9) should help you get started.

11:

Modulus. (a) Using loops and numeric operators, output all even numbers from 0 to 20.

(b) Same as part (a), but output all odd numbers up to 20.

(c) From parts (a) and (b), what is an easy way to tell the difference between even and odd numbers?

(d) Using part (c), write some code to determine if one number divides another. In your solution, ask the user for both numbers and have your function answer "yes" or "no" as to whether one number divides another by returning 1 or 0, respectively.

12:

Limits. Determine the largest and smallest ints, longs, floats, and complex numbers that your system can handle.

13:

Conversion. Write a function that will take a time period measured in hours and minutes and return the total time in minutes only.

14:

Bank account interest. Create a function to take an interest percentage rate for a bank account, say, a Certificate of Deposit (CD). Calculate and return the Annual Percentage Yield (APY) if the account balance was compounded daily.

15:

GCD and LCM. Determine the greatest common divisor and least common multiple of a pair of integers.

16:

Home finance. Take an opening balance and a monthly payment. Using a loop, determine remaining balances for succeeding months, including the final payment. "Payment 0" should just be the opening balance and schedule monthly payment amount. The output should be in a schedule format similar to the following (the numbers used in this example are for illustrative purposes only):

								
Enter opening balance: 100.00
Enter monthly payment: 16.13

         Amount  Remaining
Pymt#     Paid    Balance
-----    ------  ---------
  0      $ 0.00  $100.00
  1      $16.13  $ 83.87
  2      $16.13  $ 67.74
  3      $16.13  $ 51.61
  4      $16.13  $ 35.48
  5      $16.13  $ 19.35
  6      $16.13  $  3.22
  7      $ 3.22  $  0.00

							
17:

*Random numbers. Read up on the random module and do the following problem: Generate a list of a random number (1 < N <= 100) of random numbers (0 <= n <= 231 -1). Then randomly select a set of these numbers (1 <= N <= 100), sort them, and display this subset.


Last updated on 9/14/2001
Core Python Programming, © 2002 Prentice Hall PTR

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.