Python From Beginner to Advanced

0% completed

Previous
Next
Importing Modules

Modules in Python are simply files containing Python code that define functions, variables, and classes, which can be used once they are imported into another Python script.

Modules help break down complex programs into smaller, manageable, and reusable parts. They not only help in maintaining and organizing code efficiently but also in sharing functionalities between projects. Python comes with a rich standard library composed of numerous modules providing ready-to-use functionalities ranging from file I/O operations to network communications.

Importing of Modules

Modules are imported using the import statement. Once imported, you can access the functions, variables, and classes defined in that module using the dot notation.

Example

In this example, we demonstrate how to import and use the math module.

Python3
Python3

. . . .

Explanation:

  • import math: This line imports the math module, which includes functions for mathematical operations.
  • result = math.sqrt(25): Calls the sqrt function from the math module to calculate the square root of 25.
  • The result is printed, demonstrating how the function from the module is used.

Importing Specific Attributes from a Module

You can also choose to import specific attributes (functions, classes, or variables) from a module instead of the entire module. This is done using the from keyword.

Example

In this example, we will import and use a specific function from the math module.

Python3
Python3

. . . .

Explanation:

  • from math import sqrt: This imports only the sqrt function from the math module.
  • result = sqrt(49): Since sqrt is imported directly, it can be used without the math. prefix.
  • The result is then printed, showing how the directly imported function is used.

Using Aliases for Imports

Sometimes it is convenient or necessary to rename a module or its attributes when importing them, to avoid conflicts with existing names or simply for ease of use. This can be done using the as keyword.

Example

In this example, we import a module and a function with aliases.

Python3
Python3

. . . .

Explanation:

  • import math as m: The math module is imported with an alias m, allowing us to reference it as m instead of math.
  • from math import sqrt as square_root: The sqrt function is imported with the alias square_root.
  • result = square_root(m.pi): Demonstrates the use of both aliases in a single expression to calculate the square root of the mathematical constant pi.
  • The result is displayed, showcasing the use of aliased imports for clarity and convenience.

Understanding how to import and use modules effectively is crucial for developing scalable and maintainable Python applications. Modules enhance code reusability and help organize functionality into distinct namespaces, reducing conflicts and errors.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next