Skip to main content

Modules of Python & Random Module | Python Programming By Akash

 1.   Modules:

Definition: It is a file or a 'toolbox' containing written code (like functions and variables) that we can use in our own program. Instead of writing complex code from scratch (like advanced math or random number generation), we simply 'import' these pre-made toolboxes. This keeps our main code clean and small.

How to Use a Module:

a.   Import the Whole Box (Safest) You bring the entire toolbox (By using import module name). To use a tool, you must say "Toolbox-Name dot Tool".

b.   Import Specific Tools (Faster) You only bring one specific tool from the box. You don't need to say the toolbox name anymore (By using from module name import tool name).

c.   Import with a Nickname (Common) You bring the box but give it a short name to save typing (By using import module name as nickname).

2.    Packages:

Definition: It is a term used to describe a directory (or folder) that contains a collection of multiple Modules. While a Module is a single file of code, a Package is a whole folder full of files. This allows us to organize very large amounts of code into separate sections, just like organizing files into folders on your desktop.

How to Use a Package:

a.   Import the Package (Safest) You bring the entire package (By using import package name). To use a tool, you must say "Pack-Name dot File-Name dot Tool".

b.   Import Specific File (.) You only bring one specific File from the Pack (By using import package name.file name). To use a tool, you must say "Pack-Name dot File-Name dot Tool".

c.   Import Specific File (from) You only bring one specific File from the Pack (By using from package name import file name). To use a tool, you must say "File-Name dot Tool".

d.   Import Special Package with __inti__.py You bring the entire package (By using import package name). To use a tool, you must say "Pack-Name dot Tool".

e.   Import Special Package with __inti__.py Tool You only  bring one specific tool from the Pack (By using from package name import Tool Name).

  • Overview Image:

3.   Random Module:

Definition: It is a built-in module in Python used to generate random numbers or make random choices. Since computers are designed to be exact and follow rules, they cannot naturally be 'random'. We import this module to simulate luck, chance, or unpredictability, which is essential for building games (like rolling dice) or shuffling data.

1.1.  Radiant Function:

Definition: It is a specific function inside the random module that stands for 'Random Integer'. It is used to generate a random whole number between two given values. The most important rule to remember is that it is inclusive, meaning both the start number and the end number can be chosen.

The Syntax: random.randint(a, b)

·  a: The lowest possible number.

·  b: The highest possible number.

For Example,

1.2.   Random Function

Definition: It is the base function inside the random module used to generate a random floating-point number (decimal). It always picks a value between 0.0 and 1.0. It is useful for generating percentages or probabilities (like a 50% chance).

The Range Rule:

·  Minimum: 0.0 (Inclusive - It can be 0).

·  Maximum: 1.0 (Exclusive - It never actually reaches 1.0, it stops at 0.999...).

For Example,

1.3.  Uniform Function:

Definition: It is a function inside the random module used to generate a random floating-point number (decimal) between two specific numbers. While random() is stuck between 0 and 1, uniform() allows you to choose your own range (like 10.5 to 20.5).

The Syntax: random.uniform(a, b)

·  a: The starting limit.

·  b: The ending limit.

·  Result: A precise decimal number anywhere between a and b.

For Example,


  • Overview Image:

Comments