You can learn about Functions in Python Programs with Outputs helped you to understand the language better.

## Python Programming – Numeric Functions

Math functions are very helpful in calculating some of the complex mathematical expressions like power, sine/cosine, factorials, etc.

eval ( )

The eval ( ) function dynamically translates the text provided by the user into an executable form that the program can process. This allows users to provide input in a variety of flexible ways; for example, users can enter multiple entries separated by commas, and the eval function evaluates it as a Python tuple.

max ( )

Python built-in function max() returns the largest of its parameters. Here is the simplest version of the program.

Example
Demo of max() function.

 def main ( ) : x1 = int(input(“Please enter first value(x1) : “)) x2 = int(input(“Please enter second value(x2) : “)) x3 = int(input(“Please enter third value(x3) : “)) print ( ” The largest value is ” , max (x1 , x2 , x3 ) ) main ( )RUN >>> please enter first value (x1) : -80 please enter second value (x2) : -20 please enter third value (x3) : -10 The largest value is -10 >>> >>> please enter first value (x1) : 56 please enter second value (x2) : 43 please enter third value (x3) : 23 The largest value is 56 >>>

min ( )

Python built-in function min() returns the smallest of its parameters. Here is the simplest version of our program.

Example
Demo of min ( ) function.

 def main ( ) : x1 = int ( input ( ” Please enter first values (x1) : ” ) ) x2 = int ( input ( ” Please enter second values(x2) : ” ) ) x3 = int ( input ( ” Please enter third values(x3) : ” ) ) print ( ” The smallest value is ” , min ( x1 , x2 , x3 ) ) main ( ) 1RUN >>> Please enter first values (x1) : -80 Please enter second values(x2) : -20 Please enter third values (x3) : -10 The smallest value is -80 >>> Please enter first values(x1) : 56 Please enter second values(x2) : 43 Please enter third values(x3) : 23 The smallest value is 23 >>>

pow ( )

