Artificial Intelligence Design Health Innovation Machine Learning Technology

Writing Import Statements

Pinterest LinkedIn Tumblr

Introduction

The import statement is used for accessing modules from different directories. You can with the use of an import statement import any required file into your working directory. You can also import built-in modules of your choice from the Python library. Import statements in python are important for structuring your code effectively. Making use of the import statement properly will make you as a developer more productive, allowing you to reuse code while keeping your projects maintainable.

Importing required files into your working directory

A Python file with a “.py” extension containing a script is effectively a module. For example, a module named “test.py”. Lets say test.py is the main module you are currently working on in your IDE meaning its the main module in your working directory, also called the scope of the project. Now, most times you have several python files or modules in a scope. You can import any python file or module that is present in the current scope into the main module you are working on. For example, you can import another file or module called “numbers.py” into the “test.py” module by coding the following in the “test.py” working directory:

ALSO READ  The Vision for Mixed Reality: Now vs The Future

import numbers

Take note though that the extension(.py) was not added in the code in other to import the file. It was just the file name without the extension. When you click on “Run”, there will be an output showing that it was successful. It is also very important to take note that you cannot import a file with a “.txt” extension, the import will not be successful. For example, lets say i want to import the “numbers.py” file into the “test.py” module as a text file. The code will look something like this:

import numberstext

Now this is wrong and wont run smoothly because we cannot import a “.txt” file into a module. It will return an error in the terminal plane because it is not a python file.

Importing built-in modules

We can also import built-in modules into our code. Python has a library of standard modules called built-in modules. This modules are directly built into the python interpreter and don’t have to be installed separately. For example, the built-in module: Json. You can import this module into your code by doing the following:

import json

json.decoder()

Once you execute the command, you can start using its functions directly. The list of built-in modules can be found in the python standard library. Some include:

1) Built-in Types:

  • Truth Value Testing
  • Boolean Operators- and, or and not
  • Comparisons
  • Numeric Types- int, float and complex
  • Iterator Types
  • Sequence Types- list, tuple, range
  • Text Sequence Type- Str
  • Binary Sequence Types- byte, byte array
  • Set Types- set, frozenset
  • Mapping Types- dict
  • Context Manager Types
  • Type Annotation Types- generic alias, union
ALSO READ  SQL Operators: The Arithmetic Operators And The Concepts Behind It.

We also have:

2) Numeric and Mathematical modules:

  • numbers- numeric abstract base classes
  • math- Mathematical functions
  • cmath- mathematical functions for complex numbers
  • decimal- decimal fixed point and floating point arithmetic
  • functions- rational numbers
  • Random- Generate psuedo-random numbers
  • Statistics- Mathematical Statistics functions

And so many more built-in functions.

Writing Import Statements

Let’s say we want to import the built-in module (math), then use one of its functions, specifically (sqrt) to find the square root of a number then print the result. We can do this by coding the following:

import math

root = math.sqrt(9)

print(root)

From the code above, we imported the module (math) and then made use of one of its functions(sqrt) to calculate the square root of a particular number. This is a perfect example of how to write import statements. Now a better way to write the code above is to use the keyword “import from”. Let’s see more to this in the next sub heading.

Writing Import Statements using keyword “import from”

The code written in the previous sub heading can be written in a better way using the keyword “import from”. Let’s take a look of how it will be coded in an IDE:

from math import sqrt

root = sqrt(9)

print(root)

As you can see from the code above, on the first line, the function(sqrt) is directly imported from the math module and on the second line, the math module was removed as there is no need for it to be specified any longer. When you run this code, it gives the same result as the code in the previous subheading. The only difference is the different ways in which we wrote our import statements.

ALSO READ  The Version Control System: A Vital Tool For Developers, It's Features and Benefits.

Take note though that you can also import as many functions as you want on a single go. For example:

from math import sqrt, log10, function

y = log10(50)

print(y)

Writing Import Statements using keyword “as”

Another way to write an import statement is using keyword “as” where the module you are importing as an alias or a different name. For example:

import math as m

root = m.sqrt(9)

print(root)

As you can see from the code above, you can create an alias or different name or shorter name for the module you are importing using the keyword “as”. This helps to make your work faster and easier so you don’t have to rewrite the module all over again. Note though that if in replacement of m on line 2, you use math, it wont work because the module(math) is now recognised as (m) not (math). You can also have an alias for the functions imported from a module. For example:

from math import function as f

This saves you the stress of writing function all over again.

Writing Import Statements using keyword “*”

To import all the functions in a given module, you make use of the keyword (*). Using this keyword gives you access to all the functions in a particular module. For example:

from math import*

This will basically import all the functions from the math module into your code. This, however, is not always the best practice because when you are working on a larger file, it can be difficult to locate your functions and it can also get confusing.

Conclusion

In conclusion, it is of good practice to make use of the import statement in your code, whether it is used to import python files or to import a bulit-in module, it makes your code neater and easier. So, why not choose today to make use of this amazing tool we have and make your journey as a developer satisfying and interesting.