# Python Programming – Constants

In this Page, We are Providing Python Programming – Constants. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

## Python Programming – Constants

Constants

math.pi
The mathematical constant π.

math. e
The mathematical constant e.

>>> math.e
2.718281828459045

Number-theoretic and representation functions

math . ceil ( x )
Return the ceiling of x as a float, the smallest integer value greater than or equal to x.

>>> import math
>>> math . ceil ( -3 . 2 )
-3 . 0
>>> math . ceil ( 3 . 2 )
4 . 0

math . copysign ( x , y )
Return x with the sign of y.

>>> math . copysign ( 5 . 1 , -2 . 8 )
-5 . 1
>>> math . copysign ( -5 . 1 , 2 . 8 )
5 . 1

math . tabs ( x )
Return the absolute value of x.

>>> math . fabs ( - 4 . 2 )
4 . 2

math . factorial ( x )
Return x factorial. Raises ValueError, if x is not integral or is negative.

>>> math . factorial ( 5 )
120
>>> math . factorial ( -5 )
Traceback ( most recent call last ) :
File "<stdin>", line 1, in <module>
ValueError: factorial ( ) not defined for negative values

math . floor ( x )
Return the largest integer value less than or equal to x as a float.

>>> math . floor ( -3 . 2 )
-4 . 0
>>> math . floor ( 3 . 2 )
3 . 0

math . fmod ( x , y )
Return remainder of a division expression. Note that the Python expression x%y may not return the same result. The result of fmod (x, y) has same sign as x, while x%y returns a result with the sign of y instead.

>>> math . fmod ( 5 . 0 , -2 . 0 )
1 . 0
>>> 5 . 0% -2 . 0
-1 . 0
>>> math . fmod ( -5 . 0 , 2 . 0 )
-1 . 0
>>> -5 . 0 % 2 . 0
1 . 0

Consider the following interesting scenario.

>>> math . fmod ( -1e-100 , 1e100)
-1e-100
>>> -1e-100 % 1e100
1e +100

It can be observed that fmod (-1e-100 , 1e100) returns -1e-100, but the result of Python’s – 1e-100%1e100 is 1e100-1e-100, which cannot be represented exactly as a float, and rounds to the surprising 1e100. For this reason, function fmod ( ) is generally preferred when working with floats, while Python’s x % y is preferred when working with integers.

math . frexp ( x )
Return the mantissa and exponent of x as the pair (m, e). The m is a float and e is an integer such that x = m x 2e exactly. If x is zero, returns ( 0 . 0 , 0 ) , otherwise 0 . 5<=abs ( m ) <1.

>>> math . frexp ( 4 . 0 )
( 0 . 5 , 3 )
>>> 0 . 5*2**3
4 . 0
>>> math . frexp ( 0 . 1 )
( 0 . 8 , -3 )
>>> 0 . 8* 2**-3
0 . 1
>>> math . frexp ( -4 . 0 )
( -0 . 5 , 3 )
>>> -0 . 5*2**3
- 4 . 0

math.fsum(iterable)
Return an accurate floating point sum of values in the iterable.

>>> math . fsum ( [ . 1 , . 1 , . 1 , . 1 , . 1 , . 1 , . 1 , . 1 , . 1 , . 1 ] )
1 . 0

math . isinf ( x )
Check if the float x is positive or negative infinity.

>>> a=1e+300
>>> a
1e+300
>>> math . isinf ( 1e+300 )
False
>>> a=1e+310
>>> a
inf
>>> math . isinf ( 1e+310 )
True

Calculating an exponent with floating point values, in particular, raises Overf lowError instead of preserving the inf result.

>>> a=10 . 0**310

Traceback ( most recent call last ) :
File "<pyshell#l>", line 1, in <module>
a=10 . 0**310
OverflowError: (34 , ' Result too large ' )

math . isnan ( x )
Check if the float x is a nan (not a number), nan does not compare as equal to any value, even itself, so nan should be checked using isnan () function.

>>> a=1e+310
>>> a
inf
>>> b=a/a
>>> b
nan
>>> math . isnan ( a )
False
>>> math . isnan ( b )
True

math . ldexp ( x , i )
Return x* (2**i). This function is reverse of function frexp ( ).

>>> math . 1dexp ( -0 . 5 , 3 )
-4 . 0
>>> -0 . 5*2**3
-4 . 0
>>> math . ldexp ( 0 . 8 , -3 )
0 . 1
>>> 0 . 8*2**-3
0 . 1

math . modf ( x )
Return the fractional and integer parts of x. Both results carry the sign of x and are floats.

>>> math . modf ( 1 . 5 )
( 0 . 5 , 1 . 0 )

>>> math . modf ( -1 . 5 )
( -0 . 5 , -1 . 0 )

math . trunc ( x )
Return the real value x truncated to an integer.

>>> math . trunc ( 93 . 2508 )
93
>>> math . trunc ( -93 . 2508 )
-93

Power and logarithmic functions

math . exp ( x )
Return ex.

>>> math . e**-3 . 2
0 . 04076220397836622
>>> math . pow ( math . e , -3 . 2 )
0 . 04076220397836622
>>> math . exp ( -3 . 2 )
0 . 04076220397836621

math . expm1 ( x )
Return ex-1. For small floats x, the subtraction in exp(x)-1 can result in a significant loss of precision; the expml ( ) function provides a way to compute this quantity to full precision:

