Working with Numbers


So far, you've been using strings to represent text. That's just one type of value. Computers let you represent information in other ways, too. One of the most basic but most important ways is as numbers. Numbers are used in almost every program. Whether you're writing a space shooter game or home finance package, you need to represent numbers some way. You've got high scores or checking account balances to work with, after all. Fortunately, Python has several different types of numbers to fit all of your game or application programming needs.

Introducing the Word Problems Program

This next program uses those dreaded word problems. You know, the kind that always seems to involve two trains leaving different cities at the same time headed in opposite directions. . . bringing back nightmares of junior high algebra as they're about to collide. Well, fear not. You won't have to solve a single word problem, or even do any math at all. I promise. The computer will do all the work. All you have to do is press the Enter key. The Word Problems program is just an amusing (hopefully) way to explore working with numbers. Check out Figure 2.5 to see a sample run.

click to expand
Figure 2.5: With Python, you can add, subtract, multiply, divide, and keep track of a pregnant hippo's weight.

The following is the source code for the program:

 # Word Problems # Demonstrates numbers and math # Michael Dawson 1/12/03 print \ """ If a pregnant hippo, weighing 2,000 pounds, gives birth to a 100 pound calf, but then eats 50 pounds of food, how much does she weigh?""" raw_input("Press the enter key to find out.") print "2000 - 100 + 50 = ", print 2000 - 100 + 50 print \ """ If an adventurer returns from a successful quest and buys each of 6 companions 3 bottles of ale, how many bottles does the adventurer buy?""" raw_input("Press the enter key to find out.") print "6 * 3 = ", print 6 * 3 print \ """ If a kid has 24 pieces of Halloween candy and eats 6 pieces a day, how many days will the stash last?""" raw_input("Press the enter key to find out.") print "24 / 6 = ", print 24 / 6 print \ """ If a group of 4 pirates finds a chest full of 107 gold coins, and they divide the booty evenly, how many coins will be left over?""" raw_input("Press the enter key to find out.") print "107 % 4 = ", print 107 % 4 print \ """ If a restaurant check comes to 19 dollars with tip, and you and your friends split it evenly 4 ways, how much do you each throw in?""" raw_input("Press the enter key to find out.") print "19 / 4 = ", print 19 / 4 print "WRONG!" raw_input("Press the enter key for the right answer.") print 19.0 / 4 raw_input("\n\nPress the enter key to exit.") 

Understanding Numeric Types

The program Word Problems uses numbers. That's obvious. But what may not be obvious is that it uses two different types of numbers. Python allows programmers to use several different types of numbers. The two types used in this program, and probably the most common, are integers and floating-point numbers (or floats). Integers are whole numbers—numbers with no fractional part. Or, another way to think about them is that they can be written without a decimal point. The numbers 1, 27, -100, and 0 are all examples of integers. Floats are numbers with a decimal point, like 2.376, -99.1, and 1.0.

You might be thinking, "Numbers are numbers. What's the big deal?" But integers and floats can act a little differently under special circumstances, as you'll see.

Using Mathematical Operators

With mathematical operators, you can turn your computer into an expensive calculator. The operators should look pretty familiar. For example, the following line

 print 2000 - 100 + 50 

subtracts 100 from 2000 and then adds 50 before printing the result of 1950. Technically, it evaluates the expression 2000 – 100 + 50, which evaluates to 1950. An expression is just a sequence of values, joined by operators, that can be simplified to another value.

The line

 print 6 * 3 

multiplies 6 by 3 and prints the result of 18.

The line

 print 24 / 6 

divides 24 by 6 and prints the result of 4.

Pretty standard stuff. But check out the next calculation:

 print 107 % 4 

Okay, using % as a mathematical operator is probably new to you. Used here, the symbol % stands for modulus, which is just a fancy way of saying, "give me the remainder." So 107 % 4 evaluates to the remainder of 107 / 4, which is 3.

The next calculation might also make you scratch your head. The following line produces a result of 4:

 print 19 / 4 

But if each person puts 4 dollars in, that's a total of only 16, not 19. And that leaves the waitress short 3 bucks. What happened? Well, when Python performs integer division (where all the numbers involved are integers), the result is always an integer. So, any fractional part is ignored. If you want floating-point division, or what some people call true division, then at least one of your numbers must be a floating-point number. The following line results in true division:

 print 19.0 / 4 

This line prints the expected 4.75. Now you've done true division and made your waitress happy.

start sidebar
IN THE REAL WORLD

Python is an evolving language. There's a highly open process for discussing potential changes and improvements. In fact, there's a list of every proposed enhancement at http://www.python.org/peps/. One change that is definitely on its way is the end of integer division. Starting in Python 3.0, all division will be true division. So, beginning in that release, 3 / 4 will be .75 and not 0.

end sidebar

Table 2.2 summarizes mathematical operators for integers, while Table 2.3 summarizes mathematical operators for floating-point numbers. Take a close look at the results of the division operator in each table.

Table 2.2: MATHEMATICAL OPERATORS WITH INTEGERS

Operator

Description

Example

Evaluates To

*

Multiplication

7 * 3

21

/

Division

7 / 3

2

%

Modulus

7 % 3

1

+

Addition

7 + 3

10

-

Subtraction

7 - 3

4

Table 2.3: MATHEMATICAL OPERATORS WITH FLOATING-POINT NUMBERS

Operator

Description

Example

Evaluates To

*

Multiplication

7.0 * 3.0

21.0

/

Division

7.0 / 3.0

2.3333333333333335

%

Modulus

7.0 % 3.0

1.0

+

Addition

7.0 + 3.0

10.0

-

Subtraction

7.0 - 3.0

4.0

TRAP

Notice the division entry in Table 2.3. It says that 7.0 divided by 3.0 is 2.3333333333333335. While this is pretty accurate, it's not exact. Computers tend to round floating-point numbers. The results are fine for most purposes. But you should be aware of this when using floats.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net