Chapter 2 Using Python as a Calculator

2.4 Simple Arithmetics

The easiest way to perform mathematics calculation using Python is to use IDLE, the interactive development environment of Python, which can be used as a fancy calculator. To begin with the simplest mathematical functions, including integral addition, subtraction, multiplication and division, can be performed in IDLE using the following mathematical expressions:

The content after the # symbols are comments and can be ignored when typing the examples.

As can be seen from the examples above, a simple Python expression is similar to a mathematical expression. It consists of some numbers, connected by a mathematical operator. In programming terminology, number constants (e.g. 3, 5, 2) are called literals. An operator (e.g. +, −, ∗) indicates the mathematical function between its operands, and hence the value of the expression (e.g. 3 + 5). The process of deriving the value of an expression is called the evaluation of the expression. When a mathematical expression is entered, IDLE automatically evaluates it and displays its value in the next line.

Since expressions themselves represent values, they can be used as operands in longer composite expressions:

In the example above, the expression 3+2 is evaluated first. Its value 5 is combined with the next literal 5 by the − operator, resulting in the value 0 of the composite expression \(3 + 2 − 5\). This value is in turn combined with the last literal 1 by the + operator, ending up with the value 1 for the whole expression. In the example, operators are applied from left to right, because + and − have the same priority. In an expression that contains more than one operators, not necessarily all operators have the same priority. For example,

The expression above evaluates to 9, because the multiplication (∗) operator has a higher priority compared with the + and − operators. The order in which operators are applied is called operator precedence, and mathematical operators in Python follow the natural precedence by the mathematical function. For example, multiplication (∗) and division (/) have higher priorities than addition (+) and subtraction (−). An operator that has higher priority than multiplication is the power operator (∗∗).

In general, a ∗∗ b denotes the value of a to the power of b.

Similar to mathematical equations, Python allows the use of parentheses (i.e. ‘(‘ and ‘)’) to manually specify the order of evaluation. For example,

The value of the expression above is 5 because the bracketed expressions 3 + 2 and 5 − 4 are evaluated first, before the ∗ operator is applied.

In the examples above, an operator connects two literal operands, and hence they are called binary operators. An operator can also be unary, taking only a single operand. An example unary operator is −, which takes a single operand and negates the number.

Until this point, all the mathematical expressions have been integral, with the values of literal operands and expressions being integers. Take the division operator (/) for example,

The result of the integral division operation is the same as floating-point division. To find the remainder of integer division, the modulo operator (%) can be used and the result is integer.

Check your understanding

© Copyright 2024 GS Ng.

Next Section - 2.5 Floating Point Expressions