Python Programming Lecture Modules
Python Programming Lecture Modules
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
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
5
EXAMPLE
Considering same module as we considered in previous example
module1.p
y
6
IMPORTING MODULES
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
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