float
python numbers would be 1.5, 3.14, 100.983647 etc
If you are having a programming background from other languages, like C++, you would have noticed that there would have been the Double
for double precision floating points.double floatNumber ;
floatNumber = 29.928746457
20+3j
where 20 is a real part, and 3j is an imaginary number.>>> integerNumber = 10
>>> floatNumber = 10.98
>>> complexNumber = 10+8j
>>> print(integerNumber,floatNumber,complexNumber) #
10 10.9 10+8j
>>> 2**2 # 4 i.e 2 to the power 2
),>>> a+b # 30
>>> b-a # 10
>>> a*b # 200
>>> b/a 2
print()
python function to display your output,as, for instance>>> print(b%a) # 20/10 remainder is 0
.math
.
This module has capabilities that we will require in our day-to-day mathematical calculations.
Let's get started with math module.
>>>import math
>>>from math import *
>>>from math import functionname
import math
will import the whole math module as a folder, and to get the internal files, that is, functions,pow()
we write:>>>import math
>>>math.pow(4,2) # 4 to the power 2 = 16
from math import functionname
on the other part do the otherwise. Instead of importing the whole module as a folder.>>>example.func()
to call our function in the file, we go strait and say func()
and we are good to go.from module import function
one-by-one to import our function, we will kinda get wearly and/or,from modulename import *
, and everything in the modulename will be used as if we wrote it in our recent file.math.acos()
: a math module function, which returns arc cosine of a number, measured in radians. >>>math.acos(0.8) # 0.6435011087932843
math.acosh()
: a math module function, return an inverse hyperbolic cosine.>>>math.acosh(90) # 5.192925985263684
math.asin()
: Just like math.acos
but returns the arc sine of a given number.>>>math.asin(0.8) # 0.9272952180016123
math.asinh()
: Similar to math.acosh
but this looks on hyperbolic sine.>>>math.asinh(90) # 5.192987713658941
math.atan()
: a math module function, which returns arc tangent of a number, measured in radians. >>>math.atan(0.8) #0.6747409422235527
math.atan2()
: a math module function, return an arc tangent of two numbers, that is, for instance 'y' and 'x'.math.tan
but this takes two numbers. however, consider the two code snippets below:from __future__ import division # We want decimal points
z = 9/8 # 1.125
math.atan(z) # 0.844153986113171
>>>math.atan2(9/8) # 0.844153986113171
math.atanh()
: Similar to math.acosh
but this looks on inverse hyperbolic tangents.>>>math.atanh(0.8) # 1.0986122886681098
math.ceil()
: a math module function that returns the ceiling of number, import math
math.ceil(1) # 1
math.ceil(1.1) # 2
math.ceil(1.9) # 2
math.copysign()
: This math module function takes two numbers, import math
math.copysign(4,-3) # -4 (4 is now negative)
math.copysign(-2,1) # 2 (2 is positive because it copied sign of 1)
math.copysign(2,5) # 2 (last number is positive)
math.cos()
: Math module function which returns the cosine of a number.>>>math.cos(0.8) # 0.6967067093471654
math.cosh()
: Math module function which returns the hyperbolic cosine of a number.>>>math.cos(0.8) # 1.3374349463048447
math.degrees()
: Math module function which converts radians of an angle to degrees.>>>math.degrees(1.5707963267948966) # 90.0 (right angle)
math.e
: Math module object which returns the Euler Number.>>>math.e # 2.718281828459045
math.exp()
: Math module function which returns the Euler Number raised to a number passed to a function.>>>math.exp(2) # (2.718281828459045 to the power 2: Euler**2)
math.expm1()
: Math module function which returns the Euler Number raised to a number passed to a function, minus 1.>>>math.expm1(2) # (2.718281828459045**2)-1 = 6.38905609893065
math.fabs()
: Math module function (short of float absolute),
which returns the absolute value of a floating point number.>>>math.fabs(2) # 2.0
math.factorial()
: Math module function which returns the factorial of a given number, ie x*(x-1)*(x-2)*...*(x-x+1).>>>math.factorial(4) # 4*3*2*1 = 24
math.floor()
: Math module function which returns the largest integral value, which is less than or equal to the given float.>>>import math
>>>math.floor(2) # 2.0
>>>math.floor(2.1) # 2.0
>>>math.floor(2.9) # 2.9
math.fmod()
: Math module function which returns the floating point number from modulus division of two numbers.>>>math.fmod(5,3) # 5%3 converted to float = 2.0
math.frexp()
: Math module function/method which returns pair (tuple) of
mantissa and exponent of a given number.>>>math.frexp(2) # (0.5,2)
math.fsum()
: Math module function/method which returns an accurate, ie without truncating, sum of
numbers of an iterable (lists/tuples).>>>math.fsum([2.0,1.5,3.3]) # 2.0+1.5+3.3 = 6.8
math.hypot()
: Math module function/method which returns euclidean distance, as described in the
Pythagoras Theory, that is:h = √ a2 + b2
.>>>math.hypot(2,3) #
√ 22 + 32
= 3.605551275463989
math.fisinf()
: Math module function/method which returns a
boolean indicating whether the given number is infinite or not.>>>math.isinf(float('inf')) # True
math.isnan()
: Math module function/method which returns a boolean indicating whether a passed float is not a number.
>>>math.isnan(float('nan')) # True
math.ldexp()
: Math module function/method which takes two numbers ie x and y and returns the product(multiplication)
of first number (x) and 2 to the power last number (y), that is x*(2**y).>>>math.ldexp(2,3) # 2*(2**3) = 16
math.log()
: Math module function/method which returns a logarithm of a number.
It takes two numbers, x and y with the first number being the number we want to do log calculations, and the second, y, being the base.>>>math.log(2,10) # log 2 base 10 = 0.30102999566398114
math.log10()
: Math module function/method which takes a numbers ie x and returns its log to base 10.>>>math.log10(2) # log 2 base 10 = 0.30102999566398114
math.log1p()
: Math module function/method which takes a number ie x and
returns the natural logarithm of that number plus 1, that is math.log1p(x)
is the same as math.log(x+1)
.>>>math.log1p(2) # math.log(2+1) = 1.0986122886681096
math.modf()
: Math module function/method which takes a number ie x
and returns the fractional part, and its integer part, both in floating point numbers.>>>import math
>>>math.modf(2) # (0.0,2.0) 0.0 means no fractional part.
>>>math.modf(2.5) # (0.5,2.0) 0.5 is fractional part and 2.0 is integral part.
math.pi
: Math module Object which returns the PI used in circle calculations.
for instance, area of a circle is
>>>math.pi # 3.141592653589793
math.pow()
: Math module function/method which takes two numbers and returns the first number raised to the last number.
That is, if numbers are 'x' and 'y', then it returns the results of xy.>>>math.pow(2,3) # 23 = 8
math.radians()
: Math module function/method which converts and angle from degrees to radians.
for instance, an angle 'x', having 90 degrees, in radians is:>>>math.radians(90) # 1.5707963267948966
math.sinh()
: Math module function/method which returns the hyperbolic sine of a given number.>>>math.sinh(5) # 74.20321057778875
math.sqrt()
: Math module function/method which returns the square root of a given number.>>>math.sqrt(9) # 3
math.tan()
: Math module function/method which returns the tangent of a given number, measured in radians.>>>math.tan(30) # -6.405331196646276
math.tanh()
: Math module function/method which returns the hyperbolic tangent of a given number.>>>math.tanh(30) # 1.0
math.trunc()
: Math module function/method which returns the tangent of a given number, measured in radians.>>>math.tan(30) # -6.405331196646276
math.trunc()
: Math module function/method and the last, which truncates a given number to the nearest integral number.math.floor()
but this returns integer.>>>math.trunc(2.9) # 2