Updated April 15, 2023
Introduction to Python Import Module
Python import module is very similar to including files in programming languages like C/C++. It is used to include the code libraries so that we can use those pre-built functionalities in our current project. Python has huge libraries of codes and modules that have pre-built code. So while creating a project, we need to write coding for each and everything; we just need to import the required module to inherit those features into our project. There are many functions in python that don’t require any module like the print function.
How to Import-Module in Python?
To import any module into python, we need to use import statements with module names at starting the code.
Here random is the module name. When we are importing any module into our program, all the features of that module are available in our program. To access any method into that module, we have to use the dot notation to access the function. We have to write the module name, then the dot and then the method name.
- Using From: We can also import our module using ‘from’ the keyword. It works the same as an import but gives us additional features. Now we don’t need to use dot notation while calling the methods from the module that we have imported. Now we can directly call any method.
- Using Alias: Sometimes, when we use importing our module, the module name is already being used in our project; in such case, module importing will not work, and our code will not work properly. So to avoid such scenarios, we can give another name to our module, i.e. alias name. Now we can use this alias to access all the methods of the module.
import [module] as [alias_name]
import random as rand rand.randint()
Here rand is the alias for our random module. It is very helpful in big projects where we have so many modules imported.
import random print(random.randint(1,5))
In the above example, we have used the randint function. This function takes two parameters, starting value and ending value. Parameters should be of integer type; if we pass floating-point number, then this function will return an error. We have 1 as starting number and 5 as the ending number in the above function, so the function will return a random number between 1 and 5. Both parameters can be the same number, but the first number should be smaller than the second number; otherwise, it will generate an error.
import random print(random.randrange(1,10,2))
In the above program, we have used the randrange function. Suppose we break this function rand + range. As we all know range function returns the list of integer value depending upon and starting and ending value and step value. Here 1 is a start, 10 as an end and 2 as a step value. Internally a range function will be executed that will generate the list of numbers like 1,3,5,7,9. Now random will pick any random value from this list only.
Examples of Python Import Module
Following are the different examples of the Python Import-Module.
import math a = 3.4 print("Ceil of 3.4 is: ") print(math.ceil(a)) print ("The floor of 3.4 is: ") print (math.floor(a))
We have used the math module in the above program and used two math modules, ceil and floor. Ceil method gives the next round off the value of the number. The floor method gives the integral value smaller than the value. If we pass the integral value, then the same value is returned by both the function.
import math a = 6 print("Factorial of 6 is: ") print(math.factorial(a))
In the above program, we import the math module and use the factorial method of the math module. We just need to mention the module name dot method name.
import math as m a = 6 print("Factorial of 6 is: ") print(math.factorial(a))
As you can in the above program, we have imported the math module, but the program is giving error as math is not defined. We have created the alias name as ‘m’ for the math module. So we have to use alias names only for calling the methods.
import math as m a = 6 print("Factorial of 6 is: ") print(m.factorial(a))
This time we have used an alias name for calling the method, and it works fine.