Python Modules

  • In Python, a module is a collection of definitions, functions, and statements that are organized together in a single file and can be used within other programs.
  • Modules help organize your code into reusable and manageable parts, and can also provide access to a wide range of built-in functionality.

To use a module in your code, you need to first import it using the import statement.

example of how you could use the math module to perform some calculations in the context of a car:

When run, the code outputs:

You can also import specific definitions or functions from a module using the from ... import ... statement. For example, if you only need the sqrt function from the math module, you can import it like this:

As approached above, you don’t have to prefix the function with the module name every time you use it.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top