0% found this document useful (0 votes)
49 views17 pages

Python Programming Lecture Modules

Modules allow programmers to organize Python code into reusable groups. A module is a .py file that defines functions, classes, and variables to encapsulate related code. Importing modules makes code more maintainable, reusable, and avoids naming collisions. Common Python modules include math, random, and datetime for numerical and random functions and date/time operations.

Uploaded by

onesnone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views17 pages

Python Programming Lecture Modules

Modules allow programmers to organize Python code into reusable groups. A module is a .py file that defines functions, classes, and variables to encapsulate related code. Importing modules makes code more maintainable, reusable, and avoids naming collisions. Common Python modules include math, random, and datetime for numerical and random functions and date/time operations.

Uploaded by

onesnone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Python Programming Lecture

MODULES

1
MODULES
 A module is a file containing Python code. 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. Smaller, more manageable subtasks of
applications are termed as modules.
Advantages to modularizing code in a large application:
▪ Simplicity: a module typically focuses on one relatively small portion of the
problem. This makes development easier and less error-prone.
▪ Maintainability: Modules are typically designed so that they enforce logical
boundaries between different problem domains so that they can be managed easily.
▪ Reusability: Functionality defined in a single module can be easily reused by other
parts of the application.
▪ Scoping: Modules typically define a separate namespace, which helps avoid
collisions between identifiers in different areas of a program.
2
IMPORTING MODULES

1. Using import Statement


Importing a module requires the use of the import
statement, whose syntax is:
import module1
import module2
:
import moduleN

It is also possible to import multiple modules on


the same line like this . . .
import module1[, module2[,... moduleN]]

3
EXAMPLE
For example, suppose you have created a file called module1.py containing one list,function
and a class.
module1.py

Then call module1 attributes one by one which can be accessed by importing the module as
follows:

4
IMPORTING MODULES

2. Using from-import Statement


It is possible to import specific module elements into your own module.
from-import brings the name into the current namespace, meaning that
you do not use the attribute/dotted notation to access the module
identifier. For this purpose, we can use the from-import
statement,whose syntax is:
from module import name1[, name2[,... nameN]]
It is also possible to import all the names from the module into the current
namespace using the following from-import statement:
from module import *

5
EXAMPLE
Considering same module as we considered in previous example
module1.p
y

• Following execution of from-import statement , allow <name(s)>  to be referenced in the caller’s


environment without the <modulename>

6
IMPORTING MODULES

3. Using Multi line Import


The multi-line import feature was added specifically for long from-import
statements. When importing many attributes from the same module, import
lines of code tend to get long and wrap, requiring a NEWLINE escaping
backslash.
Example

Your other option is to have multiple from-import statements:

7
Math Module
To build financial or scientific projects it becomes necessary to implement
mathematical calculations in the project.
Python provides the math module to deal with such calculations.
Math module provides constants and functions to deal with both basic operations
such as addition(+), subtraction(-), multiplication(*), division(/) and advance
operations like trigonometric, logarithmic, exponential functions.

8
Constants provided by the math
module
Math module provides various the value of various constants like pi, tau is
basically 2*pi. Having such constants saves the time of writing the value of each
constant every time we want to use it and that too with great precision. Constants
provided by the math module are – 
 Euler’s Number Output
 Pi
 Tau
 Infinity
 Not a Number (NaN)

9
10
Numeric Functions
Output

Logarithmic and Power Functions


Output

11
Trigonometric and Angular Functions

12
Random Module
Python Random module is an in-built module of Python which is used to
generate random numbers. These are pseudo-random numbers means these are
not truly random. This module can be used to perform random actions such as
generating random numbers, print random a value for a list or string, etc.

13
Built in functions in random module

14
Random Functions
1. randint()
This function generates an integer between the specified limits. It takes two
arguments x and y and produces integer i such that x <= i <= y.

2. randrange()
This function takes 2 arguments start and stop along with 1 optional argument step.
And returns a random integer in the range(start, stop, step). The default value of step
is 1.

15
Random Functions
3. random()
This function returns a random floating-point number between 0 and 1 (excluding 0
and 1).

4.  uniform()
It takes two arguments to start and stop and then returns a float between the start and
stop (including the limits).

5. choice()
This function is used to choose a random element from a specific sequence,

16
Random Functions
6. shuffle()
This function takes one argument – a list. It then shuffles the elements of the list in place and returns
None.

7. seed()
This function is used when you need to generate the same sequence of random numbers multiple times.
It takes one argument- the seed value.
This value initializes a pseudo-random number generator. Now, whenever you call the seed() function
with this seed value, it will produce the exact same sequence of random numbers.

17

You might also like