Python Modules

Python Modules

What is Python Module

A Python module is a file containing Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code.

Grouping related code into a module makes the code easier to understand and use. It also makes the code logically organized.

Create a Python Module

To create a Python module, write the desired code and save that in a file with .py extension. Let’s understand it better with an example:

Import module in Python

We can import the functions, and classes defined in a module to another module using the import statement in some other Python source file.

When the interpreter encounters an import statement, it imports the module if the module is present in the search path.

Note: A search path is a list of directories that the interpreter searches for importing a module.

For example, to import the module calc.py, we need to put the following command at the top of the script.

Python Import From Module

Python’s from statement lets you import specific attributes from a module without importing the module as a whole.

Import Specific Attributes from a Python module

Here, we are importing specific sqrt and factorial attributes from the math module.

Locating Python Modules

Whenever a module is imported in Python the interpreter looks for several locations. First, it will check for the built-in module, if not found then it looks for a list of directories defined in the sys.path. Python interpreter searches for the module in the following manner –

  • First, it searches for the module in the current directory.
  • If the module isn’t found in the current directory, Python then searches each directory in the shell variable PYTHONPATH. The PYTHONPATH is an environment variable, consisting of a list of directories.
  • If that also fails python checks the installation-dependent list of directories configured at the time Python is installed.

importing built-in module math

import math

?

# using square root(sqrt) function contained

# in math module

print(math.sqrt(25))

?

# using pi function contained in math module

print(math.pi)

?

# 2 radians = 114.59 degrees

print(math.degrees(2))?

?

# 60 degrees = 1.04 radians

print(math.radians(60))?

?

# Sine of 2 radians

print(math.sin(2))?

?

# Cosine of 0.5 radians

print(math.cos(0.5))?

?

# Tangent of 0.23 radians

print(math.tan(0.23))

?

# 1 * 2 * 3 * 4 = 24

print(math.factorial(4))?

?

# importing built in module random

import random

?

# printing random integer between 0 and 5

print(random.randint(0, 5))?

?

# print random floating point number between 0 and 1

print(random.random())?

?

# random number between 0 and 100

print(random.random() * 100)?

?

List = [1, 4, True, 800, "python", 27, "hello"]

?

# using choice function in random module for choosing

# a random element from a set such as a list

print(random.choice(List))

?

?

# importing built in module datetime

import datetime

from datetime import date

import time

?

# Returns the number of seconds since the

# Unix Epoch, January 1st 1970

print(time.time())?

?

# Converts a number of seconds to a date object

print(date.fromtimestamp(454554))?

要查看或添加评论,请登录

VEERAMANIKANDAN S的更多文章

  • A Tough Fight in the Anna University Semi-Final: A Chapter to Remember

    A Tough Fight in the Anna University Semi-Final: A Chapter to Remember

    As I, the captain of the SNSCE Cricket Team, bid farewell to the team after an unforgettable journey, I can’t help but…

  • .NET Framework

    .NET Framework

    For the newer cross-platform framework, see .NET.

  • c#

    c#

    During the development of the .NET Framework, the class libraries were originally written using a managed code compiler…

  • DEVOPS

    DEVOPS

    DevOps is a collection of two words, ” Development ” and ” Operations ,” representing a cultural approach that…

  • DATA STRUCTURES IN PYTHON

    DATA STRUCTURES IN PYTHON

    Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory…

  • WEB SCRAPING

    WEB SCRAPING

    Web scraping, web harvesting, or web data extraction is data scraping used for extracting data from websites.[1] Web…

    1 条评论
  • AI-Driven Analytics Is Essential For Data-Driven Decision-Making

    AI-Driven Analytics Is Essential For Data-Driven Decision-Making

    Businesses today are relying on analytics powered by artificial intelligence (AI) as a “must have” when it comes to…

  • Ethical Considerations in Data Analytics

    Ethical Considerations in Data Analytics

    Data analysis plays a crucial role in understanding the world around us and making informed decisions. As our reliance…

  • AI and Predictive Analytics

    AI and Predictive Analytics

    AI, or Artificial Intelligence, refers to the simulation of human intelligence in machines that are programmed to think…

  • Unveiling Insights

    Unveiling Insights

    Abstract: Crowdfunding, facilitated by platforms like Kickstarter, has revolutionized fundraising by enabling…

社区洞察

其他会员也浏览了