1.
Modules in Python
A module is a Python file (.py) that contains functions, variables, or classes.
uses of modules :
Organize code
Reuse code
Avoid writing everything from scratch
Example:
import math
Now you can use everything inside the math module.
2. Random Numbers (random module)
The random module is used to generate pseudo-random numbers.
Common functions
Function Example Output / Meaning
[Link]() [Link]() Float between 0.0 to <1.0
[Link](a, b) [Link](1, 6) Integer between a and b
(inclusive)
[Link](seq) [Link]([10,20,30]) Random element from a
list/string
Example code
import random
print([Link]())
print([Link](1,10))
print([Link](['A', 'B', 'C']))
output:
0.4738291049273
Explanation
1. [Link]()
Gives a floating-point number between 0.0 and 1.0 (not including 1)
Example: 0.4738291049273
2. [Link](1,10)
Gives an integer between 1 and 10 (both included)
Example: 7
3. [Link](['A','B','C'])
Chooses one value randomly from the list
Example: B
3. The time module
This module provides functions related to time.
Common functions:
Function Example Meaning
[Link]() [Link]() Returns current time in
seconds since epoch
[Link](seconds) [Link](2) Pauses program for given
seconds
[Link]() [Link]() Converts time into readable
format
Example
import time
print([Link]())
[Link](2)
print("Program paused for 2 seconds")
output:
1733651104.5483925
Program paused for 2 seconds
Explanation
1. [Link]()
Prints the current time in seconds since 1 Jan 1970 (Epoch time).
Example output:
1733651104.5483925
(This number changes every second.)
2. [Link](2)
The program waits 2 seconds before printing the next line.
3. print("Program paused for 2 seconds")
After 2 seconds delay, it prints:Program paused for 2 seconds
5. Creating Your Own Modules
You can create your own module using any .py file.
Step 1: Create a file
[Link]
def greet(name):
return "Hello " + name
This file contains one function:
greet(name) → returns a gree ng message.
Step 2: Import it in another file
import mymodule
print([Link]("xyz"))
What happens when this runs?
Python reads the file [Link].
import mymodule brings the file into the program.
[Link]("xyz") calls the greet() function.
Output
Hello xyz
6. Namespaces in Python
A namespace is a place where names (variables, functions, classes) are stored.
Examples of namespaces:
Local namespace → inside a func on
Global namespace → module-level
Built-in namespace → Python keywords like len, print
L — LocalVariables inside a function.
E — Enclosing Variables inside the outer function, when functions are nested.
G — Global Variables defined at the top level of the file.
B — Built-in Python keywords or built-in functions like:len, print, max, etc
Example:
x = "global"
def outer():
x = "enclosing"
def inner():
x = "local"
print(x)
inner()
outer()
op: local
Step-by-Step Explanation (LEGB Rule)
1 ⃣ Global Scope
x = "global"
This variable belongs to the entire file.
But it will not be used inside the inner function because more local variables exist.
2 ⃣ Call outer()
When outer() is called, the function starts executing.
Inside outer():
x = "enclosing"
This creates a variable inside outer, but outside inner.
This is called the Enclosing scope.
3 ⃣ Define inner()
Inside inner:
x = "local"
print(x)
Here the variable x = "local" is defined inside the function, so it is Local scope.
4 ⃣ Python follows LEGB lookup rule
When Python needs to find the value of x inside inner(), it searches:
L → Local → ✔ x = "local" (FOUND!)
E → Enclosing → "enclosing" (not needed)
G → Global → "global" (not needed)
B → Built-in → (never reached)
8. Attributes and Dot Operator
✔ Attribute
A value attached to an object (variable or method).
✔ Dot operator (.)
Used to access attributes of:objects,classes,modules
Types of attributes
[Link] Attribute:Shared by all objects of the class
Access it using class name
Example: [Link]
Instance Attribute
Different for each object
Access it using object name
Example: [Link]