|
CS 140: Introduction
to Computer Programming
|
Arithmetic
Performing numerical computations is one of
those things that computers do well.
Python is capable of some pretty impressive
feats of arithmetic. For instance, it will
tell you without a moment's hesitation that 2
multiplied by itself 1000 times is exactly
10715086071862673209484250490600018105614048117055336074437503
88370351051124936122493198378815695858127594672917553146825187
14528569231404359845775746985748039345677748242309854210746050
62371141877954182153046474983581941267398767559165543946077062
914571196477686542167660429831652624386837205668069376L,
a 302-digit number! By the way, you can
just ignore the L at the end; it stands for
"long integer." (Really.)
Recall that there are two different numerical
data types: integers and floats.
Arithmetic with floats (decimal numbers) works
exactly the way you would expect. There
are occasionally issues with round-off error,
but we won't need to worry about this in our
course. Just remember to put a decimal
point in a number (as in 5.0) if you want to
work with decimals.
We'll take a look at integer arithmetic a bit
more carefully. Addition, subtraction, and
multiplication of integers hold no
surprises. Thus the program
a = 17
b = 26
print "The sum of", a, "and", b, "is", a+b
print "The difference",
a, "minus", b,
"is", a-b
print "The product of",
a, "and", b, "is", a*b
gives as its output
The sum of 17 and
26 is 43
The difference 17 minus 26 is -9
The product of 17 and 26 is 442
But integer division ignores anything past the
decimal point, which is only reasonable.
Thus Python would compute 42/6
as 7, but would reduce 17/3
to 5, and for 9/10
gives 0. (Actually,
there's some fine print for negatives—Python
actually rounds down, so it would reduce -17/3
to -6 rather than -5.
We'll probably avoid these sorts of issues,
though.) Note that if you actually want to
compute 5/6 as a decimal,
you'll need to type 5.0/6,
to which Python responds 0.8333333333333334.
(In case you're wondering why there's a 4
at the end rather than a 3,
this is an example of the round-off error
mentioned above.)
Python does supply a means for finding the
remainder when dividing; we use the %
(percent) symbol. Thus 42%6,
17%3, and 9%10
will evaluate as 0, 2,
and 9, respectively.
(The mathematically inclined may wish to verify
that -17%3 is equal to 1.
But you don't need to worry about this.)
The % command comes in
handy now and again. For instance, here is
a program that checks whether one number is a
multiple of another.
a = input("Enter a positive integer:
")
b = input("Enter a smaller positive
integer: ")
remainder = a%b
if
remainder == 0:
print "How about that",
a, "is a multiple of",
b
else:
print "I'm afraid that",
a, "is not a multiple
of", b
The last arithmetic operation that we will
mention here is exponentiation; that is, raising
a number to a power. To compute 38
squared, for instance one could type 38**2.
The double star means "second power."
Python can also compute 3 to the tenth power via
3**10, which yields
59049. To obtain the 302-digit number at
the top of this page I typed 2**1000.
|
|
|