×

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure the programming code very efficiently. Import function allows us to reusable of code and making the past done work and projects maintainable.

Basically, Python basic import is well categorised into modules and packages and the module is defined as they are viewed into Python with the help of importing process and it is also defined as an object that comes up with an organizational unit of Python programming code.

Python only responds to the file containing the extension .py.

The power of modules is that they can be imported anytime and can be reused in other code as well.

Example:

>>>import math
>>>math.pi

Output:

3.141592653

In this piece of code, we observed that we import the math module and use it and the second line we can access the pi variable which is within the math module. math is present and is part of the Python’s standard library which means it is universal in the Python and can be accessed and made available while working with the Python.

NOTE:

While importing we write math.pi rather than just pi because it is not only a module but also a namespace that keeps all the attributes of module together. It makes the code readable and organized in well manner.

We can import any module into the current program to use the functionality. If we want to import the module, we need to use the import keyword with the respective module name associated with. While interpreting the code, if import statement appears then it imports directly into our program. We can also use the functions inside a module by using the .dot () operator along with the associated module name.

For working of the code more efficiently, it is better to import one module only once per the interpreter window. If there is a chance in changing the modules we must again start the interpreter. And if we want test only one module iteratively just uses reload () with the module name with it.

reload(module name)

There are 3 types of ways importing modules:

  • From .. import statement
  • From ..import * statement
  • Renaming the imported module

From .. import statement:

This type is used to import only specific categorized functions and variables from a whole module instead of importing all the functions which are not necessary when choosing a particular function. It is used to import a specific kind of function from the main Python module.

Example:

>>>form math import add
>>>Print (add(1, 2))

Output:

3

We can make the use of add () directly without implementing the module name. when it comes to the point of implementation of multiple attributes at a once, we need to separate the multiple attributes with just a comma and the syntax is followed as:

From calculation import add,sub

Here, add and sub are two multiple attributes separated by comma in between.

From ..import * statement:

This type of statement is a drawback in the Python language because if we implement this statement we disturb the concept of namespace and the function import* is when used, imports all the related functions and classes into the program. These may create a conflict among the other defined functions and other libraries that we imported.

The main disadvantage of this format is that code readability and hiding of the bugs within the program.

Example:

from calculation import *
print(add(2, 3))
print(sub(3, 2))

Output:

5
1

Renaming the Imported Module:

We can rename the module that we are importing, and it comes to the use when the module name is large or wants to a give a short name to understand it easily. We need to use the keyword as and rename it.

Example:

import calculation as cal
print (cal.add(1, 2))

Output:

3

Once the module is renamed, we can’t access the first named module because it’s not going to be recognised anywhere in the program.

Here, the calculation is renamed as cal and calculation is not accessible anywhere in the code.

After all importing the modules, if we want to list out all the names used in the particular module we need to use the dir() function which gives the list.


Related Topics

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Multiprocessing in python

The word multiprocessing is used to compute the operation in which more than two processors run simultaneously with more than one central processor. The computer's performance increases gradually. Multiprocessing is...

6 minutes read.

Python Indentation

Overview The spaces at the start of a code line are known as an indentation in Python programming. Indentation is only used for readability in other programming languages like C, C++,...

8 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

Python Word Tokenizer

Python Overview Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. Python is an interpreted language...

4 minutes read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

Python String strip() method

Python String strip() method The string. strip() method in Python removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to...

1 minute read.

Exrex Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster...

4 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Python program to find whether a given number is even or odd

Python program to find whether a given number is even or odd A number is said to be even if any number is completely divisible by 2, which means there is...

1 minute read.

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for...

4 minutes read.

Python tuple() function

Python tuple() function The tuple() function in Python creates a tuple object. Syntax tuple([iterable]) Parameter Iterable: This parameter represents a sequence, collection or an iterator object. Return This function returns a tuple object. Example 1 # Python Program explaining #...

1 minute read.

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

3 minutes read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Set clear() Method

Python Set clear() Method The set.clear() method removes all the elements from the set. Syntax set.clear() Parameter NA Return None Example 1 # Python program explaining # the set.clear() method # initializing the set fruits = {"watermelon", "banana", "apple"} # printing the set...

2 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

Python Struct

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.