The pow(x,y) function returns the value of x raised to the power of y, i.e.,xy. Both pow( ) and the double star (** ) operator perform exponentiation. ** is an operator and pow( ) is a built-in function. Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow( ) function for computing exact integer powers.

 print ( ” pow ( 2 , 0 ) : ” , pow ( 2 , 0 ) 1 print ( ” pow ( 2 , 2 ) : ” , pow ( 2 , 2 ) 4 print ( ” pow ( 2 , 3 ) : ” , pow ( 2 , 3 ) 8 print ( ” pow ( 2 , 4 ) : ” , pow ( 2 , 4 ) 16 print ( ” pow ( 3 , 4 ) : ” , pow ( 3 , 4 ) 81 print ( ” pow ( 2 , -2 ) : ” , pow ( 2 , -2 ) )RUN >>> pow ( 2 , 0 ) : 1 pow ( 2 , 2 ) : 4 pow ( 2 , 3 ) : 8 pow ( 2 , 4 ) : 16 pow ( 3 , 4 ) : 81 pow ( 2 , -2 ) : 0.25 >>>

round ( )

The round ( ) built-m function has a syntax of round (flat,ndig=0). It normally rounds a floating-point number to the nearest integral number and returns that result (still) as afloat. When the optional third dig option is given, round() will round the argument to the specific number of decimal places.

Example
Demo of round ( ) function.

 >>> round ( 3 ) 3 >>> round ( 3. 45 ) 3 b>>> round ( 3 . 4999999 ) 3 >>> round ( 3 . 4999999 , 1 ) 3 . 5

int ( )

It constructs an integer number from an integer literal, afloat literal (by rounding down to the previous whole number), or a string literal (provided that the string represents a whole number). For example,

 x = int ( 1 ) # x will be 1 x = int ( 2 . 8 ) # y will be 2 x = int ( ” 3 ” ) # Z will be 3

abs ( )

abs ( ) returns the absolute value of the given argu-ment. Here are some examples of using the abs ( ) built-in function:

Example
Demo of abs ( ) function.

 >>> abs ( -1 ) 1 >>> abs ( 10 . 0 ) 10 . 0

ceil ( )

The method ceil ( ) returns the smallest whole number greater than or equal to x. The general form is:
math. ceil ( x )

Demo of ceil ( ) function.

 Import math # This will Import math module print(“math.ceil ( -35 . 18 ) ” , math . ceil ( -35 . 18 ) ) print ( ” math . ceil ( 100 . 12 ) ” , math . ceil ( 100 . 12 ) ) print ( ” math . ceil ( 100 . 83) ” , math . ceil ( 100 . 83 ) ) print ( ” math . ceil ( 1 . 13 ) : ” , math . ceil ( 1 . 13 ) ) print ( ” math . ceil ( -1 . 13) : ” , math . ceil ( -1 . 13 ) ) RUN >>> math . ceil ( -35 . 18 ) : -35 math . ceil ( 100 . 12) : 101 math . ceil ( 100 . 83 ) : 101 math . ceil ( 1 . 13 ) : 2 math . ceil ( -1 . 13 ) : -1 >>>

floor ( )

The floor ( ) method returns the largest whole number less than or equal to x. For example,

Example
Demo of floor ( ) function.

 import math # This will import math module print ( ” math . floor ( -35 . 18 ) 11 , math . floor ( -35 . 18 ) ) print ( ” math . floor ( 100 . 12 ) ” , math . floor ( 100 . 12 ) ) print ( ” math . floor ( 100 . 83 ) ” , math . floor ( 100 . 83 ) ) print ( ” math . floor ( 1 . 3 ) : “, math . floor ( 1 . 3 ) ) print ( ” math . floor ( 2 . 0 ) : “, math . floor ( 2 . 0 ) ) RUN >>> math . floor ( -35 . 18 ) : -36 math . floor ( 100 . 12 ) : 100 math . floor ( 100 . 83 ) : 100 math . floor ( 1 . 3 ) : 1 math . floor ( 2 . 0 ) : 2 >>>

fabs ( )

The fabs ( ) function returns the absolute value of x. The general form is:
math . fab ( x )

Example
Demo of fab ( ) function.

 import math # This will import math module print ( ” math . fabs ( -35 . 18 ) : ” , math .fabs ( -35 . 18 ) ) print ( ” math . fabs ( 100 . 12 ) : ” , math . fabs ( 100 . 12 ) ) print ( ” math . fabs ( 100 . 83 ) : ” , math . fabs ( 100 . 83 ) ) print ( ” math . fabs ( 1 . 13 ) : ” , math . fabs ( 1 . 13 ) print ( ” math . fabs ( 2 . 0 ) : ” , math . fabs ( 2 . 0 ) ) RUN >>> math . fabs ( -35 . 18 ) : 35 . 18 math . fabs ( 100 . 12) : 100 math . fabs ( 100 . 83 ) : 100 math . fabs ( 1 . 13 ) : 1 . 13 math . fabs ( 2 . 0 ) : 2 . 0 >>>

sqrt ( )

The sqrt ( ) function returns the non-negative square root of x. If x is negative, a domain error occurs. For Example:
math . sqrt ( x )

Example
Demo of sqrt ( ) function.

 import math # This will import math module print ( ” math . sqrt ( 49 ) : ” , math . sqrt ( 49 ) ) print ( ” math . sqrt ( 5 ) : “, math . sqrt ( 7 ) ) print ( ” math . sqrt ( 64 ) : ” , math . sqrt ( 64 ) )RUN >>> math . sqrt ( 49 ) : 7 . 0 math . sqrt ( 5 ) : 2 . 6457513110645907 math . sqrt ( 64 ) : 8 . 0 >>>

Example
Program to compute the real roots of a quadratic equation.

The program shown in Figure 10.35 uses the sqrt (square root) to compute the roots of a quadratic equation of the form:
a x2 + bx + c = 0

 # A program that computes the real roots of a quadratic equation. # Illustrates use of the math library. # Note: this program crashes if the equation has no real roots, import math # Makes the math library available. def main ( ) : print ( ” This program finds the real solutions to a quadratic ” ) print ( ) a = int ( input ( ” Please enter the coefficient a : ” ) ) b = int (input ( ” Please enter the coefficient b: ” ) ) c = int ( input ( ” Please enter the coefficient c : ” ) ) discRoot = math . sqrt ( b * b – 4 * a * c ) root1 = (-b + discRoot) / (2 * a) root2 = (-b – discRoot) / (2 * a) print ( ) print ( ” The solutions are : ” , root1, root2) main ( )RUN >>> This program finds the real solutions to a quadratic Please enter the coefficient a: 3 Please enter the coefficient b: -4 Please enter the coefficient c: -2 The solutions are: 1.7207592200561266 -0.38742588672279316 >>>

exp ( )

The exp ( ) function returns the exponential number E raised to the power of a. For example,

Example
Demo of exp ( ) function.

 import math # This will import math module print ( ” math . exp ( – 35 . 18 ) : “, math . exp ( -35 . 18 ) ) print ( ” math . exp ( 100 . 12 ) : “, math . exp ( 100 .12 ) ) print ( ” math . exp ( 100 . 83 ) : “, math . exp ( 100 . 83 ) ) print ( ” math . exp ( 1 . 13 ) : “, math . exp ( 1 . 13 ) ) print ( ” math . exp ( 2 . 0) : ” , math . exp ( 2 . 0 ) )RUN math . exp ( – 35 . 18 ) : 5,266476209220732e-16 math . exp ( 100 . 12 ) : 3.0308436140742566e + 43 math . exp ( 100 . 83 ) : 6.164709417352325e + 43 math . exp ( 1 . 13 ) : 3.0956565001247114 math . exp ( 2 . 0 ) : 7.38905609893065 >>>

divmod ( )

divmod ( ) built-in function combines division and modulus operations into a single function call that returns the pair (quotient, remainder) as a tuple. For example,

 >>> divmod ( 14 , 5 ) ( 2 , 4 ) >>> divmod ( 2 . 7 , 1 . 5 ) ( 1 . 0 , 1 . 2000000000000002 )Example Demo of divmod ( ) function using integer values.def main ( ) : x1 = int ( input ( ” Please enter first value ( x1 ) : ” ) ) x2 = int(input(“Please enter second value(x2) : ” ) ) print ( ‘ The quotient and remainder are ” , divmod ( x1 , x2 ) ) main ( )RUN >>> Please enter first value( 0x1 ) : 10 Please enter second value ( x2 ) : 3 The quotient and remainder are ( 3 , 1 ) >>> >>> Please enter first value(x1) : 3 Please enter second value(x2) : 10 The quotient and remainder are ( 0 , 3 ) >>>

Example
Demo of divmod ( ) function using float value.

 def main ( ) : x1 = float ( input ( ” Please enter first value(x1) : ” ) ) x2 = float(input(“Please enter second value(x2) : ” ) ) print ( ” The quotient and remainder are ” , divmod ( x1 ,x2 ) ) main ( )RUN >>> Please enter first value(x1) : 2 . 5 Please enter second value(x2) : 10 The quotient and remainder are (0 . 0 , 2 . 5 ) >>>>>> Please enter first value(x1) : 10 Please enter second value(x2) : 2.5 The quotient and remainder are ( 4 . 0 , 0 . 0 ) >>>