0% found this document useful (0 votes)
3 views3 pages

Chapter 13 Introduction To Python Modules Notes

The Python Standard Library is a comprehensive collection of built-in functions and modules that streamline programming tasks, reducing errors and saving time. It includes modules for mathematical operations, random number generation, and statistical analysis, which require importing to use. Programmers can import multiple modules or specific functions to enhance their coding efficiency.

Uploaded by

raidislive2
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)
3 views3 pages

Chapter 13 Introduction To Python Modules Notes

The Python Standard Library is a comprehensive collection of built-in functions and modules that streamline programming tasks, reducing errors and saving time. It includes modules for mathematical operations, random number generation, and statistical analysis, which require importing to use. Programmers can import multiple modules or specific functions to enhance their coding efficiency.

Uploaded by

raidislive2
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

Python Standard Library

The Python Standard Library is a huge collection of pre-written code, functions, and
modules that help programmers perform tasks without writing everything from scratch.

It contains:

1. Built-in functions (like input(), print(), len(), sum(), max(), min() etc.)

2. Built-in modules (math, random, statistics, datetime, os, etc.)

Why do we need the Standard Library?

• Saves time (ready-made functions)


• Reduces errors (tested and reliable)
• Makes programming easier
• Supports different tasks (math, statistics, system operations, random numbers,
files, etc.)

Two parts of the Standard Library

Built-in Functions

• Loaded automatically
• Can use them without import statement

Example:

x = abs(-9) # returns 9

Modules

• Organized groups of related functions


• Not loaded automatically
• Need to import before use

Example:

import math

[Link](16)

Modules

Modules are Python files that contain several functions grouped by purpose.
To use them, we import them.

math Module

Used for mathematical operations that go beyond +, −, *, /.

How to import

import math

Popular functions inside math

Function. Use Example. Output

[Link](x) next highest integer [Link](4.3) 5

[Link](x) next lowest integer [Link](4.8) 4

[Link](x) square root [Link](25) 5.0

[Link](x,y) xⁿ using float math [Link](2,3) 8.0

[Link](n) factorial [Link](5) 120

[Link](x) absolute value [Link](-3.5) 3.5

[Link](x,y) greatest common divisor [Link](12,18) 6

random Module

• Used to generate random numbers, especially for:


• Games
• Simulations
• Lucky draw programs
• Random selection (MCQs, quizzes)
Common functions
Function Use Example Output

[Link]() float between 0 and 1 [Link]() 0.4534...

[Link](a,b) random integer between a & b [Link](1,5) 3

[Link](n) random number between 0 and n [Link](10) 7

[Link](a,b) random number between a & b [Link](2,6) 4

statistics Module

• Used for mathematical statistics, especially in:


• Data analysis
• Charts
• Averages
• Educational scoring systems

Important functions

Function Meaning Example Output

[Link](x) Average [Link]([2,4,6]) 4

[Link](x) Middle value [Link]([2,5,7]) 5

[Link](x) Most repeated value [Link]([3,3,4,5]) 3

How to import multiple modules

import math, random

How to import specific functions only

from math import sqrt, ceil

Then call directly:

sqrt(25)

• Module is loaded only once


• Even if imported many times, Python loads it once to save memory.

You might also like