0% found this document useful (0 votes)
2 views4 pages

Using Modules in Python

Uploaded by

vishrutjoshi2008
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)
2 views4 pages

Using Modules in Python

Uploaded by

vishrutjoshi2008
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
You are on page 1/ 4

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 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
math.sqrt(16)

• You can give alias name to imported module as


Syntax :
import <module> as <alias name>

For Example :-

import math as a
a.sqrt (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:-
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: - (Previously done in class XI )


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

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 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 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).
random.uniform(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
random.randrange(stop) or random.randrange(start, stop, [ steps]) : - It returns a randomly selected
element from rang ( start, stop, step ) .

You might also like