>>> x=0 . 0000000000000000000000001
>>> math . exp ( x ) , -1
0 . 0
>>> math . expm1 ( x )
1e - 25

math . log ( x [ , base ] )
With one argument, return the natural logarithm of x (to base e). With two arguments, return the logarithm of x to the given base, calculated as log (x) / log (base).

>>> math . log ( 9 )
2 . 1972245773362196
>>> math . log ( 9 , 2 )
3 . 1699250014423126

math . loglp ( x )
Return the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero.

>>>x=0.0000000000000000000000001
>>> X
1e-25
>>> 1+x
1 . 0
>>> math . 1og ( 1 +x )
0 . 0
>>> math . 1og1p ( x )
1e-25

math . 1og10 ( x )
Return the base-10 logarithm of x. This is usually more accurate than log (x, 10).

>>> math . 1og10 ( 100 )
2 . 0
>>> math . 1og10 ( 10000 )
4 . 0

math . pow ( x , y )
Return x raised to the power y. In particular, pow ( 1 . 0 , x ) and pow ( x , 0 . 0 ) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow (x, y) is undefined, and raises ValueError.

>>> math.pow ( 9 . 0 , 0 . 5 )
3 . 0
>>> math . pow ( -9 . 0 , 0 . 5 )
Traceback ( most recent call last ) :
File "<stdin>" , line 1 , in <module>
ValueError: math domain error

Unlike the built-in * * operator, math. pow ( ) converts both its arguments to type float.

math . sqrt( x )
Return the square root of x. Computing the square roots of negative numbers requires complex numbers, which are not handled by math module. Any attempt to calculate a square root of a negative value results in ValueError.

>>> math . sqrt ( 9 . 0 )
3 . 0
>>> math . sqrt ( -9 , 0 )

Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
math . sqrt ( -9 . 0 )
ValueError: math domain error

Trigonometric functions

math . acos ( x )
Return the arc cosine of x, in radians.

>>> math . acos ( 0 . 5 )
1 . 0471975511965979

math . asin ( x )
Return the arc sine of x, in radians.

>>> math . asin ( 0 . 5 )
0 . 5235987755982989

math . atan ( x )
Return the arc tangent of x, in radians.

>>> math . atan ( 0 . 5 )
0 . 4636476090008061

math . atan2 ( y , x )
Return atan(y/x), in radians. The resuit is between -TI and TI. The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis. The point of atan2 () is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle. For example, atan (1) and atan2 (1,1) are both n /4, but atan2 (-1,-1) is -3#/4.

>>> math . atan ( 1 . 0 )
0 . 7853981633974483
>>> math . pi / 4
0 . 7853981633974483

math . cos ( x )
Return the cosine of x radians.

>>> math . cos ( 0 . 7853981633974483 )
0 . 7071067811865476

math . hypot ( x , y )
Return the Euclidean distance, $$\sqrt{x^{2}+y^{2}}$$ . This is the length of the vector from the origin to point (x,y).

>>> math . hypot ( 3 . 0 , 4 . 0 )
5 . 0

math . sin ( x )
Return the sine of x radians.

>>> math . sin ( 0 . 7853981633974483 )
0 . 7071067811865475

math . tan ( x )
Return the tangent of x radians.

>>> math.tan ( 0 . 7853981633974483 )
0.9999999999999999

Angular conversion

math . degrees ( x )
Converts angle x from radians to degrees.

>>> math . degrees ( 1 . 5707963267948966 )
90 . 0
>>> 1 . 5707963267948966*180 / math . pi
90 . 0

math . radians ( x )
Converts angle x from degrees to radians.

>>> math . radians ( 90 )
1 . 5707963267948966
>>> ( 90*math . pi ) / 180
1 . 5707963267948966

Hyperbolic functions

math . acosh ( x )
Return the inverse hyperbolic cosine of x.

>>> math . cosh ( 1 . 0 )
1 . 5430806348152437

math . asinh ( x )
Return the inverse hyperbolic sine of x.

>>> math . asinh ( 1 . 0 )
0 . 8813735870195429

math . atanh ( x )
Return the inverse hyperbolic tangent of x.

>>> math . atanh ( 0 . 8 )
1 . 0986122886681098

math . cosh ( x )
Return the hyperbolic cosine of x.

>>> math . cosh ( 0 . 7853981633974483 )
1 . 3246090892520057

math . sinh ( x )
Return the hyperbolic sine of x.

>>> math . sinh ( 0 . 7853981633974483)
0 . 8686709614860095

math . tanh ( x )
Return the hyperbolic tangent of x.

>>> math . tanh ( 0 . 7853981633974483 )
0 . 6557942026326724

Special functions

math . erf ( x )
Return the error function at x.

>>> math . erf ( 0 . 25 )
0 . 2763263901682369

math . erfc ( x )
Return the complementary error function at x i.e. 1-erf (x).

>>> math . erf c ( 0 . 25 )
0 . 7236736098317631

math . gamma ( x )
Return the gamma function at x.

>>> math . gamma ( 5 . 5 )
52 . 34277778455352

math . lgamma ( x )
Return the natural logarithm of the absolute value of the gamma function at x.

>>> math . 1 gamma ( 5 . 5 )
3 . 9578139676187165