A programmer’s tools for performing calculations are math operators. Python has numerous operators that can be used to perform mathematical calculations.
A math expression performs
a calculation and gives a value. The following is an example of a simple math
expression:
12 + 2
The values on the right
and left of the operator are called operands.
Example:
(simple_math.py)
a=10;
b=20;
c=a+b
print(‘Total is:
“, c)
Floating-Point and
Integer Division
Python has two
different division operators. The / operator performs floating-point division,
and the // operator performs integer division.
The difference between
them is that the / operator gives the result as a floating-point value, and the
// operator gives the result as an integer.
Example:
>>> 5 /
2
2.5
>>> 5
// 2
2
The // operator works
like this:
• When the result is
positive, it is truncated, which means that its fractional part is thrown
away.
• When the result is
negative, it is rounded away from zero to the nearest integer.
Example:
>>> −5 // 2
−3
Operator Precedence
First, operations that are enclosed in parentheses are performed first.
Then the operator with the higher precedence is applied first.
The precedence of the
math operators, from highest to lowest, are:
1. Exponentiation: **
2. Multiplication,
division, and remainder: * / // %
3. Addition and
subtraction: + −
Notice that the
multiplication (*), floating-point division (/), integer division (//), and remainder
(%) operators have the same precedence. The addition (+) and subtraction (−) operators
also have the same precedence. When two operators with the same precedence share
an operand, the operators execute from left to right.
Example:
outcome = 12.0 + 6.0 /
3.0
There is an exception
to the left-to-right rule. When two ** operators share an operand, the
operators execute right-to-left. For example, the expression 2**3**4 is
evaluated as 2**(3**4).
Mixed-Type Expressions
and Data Type Conversion
When a math operation
is performed on two operands, the data type of the result will depend on the
data type of the operands. Python follows these rules when evaluating
mathematical expressions:
• When an operation is
performed on two int values, the result will be an int.
• When an operation is
performed on two float values, the result will be a float.
• When an operation is
performed on an int and a float, the int value will be temporarily converted to
a float and the result of the operation will be a float. (An expression that
uses operands of different data types is called a mixed-type expression.)
The first two
situations are straightforward: operations on ints produce ints, and operations
on floats produce floats. The third situation, which involves mixed-type
expressions:
my_number = 5 * 2.0
When this statement
executes, the value 5 will be converted to a float (5.0) and then multiplied by
2.0. The result, 10.0, will be assigned to my_number.
The int to float conversion
happens implicitly in above calucation.
For explicit
conversion, use either the int( ) or float( ) functions.