Last Updated:

Python : Modules | in-built functions in Modules

Module in Python

Module Concept


In the C language, there are special files where various functions, class variables, and the like are located. This is very convenient, because the person working in the program can save his own time and constantly not write the same and functions many times.

It is noteworthy that in the Python programming language for this there are also modules that include functions, classes, and even compiled code.

In Python, there are three types of these modules:

  1. These are modules that were written in Python (.py).
  2. Then these are modules that were written in C, but the loading of such modules occurs dynamically (.dll, .sl, and so on).
  3. And the latter are modules that were written in C, but they were associated with some kind of interpreter.

Number representation functions

Let's start simple.

ceil ( ) and floor ( )


Work with the integer part of the number. It is these functions that are called general-purpose functions. The first will round any number in your program to near standing = integer (note here: rounding occurs upwards). The second removes in your number all the numbers that relate to decimal places.

They also have a common purpose: both functions take a decimal number as an argument, and then output an integer.

Let's look at a fairly simple case:

# Import of the module math
import math
# Fractional number
# output of the whole part, rounding to a larger
print ("Upper limit 7.20:"math.ceil(number))
# output of the whole part, rounding to a smaller
print("Lower limit 7.20:"math.floor(number))

As a result, our result:

Upper limit 7.20:8

Lower limit 7.20: 7

Fabs function ( )

Searches for an absolute value. This function is used to calculate the absolute value of any number. It is worth noting that if the number you have chosen contains at least one negative sign in its composition, then this function will clear it, and then display a positive number instead.

Here's a case:

# Import module math
import math
number = -7.20
# output absolute value

And our small result:


fmod() function

Responsible for finding the residue during division. This function is used with the variables x and y. It is she who outputs x % y. The only difference is that x % y will always work only with integers. But this wonderful function can also be used for numbers that have a floating point.

Let's get acquainted with a more specific case:

# Import module math
import math
print (math.fmod(11,2))
print (math.fmod(-11,2))
print (math.fmod(-11.2,2))
print (math.fmod(11.2,2))

And see what we get as a result:





Factorial function ( )

It's very simple here: it's a function belonging to the factorial. Its essence lies in the fact that it takes an integer and a positive number, and then finds and returns its factorial. Here we really want to draw your attention: the use of a negative number is simply meaningless, it will lead you to get an error in the value.

Let's look at one of the cases to make it clearer:

# Import module math
import math
number = 11
# output of the factorial number
print ("factorial", math.factorial(number))

And here's what we'll get as a result:

39916800 factorial

And here's a case of trying to enter a negative number. Remember, you can't do that!

# Import module math
import math
number = -11
# output of the factorial number
print ("factorial", math.factorial(number))

And what do we immediately see in the result bar:

ValueError: factorial ( ) not defined for negative values

Function exp ( )

This function takes one parameter as a fractional number and then outputs e^x.

You may want to consider a few use cases:

# Import module math
import math
print(e to power 3", math.exp(3))
print(e to power 9", math.exp(9))
print(e to power 14", math.exp(14))
print(e to power 2", math.exp(2))

Now let's take a look at the result:

e to power 3 20.08554

e to the power of 9 8103.08393

e to the power of 14 1202604.28416

e to power 2 7.38906

We really hope that this article was useful to you, and you were able to learn something new for yourself here. You can always leave your feedback and comments on this topic in the comments a little below.