0% found this document useful (0 votes)
23 views15 pages

Using Python Libraries and Modules

Uploaded by

ajtemp17
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)
23 views15 pages

Using Python Libraries and Modules

Uploaded by

ajtemp17
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

Using With Python Libraries

Library: - Library is a collection of modules (and packages) that together


cater to a specific type of applications or requirements.
Modules: - The act of partitioning a program into individual
components (known as modules) is called modularity.
A module is a separate unit in itself.
Advantage of module: -
• It reduces its complexity to some degree.
• It creates a number of well defined, documented boundaries within
the program.
• Its contents can be reused in other programs, without having to
rewrite or recreate them.

Python module: - A python module is a file (.py file) containing


variables, class definitions, statement and functions related to a
particular task.
• The python module that come preloaded with python are called
standard library modules.

Importing Modules in a Python Program


Python provides import statement to import modules in a program. The
import statement can be used in two forms.
(1) To import entire module: the import <module> command
(2) To import selected objects from a module: the from <module>
import <object> command

Importing Entire Module: -


The imports statement can be used to import entire module and even

By PathWala / PortalExpress-
for importing selected items.
To import entire module syntax is -
import <module 1 >, <module 2>....
For example:
import time
import decimals, fractions

Dot notation :- After importing a module, to access one of the


functions, you have to specify the name of the module and the name of
the function, separated by a dot (also known as a period) this format is
called dot notation.
Syntax: -
<module-name>.<function-name> ()
This way of referring to a module's object is called dot notation.
For example:
import math
[Link](16)

• You can give alias name to imported module as


Syntax :
import <module> as <alias name>

For Example :-

import math as a
[Link] (16)

Importing Select Objects from a Module: -


If you want to import some selected items, not all from a module, then
you can use following syntax:-

By PathWala / PortalExpress-
from <module name >import<object name>

For example:
from math import sqrt

To Import Multiple Objects:-


If you want to import multiple objects from the module then you can
use following syntax :-
from <module name >import<object name>,<object name>,<object
name>....

For example: -
from math import sqrt, pi, pow

USING PYTHON STANDARD LIBRARY'S FUNCTIONS AND MODULES: -


Python's standard library offers many built in functions and modules for
specialized type of functionality.

For example:-
len(),str(),type()
math module, random module , etc.

Using python's built in functions: -


The python interpreter has a number of functions built into it that are
always available, you need not import any module for them.

By PathWala / PortalExpress-
By PathWala / PortalExpress-
By PathWala / PortalExpress-
Python's built in string functions: -
That are ---

• <str>.join (<string iterable>) - Joins a string or character after each


member of the string iterator.

(I) If the string based iterator is a string then the <str> is inserted after

By PathWala / PortalExpress-
every character of the string.
For example:
>>>"***". join ("Hello")
'H***e***l***l***o'

(ii) If the string based iterator is a list or tuple of string then, the given
string / character is joined with each member of the list or tuple, But
the list or tuple must have all member as strings otherwise Python will
raise an error.

>>>"***". join (("Hello", "Python"))


'Hello***Python'

>>>"***". join (["Hello", "Python", "Language"])


'Hello***Python***Language'

>>>"***". join ((123,"Hello","Python"))


Error

• <str>. split (<string/char>) - Split a string based on given string or


character and return a list containing split strings as members.

(i) If you do not provide any argument to split then by default it will
split the give string considering whitespace as a separator.

For example:
>>>"I Love Python". split()
['I', 'Love', 'Python']

(ii) If you provide a string or a character as an argument to split (),then

By PathWala / PortalExpress-
the given string is divided into parts considering the given
string/character as separate and separator character is not included in
the split string.

For example:
>>>"I Love Python". split ("o")
['I L','ve Pyth','n']

• <str>. replace (<word to be replaced>,<replace word>) - Replaces a


word or part of the string with another in the given string <str>.

For example:
>>>"I Love Python". replace ("Python", "Programming")
>>>"I Love Programming"
USING RANDOM MODULE: -
Python has a module namely random that provides random number
generators.
To use random number generators in your Python program, you first
need to import module random using any import command
import random

Some most common random number generator functions in random


module are:

random () : - It returns a random floating point number N in range


[0.0,1.0] , i.e., 0.0 ≤ N ≥ 1.0.

randint (a, b) : - It returns a random integer N in the range (a, b) , i.e. , a


≤ N ≤ b (both range-limit are inclusive).

By PathWala / PortalExpress-
[Link](a, b) : - It returns a random floating point number N
such that
a ≤ N ≤ b for a ≤ b and
b ≤ N ≤ a for b < a and

[Link](stop) or [Link](start, stop, [ steps]) : - It


returns a randomly selected element from rang ( start, stop, step ) .

USING STRING MODULE: -


Python has a module by the name string that comes with many
constant and classes.
If you want to use string module, then you must import it by using
import command: -
Like that: -

import string

By PathWala / PortalExpress-
For example: -
>>>import string
>>>[Link]
'0123456789'

