What Is a Type?

We’ve now seen two types of numbers (integers and floating-point numbers), so we ought to explain what we mean by a type. In Python, a type consists of two things:

  • A set of values
  • A set of operations that can be applied to those values

For example, in type int, the values are …, -3, -2, -1, 0, 1, 2, 3, … and we have seen that these operators can be applied to those values: +, -, *, /, //, %, and **.

The values in type float are a subset of the real numbers, and it happens that the same set of operations can be applied to float values. We can see what happens when these are applied to various values in Table 1, Arithmetic Operators. If an operator can be applied to more than one type of value, it is called an overloaded operator.


Table 1. Arithmetic Operators

Symbol

Operator

Example

Result

-

Negation

-5

-5

+

Addition

11 + 3.1

14.1

-

Subtraction

5 - 19

-14

*

Multiplication

8.5 * 4

34.0

/

Division

11 / 2

5.5

//

Integer Division

11 // 2

5

%

Remainder

8.5 % 3.5

1.5

**

Exponentiation

2 ** 5

32


Finite Precision

Floating-point numbers are not exactly the fractions you learned in grade school. For example, look at Python’s version of the fractions 2/3 and 5/3:

 

 >>>​​ ​​2​​ ​​/​​ ​​3
 0.6666666666666666
 >>>​​ ​​5​​ ​​/​​ ​​3
 1.6666666666666667

The first value ends with a 6, and the second with a 7. This is fishy: both of them should have an infinite number of 6s after the decimal point. The problem is that computers have a finite amount of memory, and (to make calculations fast and memory efficient) most programming languages limit how much information can be stored for any single number. The number 0.6666666666666666 turns out to be the closest value to 2/3 that the computer can actually store in that limited amount of memory, and 1.6666666666666667 is as close as we get to the real value of 5/3.

Operator Precedence

Let’s put our knowledge of ints and floats to use in converting Fahrenheit to Celsius. To do this, we subtract 32 from the temperature in Fahrenheit and then multiply by 5/9:

 >>>​​ ​​212​​ ​​-​​ ​​32​​ ​​*​​ ​​5​​ ​​/​​ ​​9
 194.22222222222223

Python claims the result is 194.22222222222223 degrees Celsius, when in fact it should be 100. The problem is that multiplication and division have higher precedence than subtraction; in other words, when an expression contains a mix of operators, the * and / are evaluated before the - and +. This means that what we actually calculated was 212 - ((32 * 5) / 9): the subexpression 32 * 5 is evaluated before the division is applied, and that division is evaluated before the subtraction occurs.

We can alter the order of precedence by putting parentheses around subexpressions:

 >>>​​ ​​(212​​ ​​-​​ ​​32)​​ ​​*​​ ​​5​​ ​​/​​ ​​9
 100.0

Table 2, Arithmetic Operators Listed by Precedence from Highest to Lowest shows the order of precedence for arithmetic operators.

Operators with higher precedence are applied before those with lower precedence. Here is an example that shows this:

 >>>​​ ​​-2​​ ​​**​​ ​​4
 -16
 >>>​​ ​​-(2​​ ​​**​​ ​​4)
 -16
 >>>​​ ​​(-2)​​ ​​**​​ ​​4
 16

Because exponentiation has higher precedence than negation, the subexpression 2 ** 4 is evaluated before negation is applied.


Table 2. Arithmetic Operators Listed by Precedence from Highest to Lowest

Precedence

Operator

Operation

Highest

**

Exponentiation

-

Negation

*, /, //, %

Multiplication, division, integer division, and remainder

Lowest

+, -

Addition and subtraction


Operators on the same row have equal precedence and are applied left to right, except for exponentiation, which is applied right to left. So, for example, because binary operators + and - are on the same row, 3 + 4 - 5 is equivalent to (3 + 4) - 5, and 3 - 4 + 5 is equivalent to (3 - 4) + 5.

It’s a good rule to parenthesize complicated expressions even when you don’t need to, since it helps the eye read things like 1 + 1.7 + 3.2 * 4.4 - 16 / 3. On the other hand, it’s a good rule to not use parentheses in simple expressions such as 3.1 * 5.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.248.162