Python is a popular, high-level, and powerful programming language that allows developers to create numerous types of applications, from web apps and desktop software to machine learning algorithms and deep learning projects. Thanks to its popularity, a massive community of developers, and rich built-in functionality, Python has a large repository of modules that can be imported and included in your code to perform specific tasks.
When it comes to including external modules in your code, Python provides a straightforward and intuitive keyword, known as the “import” keyword. In this tutorial, we’ll look at how the “import” keyword works, and how it can be utilized to include external modules and packages inside your Python code.
What is the “import” Keyword in Python?
The “import” keyword is a built-in feature in Python that allows you to use external modules or packages inside your code. These modules can include functions, variables, and classes that can be integrated into your code to help perform a specific task or add new functionality to your application.
Python has several built-in libraries, such as “os,” “sys,” “math,” and “time,” that you can use in your code. These libraries contain various functions, methods, and classes that allow you to perform specific tasks and operations in your application. However, Python also provides the option of importing external packages and modules that may not be a part of the standard library.
Importing a Module in Python
Using the “import” keyword is relatively easy in Python. To include an external module in your code, you simply need to type the import statement and the name of the module you want to include. The syntax for importing a module is as follows:
For example, to include the “math” module, we can use the following code:
When you execute this line of code, it will allow Python to use all the features and functions of the “math” module inside your code. Thus, you can call the functions of the “math” module from inside your program.
import math x = math.pow(2,3) #2 raised to the power of 3 print(x) #output: 8
In this example, we include the “math” module in our program using the “import” keyword. The code then uses the “pow” function inside the “math” module to calculate the value of 2 raised to the power of 3, which is equal to 8. The code then prints the value of x to the console.
Importing a Module with Aliases
Python allows you to create an alias for a module, which is particularly useful when you’re working with modules that have long names or names that may conflict with names in your code. To create an alias for a module, you can use the “as” keyword, followed by the alias name.
The syntax for importing a module with an alias is as follows:
import modulename as aliasname
For example, to import the “numpy” module with an alias “np,” we can use the following code:
import numpy as np
Now, you can use the alias name “np” instead of the actual name “numpy” to access the functions and features of the module.
import numpy as np #creating a 1-dimensional numpy array array1 = np.array([1,2,3,4,5]) print(array1) #output: [1 2 3 4 5] #creating a 2-dimensional numpy array array2 = np.array([(1,2,3),(4,5,6)]) print(array2) #output: #[[1 2 3] # [4 5 6]]
In this example, we create a 1-dimensional numpy array and a 2-dimensional numpy array using the “numpy” module. However, we import the “numpy” module using an alias name “np.” So, we can use the term “np” instead of the term “numpy” in our code whenever we want to use functions from the numpy library.
Importing a Specific Function from a Module
Sometimes you may only need to use one or two functions from an external module and importing the entire module might not be required. In such cases, you can import specific functions or variables from a module using the “from” keyword.
The syntax for importing a specific function from a module is as follows:
from modulename import functionname
For example, to import the “sqrt” function from the “math” module, we can use the following code:
from math import sqrt
Now, you can use the “sqrt” function directly in your code without having to use the module name.
from math import sqrt x = sqrt(25) print(x) #output: 5.0
In this example, we import the “sqrt” function from the “math” module into our code using the “from” keyword. We then use the “sqrt” function directly in our code to calculate the square root of 25. The code then prints the value of x to the console.
The “import” keyword in Python is a helpful and powerful feature that allows you to include external modules and packages inside your code. In this tutorial, we covered the basics of importing modules in Python, how to import external libraries using an alias, and how to import specific functions from modules. Now you have a good understanding of how to utilize this feature in your Python projects to perform specific tasks and add new functionality to your applications.