CREATING A PYTHON LIBRARY: -

Package: - A package is a collection of Python modules under a


common namespace, created by placing different modules on a single
directory along with some special file such (__init__.py).
• A library can have one or more packages and sub-packages.

Steps of making package: -

By PathWala / PortalExpress-
 At first you have known a path where all files of python saved.
You can find the path of python by following command as shown in
figure:-

 Copy that path and paste in computer paths like this: -

 Make a file (package name). For example we make pathwala


(package) folder.

By PathWala / PortalExpress-
 Now make another folder (Sub Package) in package folder; if you
want
For example we make portal_express (sub package)

 Now make an empty module with name __init__.py in


portal_express (sub package)
And save that module like that:-

By PathWala / PortalExpress-
Like that:-
 Similarly also, make an empty module with name __init__.py in
pathwala(package)

 Now make modules as you want in pathwala(Package)


For example : We make volume module as shown in figure

By PathWala / PortalExpress-
And save that module in pathwala (Package)

 Now make modules as you want in portal_express(Sub Package)


For example : We make area module as shown in figure

 And save that module in portal_express (Sub Package)

By PathWala / PortalExpress-
 Now, Your package become ready to use in Python
For example as shown in figure: -

Thankyou!!!!!

For Sumita Arora Type C solution ( Programing Question ) Visit our


YouTube Channel Portal Express
For Solution of Sumita Arora visit on Path Wala

By PathWala / PortalExpress-

Common questions

Powered by AI

A Python module is a single file containing Python code, which can include variables, class definitions, functions, and statements for a specific task. It aids modularity by allowing reuse and partitioning the program into separate components . A package, on the other hand, is a collection of modules under a common namespace, allowing for organizational hierarchy. It is created by placing different modules in a single directory with a special __init__.py file . Both contribute to modularity by creating well-defined boundaries and enabling code reuse, thus reducing complexity .

Aliasing in Python modules allows a module to be imported with a different name, typically shorter or more convenient for use within a program . This is done using the syntax 'import <module> as <alias>'. For example, 'import math as m' would allow access to module math's functions using 'm' as a prefix, such as 'm.sqrt(16)' . Aliasing enhances readability and can help resolve conflicts with module names, improving code clarity and maintainability .

Python allows importing multiple specific objects from a module using the syntax 'from <module name> import <object name1>, <object name2>, ...'. This method is useful when only certain functions or variables are needed from a large module, optimizing resource usage and improving performance . For example, 'from math import sqrt, pi, pow' imports only the sqrt, pi, and pow functions from the math module, allowing direct access without module prefixing .

The 'random' module enhances Python programs by providing functions to generate pseudo-random numbers for various applications, such as simulations, games, and testing . It includes functions like 'random()', 'randint()', 'uniform()', and 'randrange()'. For example, using 'randint(a, b)' generates a random integer N where a ≤ N ≤ b . This versatility in creating randomness supports diverse programming needs, adding unpredictability where required .

Python's built-in string methods enhance string manipulation by providing powerful and flexible tools for modifying and processing strings. The 'join' method concatenates elements of a string iterator by inserting a specified string between elements, enabling creation of formatted strings easily . The 'split' method divides a string into a list based on a specified delimiter, facilitating parsing of structured text . The 'replace' method allows substituting parts of a string with a different string, which is useful for text edits and data sanitation .

Modular programming provides several advantages, including reduced complexity, improved maintainability, and reusability of code . Python supports modular programming by offering modules and packages, allowing developers to encapsulate code into separate units that can be imported and reused across different programs. By organizing code into modules, Python enables clear documentation and well-defined program boundaries . The import statement in Python facilitates the integration of these modules into larger programs, further supporting the modular programming paradigm .

The '__init__.py' file is crucial in a Python package as it denotes a directory as a Python package directory. It can be an empty file but often contains initialization code for the package . The presence of '__init__.py' differentiates packages from ordinary directories and allows modules within the package to be imported using the package's namespace . This organizational method facilitates code management and modular programming .

The 'import' statement in Python enhances program functionality by allowing the incorporation of pre-built and tested modules, reducing the need for redundant code and facilitating code reuse . It maintains program maintainability by creating clear boundaries between different functional components of a codebase, making it easier to manage, update, and document . Using dot notation, programmers can also selectively import components, ensuring that only necessary parts of a module are integrated, which optimizes performance .

Dot notation is a way of accessing functions, variables, and classes within a module in Python by using the format <module-name>.<object-name>. This is essential after importing a module to specify which part of the module is being used. For example, after importing the math module, one can access the square root function using math.sqrt(16), which returns 4.0 .

The significance of Python's standard library lies in its vast collection of built-in modules and functions that offer ready-made, tested solutions for a wide range of programming needs, from mathematical operations to network protocols . This benefits developers by reducing the need for custom implementations, thus saving time and reducing potential errors. The standard library ensures consistency across different applications and simplifies the learning curve for new developers by providing a unified set of tools . Additionally, the standard library's integration with Python enhances program performance and reliability .

You might also like