0% found this document useful (0 votes)
6 views8 pages

Module Python

A module in Python is a file containing reusable code such as functions, classes, and variables, which can be imported into other programs. The document explains how to use modules, including importing them in various ways, creating custom modules, and highlights common standard library modules like math and sys. The sys module specifically provides access to system parameters, command-line arguments, and allows for input/output control, making it essential for interacting with the Python runtime environment.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Module Python

A module in Python is a file containing reusable code such as functions, classes, and variables, which can be imported into other programs. The document explains how to use modules, including importing them in various ways, creating custom modules, and highlights common standard library modules like math and sys. The sys module specifically provides access to system parameters, command-line arguments, and allows for input/output control, making it essential for interacting with the Python runtime environment.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module

A module is a Python file containing functions, classes, and variables


that can be reused in other programs.​

It helps in code reusability, modularity, and better organization of large


programs.​

Example: math, random, csv are Python modules.

2. How to Use a Module


Modules can be used with the import statement:

import math
print([Link](25)) # using a function from math module

#Different ways of importing:

import module_name​

import math
print([Link])

#from module import function​



from math import sqrt
print(sqrt(49))

#import with alias​



import math as m
print([Link](0))

Names of Some Common Modules


●​ math → mathematical functions (sqrt, pow, factorial)​

●​ random → random number generation​

●​ datetime → date & time operations​

●​ os → operating system functions​

●​ sys → system-specific functions (like command-line arguments)​

●​ csv → handling CSV files​

●​ json → working with JSON data

Making Your Own Module


Steps:

1.​Create a new file with extension .py (e.g., [Link]).​

Write functions/variables inside it.​



# [Link]

def greet(name):

return f"Hello, {name}!"


Import and use it in another file:

import mymodule

print([Link]("Python"))

Name of the Module


Everymodule has a [Link] can find name of a module by using name attribute of the
module.

Example:print("Hello World")
print(__name__)

The dir() function

Standard Library Modules in Python


Python has a large standard library (comes pre-installed). Examples:

●​ math – Mathematical functions​

●​ statistics – Mean, median, mode​

●​ random – Random numbers​

●​ datetime – Date and time​

●​ os – File and directory handling​


●​ sys – System-related operations​

●​ re – Regular expressions​

●​ csv – Reading/writing CSV files​

●​ json – Working with JSON data​

●​ collections – Specialized data structures (Counter, deque, etc.)

In short:

●​ Module = Python file with reusable code.


●​ Use = Import with import / from.
●​ Examples = math, os, sys.
●​ Own module = Write functions in a .py file and import it.
●​ Standard Library = Built-in set of modules shipped with Python.

SYS MODULE
The sys module in Python provides access to system-specific parameters and
functions, allowing interaction with the Python interpreter and its runtime environment. It
is a built-in module, meaning it is always available and does not require installation.

●​ sys module provides access to variables and functions that interact


closely with Python interpreter and runtime environment.
●​ It allows developers to manipulate various aspects of program
execution and interpreter itself.

Why do we need the sys module?


1.​ Gives access to system-specific parameters and functions like

command-line arguments.
2.​ Allows interaction with Python runtime environment (e.g., path,

version, exit).

3.​ Useful for reading input and writing output using [Link] and

[Link].

4.​ Enables control over the interpreter with functions like [Link]() and

[Link]().

5.​ Helps in debugging and managing system-level behavior in Python

scripts.

Input and Output using sys


The sys module controls program input, output and error streams, enabling
precise data handling beyond standard input and print functions.

[Link]
Writes output to the standard output stream and allows low-level control
over printed output.
import sys
[Link]('PYTHON')

[Link]:

Reads input directly from the standard input stream and supports reading
multiple lines or redirected input.

import sys

for line in [Link]:

print(f'Input : {line}')
print("Exit")

Command-Line Arguments
Command-line arguments are those which are passed during the calling of
the program along with the calling statement. To achieve this using the sys
module, the sys module provides a variable called [Link]. It's main purpose
are:

●​ It is a list of command-line arguments.

●​ len([Link]) provides the number of command-line arguments.

import sys

n = len([Link])

print("Total arguments passed:", n)

print("Name of Python script:", [Link][0])

for i in range(1, n):

print([Link][i], end=" ")

Sum = 0

for i in range(1, n):

Sum += int([Link][i])

print(Sum)
Exiting the Program

[Link]([arg]) can be used to exit the program. The optional argument arg
can be an integer giving the exit or another type of object. If it is an integer,
zero is considered "successful termination".

import sys
age = 17
if age < 18:
[Link]("Age less than 18")
else:
print("Age is not less than 18")

Working with Modules


[Link] is a list in the sys module that defines directories Python searches
for modules after checking built-ins. As a regular list, it can be modified at
runtime to add, remove or reorder paths.

import sys
print([Link])

This code will print the system paths that Python uses to search for modules.
The [Link] list contains the directories that Python will search for modules
when it imports them.

[Link]()
[Link] is a dictionary that contains all the modules currently imported
in the Python interpreter. The keys are module names, and the values are the
corresponding module objects.

Example:
import sys
print([Link])

You might also like