Log in / Register
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

1.12 Integers and Real Numbers

In most programming languages, including Python, there is a distinction between integers and real numbers. Integers, given the type name int in Python, are written as a sequence of digits, like 83 for instance. Real numbers, called float in Python, are written with a decimal point as in 83.0. This distinction affects how the numbers are stored in memory and what type of value you will get as a result of some operations.

In Fig. 1.18 the type of the result is a float if either operand is a float unless noted otherwise in the table.

Dividing the integer 83 by 2 yields 41.5 if it is written 81/2. However, if it is written 83//2 then the result is 41. This goes back to long division as we first learned in elementary school. 83//2 is 41 with a remainder of 1. The result of floor division isn't always an int. 83//2.0 yields 41.0 so be careful. While floor division returns

an integer, it doesn't necessarily return an int.

We can insure a number is a float or an integer by writing float or int in front of the number. So, float(83)//2 also yields 41.0. Likewise, int(83.0)//2 yields 41.





x + y

x and y may be floats or ints.


x y

x and y may be floats or ints.


x * y

x and y may be floats or ints.


x / y

x and y may be floats or ints. The result is always a float.



x // y

x and y may be floats or ints. The result is the first

integer less than or equal to the quotient.

Remainder or


x % y

x and y must be ints.

This is the remainder of dividing x by y.


x ** y

x and y may be floats or ints.

This is the result of raising x to the yth power.




Converts the numeric value of x to a float.




Converts the numeric value of x to an int.

The decimal portion is truncated, not rounded.




Gives the absolute value of x.



Rounds the float, x, to the nearest whole

number. The result type is always an int.

Fig. 1.18 Numeric operations

There are infinitely many real numbers but only a finite number of floats that can be represented by a computer. For instance, the number PI is approximately 3.14159. However, that number can't be represented in some implementations of Python. Instead, that number is approximated as 3.1415899999999999 in at least one Python implementation. Writing 3.14159 in a Python program is valid, but it is still stored internally as the approximated value. This is not a limitation of Python. It is a limitation of computers in general. Computers can only approximate values when there are infinitely many possibilities because computers are finite machines.

You can use what is called integer conversion to transform a floating point number to its integer portion. In effect, integer conversion truncates the digits after the decimal point in a floating point number to get just the whole number part. To do this you write int in front of the floating point number you wish to convert. This does not convert the existing number. It creates a new number using only the integer portion of the floating point number.

Example 1.5 Assume that you work for the waste water treatment plant. Partof your job dictates that you report the gallons of water treated at the plant.However, your meter reports lbs of water treated. You have been told to toreport the amount of treated waste water in gallons and ounces. There are 128ounces in a gallon and 16 ounces in a pound. Here is a short program that performs the conversion.

1 lbs = f l o a t ( i n p u t ("Please enter the lbs of water treated:"))

2 ounces = lbs * 16

3 gallons = i n t (ounces / 128)

4 ounces = ounces - gallons * 128

5 p r i n t ("That 's",gallons ,"gallons and",

6 ounces ,"ounces of treated waste water.")

In Example 1.5 the lbs were first converted to ounces. Then the whole gallons were computed from the ounces by converting to an integer the result of dividing the ounces float by 128. On line 4 the remaining ounces were computed after taking out the number of ounces contained in the computed gallons.

Several of the operations between ints and floats are given in Fig. 1.18. If you need to round a float to the nearest integer (instead of truncating the fractional portion) you can use the round function. Absolute value is taken using abs. There are other operations between floats and ints that are not discussed in this chapter. A complete list of all operations supported by integers and floats are given in Chaps. 8 and 9. If you need to read some documentation about an operator you can use the appendices or you can search for Python documentation on the internet or you can start a Python shell and type help(float) or help(int). This help facility is built into the Python programming language. There is extensive documentation for every type within Python. Typing help(type) in the Python shell where type is any type within Python will provide you with all the operations that are available on that type of value.

Practice 1.10 Write a short program that computes the length of thehypotenuse of a right triangle given the two legs as pictured in Fig. 1.23 onp. 35. The program should use three variables, sideA, sideB, and sideC. ThePythagorean theorem states that the sum of the squares of the two legs ofthe triangle equals the square of the hypotenuse. Be sure to assign all threevariables their correct values and print the length of sideC at the end of theprogram. HINT: Raising a value to the 1/2 power is the same thing as findingthe square root. Try values 6 and 8 for sideA and sideB.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science