Python Language


Simple Mathematical Operators All Versions

Python 2.x

2.0
2.1
2.2
2.3
2.4
2.5
2.6
2.7

Python 3.x

3.0
3.1
3.2
3.3
3.4
3.5
3.6

This draft deletes the entire topic.

Introduction

Python does common mathematical operators on its own, including integer and float division, multiplication, exponentiation, addition, and subtraction. The math module (included in all standard Python versions) offers expanded functionality like trigonometric functions, root operations, logarithms, and many more.
expand all collapse all

Examples

  • 9

    Python does integer division when both operands are integers. The behaviour of Python's division operators have changed between Python 2.x and 3.x.

    a, b, c, d, e = 3, 2, 2.0, -3, 10
    
    Python 2.x2.7

    The result of the ' / ' operator depends on the type of the numerator and denominator.

    a / b                  # = 1 
    
    a / c                  # = 1.5
    
    d / b                  # = -2
    
    b / a                  # = 0
    
    d / e                  # = -1
    

    Note that because both a and b are ints, the result is an int.

    The result is always rounded down (floored).

    Because c is a float, the result of a / c is a float.

    You can also use the operator module:

    import operator        # contains 2 argument arithmetic functions
    operator.div(a, b)     # = 1
    operator.__div__(a, b) # = 1
    
    Python 2.x2.2

    What if you want float division:

    Recommended:

    from __future__ import division # applies Python 3 style division to the entire module
    a / b                  # = 1.5 
    a // b                 # = 1
    

    Okay (if you don't want to apply to whole module):

    a / (b * 1.0)          # = 1.5
    1.0 * a / b            # = 1.5
    a / b * 1.0            # = 1.0    (careful with order of operations)
    
    from operator import truediv
    truediv(a, b)          # = 1.5
    

    Not recommended (may raise TypeError, eg if argument is complex):

    float(a) / b           # = 1.5
    a / float(b)           # = 1.5
    
    Python 2.x2.2

    The ' // ' operator in Python 2 forces floored division regardless of type.

    a // b                # = 1
    a // c                # = 1.0
    
    Python 3.x3.0

    In Python 3 the / operator performs 'true' division regardless of types. The // operator performs floor division and maintains type.

    a / b                  # = 1.5 
    e / b                  # = 5.0
    a // b                 # = 1
    a // c                 # = 1.0
    
    import operator             # contains 2 argument arithmetic functions
    operator.truediv(a, b)      # = 1.5
    operator.floordiv(a, b)     # = 1
    operator.floordiv(a, c)     # = 1.0
    

    Possible combinations (builtin types):

    • int and int (gives an int in Python 2 and a float in Python 3)
    • int and float (gives a float)
    • int and complex (gives a complex)
    • float and float (gives a float)
    • float and complex (gives a complex)
    • complex and complex (gives a complex)

    See PEP 238 for more information.

  • 7
    a, b = 1, 2
    
    # Using the "+" operator:
    a + b                  # = 3
    
    # Using the "in-place" "+=" operator to add and assign:
    a += b                 # a = 3 (equivalent to a = a + b)
    
    import operator        # contains 2 argument arithmetic functions for the examples
    
    operator.add(a, b)     # = 5  since a is set to 3 right before this line
    
    # The "+=" operator is equivalent to: 
    a = operator.iadd(a, b)    # a = 5 since a is set to 3 right before this line
    

    Python allows to concatenate two strings using + Operator.

    a = "first string "
    b = "second string"
    print a + b
    # Out: 'first string second string'
    

    Possible combinations (builtin types):

    • int and int (gives an int)
    • int and float (gives a float)
    • int and complex (gives a complex)
    • float and float (gives a float)
    • float and complex (gives a complex)
    • complex and complex (gives a complex)

    Note: the + operator is also used for concatenating strings, lists and tuples.

  • 6
    a, b = 2, 3
    
    (a ** b)               # = 8
    pow(a, b)              # = 8
    
    import math
    math.pow(a, b)         # = 8.0 (always float; does not allow complex results)
    
    import operator
    operator.pow(a, b)     # = 8
    

    Another difference between the built-in pow and math.pow is that the built-in pow can accept three arguments:

    a, b, c = 2, 3, 2
    
    pow(2, 3, 2)           # 0, calculates (2 ** 3) % 2, but as per Python docs,
                           #    does so more efficiently
    

    Special functions

    The function math.sqrt(x) calculates the square root of x.

    import math
    import cmath
    c = 4
    math.sqrt(c)           # = 2.0 (always float; does not allow complex results)
    cmath.sqrt(c)          # = (2+0j) (always complex)
    

    The function math.exp(x) computes e ** x.

    math.exp(0)  # 1.0
    math.exp(1)  # 2.718281828459045 (e)
    

    The function math.expm1(x) computes e ** x - 1. When x is small, this gives significantly better precision than math.exp(x) - 1.

    math.expm1(0)       # 0.0
    
    math.exp(1e-6) - 1  # 1.0000004999621837e-06
    math.expm1(1e-6)    # 1.0000005000001665e-06
    # exact result      # 1.000000500000166666708333341666...
    
Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Numerical types and their metaclasses

The numbers module contains the abstract metaclasses for the numerical types:

subclassesnumbers.Numbernumbers.Integralnumbers.Rationalnumbers.Realnumbers.Complex
bool
int
fractions.Fraction
float
complex
decimal.Decimal
Still have a question about Simple Mathematical Operators? Ask Question

Topic Outline