Type Conversions

Sometimes values of one data type need to be converted into another. You have seen that combining an int with an int produces an int, and combining a float with a float creates another float. But what happens if we write an expression that mixes an int with a float? For example, what should the value of x be after this assignment statement?

If this is floating point division, then the result should be the float value 2.5. If integer division is performed, the result is 2. Before reading ahead for the answer, take a minute to consider how you think Python should handle this situation.

In order to make sense of the expression 5.0 / 2, Python must either change 5.0 to 5 and perform integer division or convert 2 to 2.0 and perform floating point division. In general, converting a float to an int is a dangerous step, because some information (the fractional part) will be lost. On the other hand, an int can be safely turned into a float just by adding a fractional part of .0. So, in mixed-typed expressions, Python will automatically convert ints to floats and perform floating point operations to produce a float result.

Sometimes we may want to perform a type conversion ourselves. This is called an explicit type conversion. For example, suppose we are writing a program that finds the average of some numbers. Our program would first sum up the numbers and then divide by n, the count of how many numbers there are. The line of code to compute the average might look like this.

Unfortunately, this line may not always produce the result we intend.

Consider a specific example. The numbers to be averaged are the ints 4, 5, 6, 7. The sum variable will hold 22, also an int, and dividing by 4 gives the answer 5, not 5.5. Remember, an int divided by an int always produces an int.

To solve this problem, we need to tell Python to convert one of the operands to a floating point value. average = float(sum) / n

The float() function converts an int into a float. We only need to convert the numerator, because this produces a mixed-type expression, and Python will automatically convert the denominator. Notice that putting the float() around the entire expression would not work.

average = float(sum/n)

In this form, sum and n could both be ints causing Python to perform an integer division and then convert the resulting quotient to a float. Of course, this float would always end in 0, since it is being converted from an int. That is not what we want.

Python also provides int() and long() functions that can be used to convert numbers into ints and longs, respectively. Here are a few examples.

As you can see, converting to an int or long int simply discards the fractional part of a float; the value is truncated, not rounded. If you want a rounded result, you can add 0.5 to the value before using int() , assuming the value is positive.

A more general way of rounding off numbers is to use the built-in round function which rounds a float off to the nearest whole value.

Notice that round returns a float. The last example shows how the result can then be turned into an int value, if necessary, by using int().

Was this article helpful?

0 0

Post a comment