0% found this document useful (0 votes)
30 views18 pages

Modules

The document provides an overview of Python modules, explaining their purpose in organizing code and promoting reusability. It covers how to import modules, the syntax for import statements, and highlights specific modules like 'math' and 'random' along with their functions. Additionally, it details how to access functions from these modules and generate random numbers.

Uploaded by

patidaranshu51
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)
30 views18 pages

Modules

The document provides an overview of Python modules, explaining their purpose in organizing code and promoting reusability. It covers how to import modules, the syntax for import statements, and highlights specific modules like 'math' and 'random' along with their functions. Additionally, it details how to access functions from these modules and generate random numbers.

Uploaded by

patidaranshu51
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

Programming in Python

Ms. Priyanka Parmar


Computer Application
Medicaps University,Indore
Module
 Modules refer to a file containing Python
statements and definitions. A file containing
Python code, for example: example.py , is
called a module, and its module name would be
example . We use modules to break down large
programs into small manageable and organized
files. Furthermore, modules provide reusability
of code. We can define our most used functions
in a module and import it, instead of copying
their definitions into different programs.
Priyanka Parmar Medicaps University,Indore
Module
 As we know that the Python interactive shell has a
number of built-in functions. As a shell start, these
functions are loaded automatically and are always
available, such as,print() and input() for I/O, Number
conversion functions such as int(), float(),
complex(),Data type conversions such as list(), tuple(),
set(), etc.
 In addition to these many built-in functions, there are
also a large number of pre-defined functions available
as a part of libraries bundled with Python distributions.
These functions are defined in modules which are
known as built-in modules.
Priyanka Parmar Medicaps University,Indore
Module
 To display a list of all of the available modules in
Python Programming Language, we can use the
following command in the Python console:
 help('modules')

Priyanka Parmar Medicaps University,Indore


Importing Modules
 To make use of the functions in a module, you’ll need
to import the module with an import statement.
 Import in python is similar to #include header_file in
C/C++.
 Python modules can get access to code from another
module by importing the file/function using import.

Priyanka Parmar Medicaps University,Indore


Importing Modules
 An import statement is made up of the import keyword
along with the name of the module.
 In a Python file, this will be declared at the top of the
code.

Priyanka Parmar Medicaps University,Indore


import statement
 import module_name
 When the import is used, it searches for the module
initially in the local scope by calling __import__()
function.
 Eg.
 import math
 print(math.pi)
 In the above code module, math is imported, and its
variables can be accessed by considering it to be a
class and pi as its object. The value of pi is returned by
__import__().
Priyanka Parmar Medicaps University,Indore
import statement
 pi as a whole can be imported into our initial
code, rather than importing the
whole module.
 from math import pi
 print(pi)
Output:-
 3.141592653589793

Priyanka Parmar Medicaps University,Indore


import statement
 from module_name import *
 In the above code module, math is not imported, rather
just pi has been imported as a variable. All the
functions and constants can be imported using *.
 from math import *
 print(pi)
 print(factorial(6))
 As said above import uses __import__() to search for
the module, and if not found, it would raise
ImportError

Priyanka Parmar Medicaps University,Indore


moduls

Priyanka Parmar Medicaps University,Indore


math module
 The math module is a standard module in
Python and is always available.
 To use mathematical functions under this
module, you have to import the module using
import math
 It gives access to the underlying C library
functions.
 This module does not support complex
datatypes. The cmath module is the complex
counter part.
Priyanka Parmar Medicaps University,Indore
Imp. Functions in Python Math Module

Priyanka Parmar Medicaps University,Indore


Random module
 Python has a built-in module that you can use to make
random numbers.
 This module defines several functions to generate
random numbers.
 We can use these functions while developing games,
in cryptography and to generate random numbers on
fly for authentication.
 Some of the random functions are:
 random() function: This function always generate
some float value between 0 and 1 (not inclusive) 0<x

Priyanka Parmar Medicaps University,Indore


.

Priyanka Parmar Medicaps University,Indore


functions in Random
 randint()
function: To generate random integer
beween two given numbers(inclusive)

Priyanka Parmar Medicaps University,Indore


functions
 randrange([start],stop,[step]): returns a random
number from range start<= x < stop
 start argument is optional and default value is 0
 step argument is optional and default value is 1
 randrange(10)-->generates a number from 0 to 9
 randrange(1,11)-->generates a number from 1 to 10
 randrange(1,11,2)-->generates a number from
1,3,5,7,9

Priyanka Parmar Medicaps University,Indore


.

Priyanka Parmar Medicaps University,Indore


Functions in random

 choice()function: It won’t return random


number. It will return a random object from the
given list or tuple.

Priyanka Parmar Medicaps University,Indore

You might also like