Simple Mathematical Operators All Versions
Python 2.x
Python 3.x
This draft deletes the entire topic.
Introduction
Examples
-
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.7The 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
andb
areint
s, the result is anint
.The result is always rounded down (floored).
Because
c
is a float, the result ofa / c
is afloat
.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.2What 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.2The ' // ' operator in Python 2 forces floored division regardless of type.
a // b # = 1 a // c # = 1.0
Python 3.x3.0In 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
andint
(gives anint
in Python 2 and afloat
in Python 3)int
andfloat
(gives afloat
)int
andcomplex
(gives acomplex
)float
andfloat
(gives afloat
)float
andcomplex
(gives acomplex
)complex
andcomplex
(gives acomplex
)
See PEP 238 for more information.
-
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
andint
(gives anint
)int
andfloat
(gives afloat
)int
andcomplex
(gives acomplex
)float
andfloat
(gives afloat
)float
andcomplex
(gives acomplex
)complex
andcomplex
(gives acomplex
)
Note: the
+
operator is also used for concatenating strings, lists and tuples. -
-
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
andmath.pow
is that the built-inpow
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 ofx
.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)
computese ** x
.math.exp(0) # 1.0 math.exp(1) # 2.718281828459045 (e)
The function
math.expm1(x)
computese ** x - 1
. Whenx
is small, this gives significantly better precision thanmath.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...
-
a, b = 1, 2 import math >>> math.sin(a) # returns the sine of 'a' radians 0.8414709848078965 >>> math.cosh(b) # returns the inverse hyperbolic cosine of 'b' radians 3.7621956910836314 >>> math.atan(math.pi) # returns the arc tangent of 'pi' (3.141592653589793) radians 1.2626272556789115 >>> math.hypot(a,b) # returns the Euclidean norm, sqrt(a*a + b*b) 2.23606797749979
math.hypot
can also be used to calculate the Euclidean distance between two points(x1,y1)
&(x2,y2)
as follows>>> math.hypot(x2-x1,y2-y1)
You can use
math.degrees
andmath.radians
to convert from radians -> degrees and degrees -> radians respectively>>> math.degrees(a) 57.29577951308232 >>> math.radians(57.29577951308232) 1.0
-
It is common within applications to need to have code like this :
a = a + 1
or
a = a * 2
There is an effective shortcut for these in place operations :
a += 1 # and a *= 2
Any mathematic operator can be used before the '=' character to make an inplace operation :
-=
decrement the variable in place+=
increment the variable in place*=
multiply the variable in place/=
divide the variable in place//=
floor divide the variable in place # Python 3%=
return the modulus of the variable in place**=
raise to a power in place
Other in place operators exist for the bitwise operators (
^
,|
etc) -
By default, the
math.log
function calculates the logarithm of a number, base e. You can optionally specify a base as the second argument.import math import cmath math.log(5) # = 1.6094379124341003 # optional base argument. Default is math.e math.log(5, math.e) # = 1.6094379124341003 cmath.log(5) # = (1.6094379124341003+0j) math.log(1000, 10) # 3.0 (always returns float) cmath.log(1000, 10) # (3+0j)
Special variations of the
math.log
function exist for different bases.# Logarithm base e - 1 (higher precision for low values) math.log1p(5) # = 1.791759469228055 # Logarithm base 2 math.log2(8) # = 3.0 # Logarithm base 10 math.log10(100) # = 2.0 cmath.log10(100) # = (2+0j)
-
Like in many other languages, Python uses the
%
operator for calculating modulus.3 % 4 # 3 10 % 2 # 0 6 % 4 # 2
Or by using the
operator
module:import operator operator.mod(3 , 4) # 3 operator.mod(10 , 2) # 0 operator.mod(6 , 4) # 2
You can also use negative numbers.
-9 % 7 # 5 9 % -7 # -5 -9 % -7 # -2
If you need to find the result of integer division and modulus, you can use the
divmod
function as a shortcut:quotient, remainder = divmod(9, 4) # quotient = 2, remainder = 1 as 4 * 2 + 1 == 9
-
a, b = 2, 3 a * b # = 6 import operator operator.mul(a, b) # = 6
Possible combinations (builtin types):
int
andint
(gives anint
)int
andfloat
(gives afloat
)int
andcomplex
(gives acomplex
)float
andfloat
(gives afloat
)float
andcomplex
(gives acomplex
)complex
andcomplex
(gives acomplex
)
Note: The
*
operator is also used for repeated concatenation of string list and tuple, for example3 * 'ab' # = 'ababab' 3 * ('a', 'b') # = ('a', 'b', 'a', 'b', 'a', 'b')
-
a, b = 1, 2 # Using the "-" operator: b - a # = 1 import operator # contains 2 argument arithmetic functions operator.sub(b, a) # = 1
Possible combinations (builtin types):
int
andint
(gives anint
)int
andfloat
(gives afloat
)int
andcomplex
(gives acomplex
)float
andfloat
(gives afloat
)float
andcomplex
(gives acomplex
)complex
andcomplex
(gives acomplex
)
Remarks
Numerical types and their metaclasses
The numbers
module contains the abstract metaclasses for the numerical types:
subclasses | numbers.Number | numbers.Integral | numbers.Rational | numbers.Real | numbers.Complex |
---|---|---|---|---|---|
bool | ✓ | ✓ | ✓ | ✓ | ✓ |
int | ✓ | ✓ | ✓ | ✓ | ✓ |
fractions.Fraction | ✓ | ― | ✓ | ✓ | ✓ |
float | ✓ | ― | ― | ✓ | ✓ |
complex | ✓ | ― | ― | ― | ✓ |
decimal.Decimal | ✓ | ― | ― | ― | ― |
Topic Outline
Introduction
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft