• No results found

Numeric Data Types

In document Python Programming for Starters (Page 54-58)

Computing with Numbers

3.1 Numeric Data Types

When computers were first developed, they were seen primarily as number crunchers, and that is still an important application. As you have seen, problems that involve mathematical formulas are easy to translate into Python programs. In this chapter, we’ll take a closer look at programs designed to perform numerical calculations.

The information that is stored and manipulated by computer programs is generically referred to as data. Different kinds of data will be stored and manipulated in different ways. Consider this program to calculate the value of loose change:

# change.py

# A program to calculate the value of some change in dollars def main():

print("Change Counter") print()

print("Please enter the count of each coin type.") 45

46 Chapter 3. Computing with Numbers quarters = eval(input("Quarters: "))

dimes = eval(input("Dimes: ")) nickels = eval(input("Nickels: ")) pennies = eval(input("Pennies: "))

total = quarters * .25 + dimes * .10 + nickels * .05 + pennies * .01 print()

print("The total value of your change is", total) main()

Here is an example of the output.

Change Counter

Please enter the count of each coin type.

Quarters: 5 Dimes: 3 Nickels: 4 Pennies: 6

The total value of your change is 1.81

This program actually manipulates two different kinds of numbers. The values entered by the user (5, 3, 4, 6) are whole numbers; they don’t have any fractional part. The values of the coins (.25, .10, .05, .01) are decimal representations of fractions. Inside the computer, whole numbers and numbers that have fractional components are stored differently. Technically, we say that these are two different data types.

The data type of an object determines what values it can have and what operations can be performed on it. Whole numbers are represented using the integer data type (int for short). Values of type int can be positive or negative whole numbers. Numbers that can have fractional parts are represented as floating point (or float) values. So how do we tell whether a number is an int or a float? A numeric literal that does not contain a decimal point produces an int value, but a literal that has a decimal point is represented by a float (even if the fractional part is 0).

Python provides a special function called type that tells us the data type (or “class”) of any value. Here is an interaction with the Python interpreter showing the difference between int and float literals:

>>> type(3)

<class ’int’>

>>> type(3.14)

<class ’float’>

>>> type(3.0)

<class ’float’>

>>> myInt = -32

3.1. Numeric Data Types 47

>>> type(myInt)

<class ’int’>

>>> myFloat = 32.0

>>> type(myFloat)

<class ’float’>

You may be wondering why there are two different data types for numbers. One reason has to do with program style. Values that represent counts can’t be fractional; we can’t have312 quarters, for example. Using an int value tells the reader of a program that the value can’t be a fraction.

Another reason has to do with the efficiency of various operations. The underlying algorithms that perform computer arithmetic are simpler, and therefore faster, for ints than the more general algorithms required for float values.

You should be warned that the float type only stores approximations to real numbers. There is a limit to the precision, or accuracy, of the stored values. Since float values are not exact, while ints always are, your general rule of thumb should be: If you don’t need fractional values, use an int.

operator operation

+ addition

subtraction

multiplication / float division

∗∗ exponentiation abs() absolute value // integer division

% remainder

Table 3.1: Python built-in numeric operations.

A value’s data type determines what operations can be used on it. As we have seen, Python supports the usual mathematical operations on numbers. Table 3.1 summarizes these operations.

Actually, this table is somewhat misleading since the two numeric data types have their own opera-tions. For example, I have listed a single addition operation, but keep in mind that when addition is performed on floats, the computer hardware performs a floating point addition, whereas with ints the computer performs an integer addition. Python chooses the appropriate underlying operation (int or float) based on the operands.

Consider the following interaction with Python:

>>> 3 + 4 7

>>> 3.0 + 4.0 7.0

>>> 3 * 4 12

48 Chapter 3. Computing with Numbers

For the most part, operations on floats produce floats, and operations on ints produce ints. Most of the time, we don’t even worry about what type of operation is being performed; for example, integer addition produces pretty much the same result as floating point addition, and we can rely on Python to do the right thing.

In the case of division, however, things get a bit more interesting. As the table shows, Python (as of version 3.0) provides two different operators for division. The usual symbol / is used for

“regular” division and a double slash // is used to indicate integer division. The best way to get a handle on the difference between these two is to try them out.

>>> 10 / 3

Notice that the / operator always returns a float. Regular division often produces a fractional result, even though the operands may be ints. Python accommodates this by always returning a floating point number. Are you surprised that the result of 10/3 has a 5 at the very end? Remember, floating point values are always approximations. This value is as close as Python can get when representing 313 as a floating point number.

In document Python Programming for Starters (Page 54-58)