Python Modules

Modules are ready made bundles of code, which can be used by developers. Modules contain code which was developed by some programmer(probably in some basement 🤷🏻) and have specific functionality. For example – Python’s Math Module contain many functions which can be used for doing Mathematical stuff in Python Code, similarly turtle module can be helpful for making graphical programs like Games.

How to import modules in Python?

  1. Modules can be imported in Python using from module_name import * statement(* being some function inside module). Here in order to initialise module, you need to do some_variable = *. Then refer to function using some_variable across whole program.
  2. Modules can also be imported using import module_name statement. Here your importing whole of module as compared to only some specific function in #1. In this case for initialising some function from module, you can do some_variable = module_name.function_name() and use some_variable throughout Python program for referencing to function.

Let’s try to understand this by some code examples.

# 1
from math import ceil
m = ceil          # Use variable m for referencing to math module function ceil()

# 2
import math
c = math.ceil()     # Use variable m for referencing to math module ceil() function
f = math.floor()    # Use variable f for referencing to math module floor() function

If your not aware of Python’s Math Module then read Math Module of Python article.

You Might Also Like

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

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

Recent Posts