0% found this document useful (0 votes)
9 views6 pages

C11 Modules

The document provides an introduction to Python modules and libraries, explaining their definitions and functionalities. It covers various Python libraries such as math, random, statistics, and their respective functions, along with the concepts of modules, packages, and how to import them. Additionally, it includes examples of using specific functions from these libraries to perform mathematical and statistical operations.

Uploaded by

mjinx3771
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)
9 views6 pages

C11 Modules

The document provides an introduction to Python modules and libraries, explaining their definitions and functionalities. It covers various Python libraries such as math, random, statistics, and their respective functions, along with the concepts of modules, packages, and how to import them. Additionally, it includes examples of using specific functions from these libraries to perform mathematical and statistical operations.

Uploaded by

mjinx3771
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

SNV GLOBAL SCHOOL

COMPUTER SCIENCE (083)


INTRODUCTION TO PYTHON MODULES

Class:XI
NAME:…………………..

What is a library?
A Python Library is a collection of pre-written modules and packages that
provide ready-made functions and classes for performing specific tasks.
Examples of Python libraries include:
• math – provides mathematical functions for different types of calculations
• cmath – provides mathematical functions for complex numbers
• random – provides functions for generating random numbers
• statistics – provides functions for statistical calculations such as mean, median,
etc.
• Urllib – provides functions for URL handling
• Numpy – provides functions for numerical python involving arrays.
• Scipy – provides functions for scientific calculations.
• Tkinter – provides traditional python user interface toolkit to create GUI based
applications.
• datetime – for date and time handling
• pandas – provides functions for data analysis and manipulation, especially with
tabular or labeled data.
• matplotlib – provides functions for creating static, animated, and interactive data
visualizations and graphs.

What is a Module?
Module is a single Python file containing docstrings, variables and constants,
classes, objects, statements and functions related to a particular task.
Example:
File: [Link]
#user defined functions
def add(a, b):
return a + b

def sub(a, b):


return a - b

Usage:
import mymodule
print([Link](10, 5))
Docstrings
A docstring (documentation string) is a string literal that appears as the first
statement in a module, function, class, or method definition.
Docstrings are written using triple quotes (""" ... """ or ''' ... '''), and can span across
Eg:

Variables
Named memory locations used to store data values that can change during program
execution.
Constants
Fixed values that do not change during the execution of a program.
Classes:
Blueprints or templates for creating objects that define data (attributes) and
behaviour (methods).
Objects
Instances(copy) of a class that contain real data and can use the class’s methods.
Statements
Individual instructions that a Python interpreter can execute (like assignments,
loops, or function calls).
Functions
Named blocks of reusable code that perform a specific task when called.

What is a Package?
A package is a collection of related modules organized in a directory structure.
It helps in grouping modules logically and avoids naming conflicts.
A package must contain an __init__.py file that tells Python the folder is a package.

Importing modules in a python program


import statement is used to import modules in a program.
Importing entire module
To import the entire module, the following commands are used.
Syntax:
import <module name>
or
from <module name> import *
Eg:
import math # import single module
import math, time # import multiple modules
To access one of the functions in the imported module, we must specify the
name of the module and the name of the function, separated by dot notation.
import math
print([Link])

Importing select objects from a module


from command can be used to import selected objects from a module.
Syntax:
from <module name> import <object name>
Eg:
from math import pi
The above command imports only pi from math module

Python’s processing of import module command


Processing of import module command
import math
• When import <module> is used, the entire module’s code is available to
the importing program.
• A new namespace is created for the module, named the same as the
module.
• To use a function or variable from the module, you must prefix it with the
module name, like:

Processing of from module import object command


from math import pi
• When from <module> import is used, only the specified functions or
variables are imported into the current program.
• No new namespace is created; the imported items are added to the current
namespace.
• The module’s name is not required to access the imported items.
Working with math module

To use math functions in Python program, we need to import math module.

Function Syntax Description Example Output

pow pow(a, b) Returns (a^b) pow(2, 3) 8.0

Returns square root


sqrt sqrt(x) sqrt(25) 5.0
of x

floor floor(x) Greatest integer ≤ x floor(4.9) 4

ceil ceil(x) Smallest integer ≥ x ceil(4.1) 5

Absolute value
abs abs(x) abs(-7) 7
(int/float)
Absolute value (float
fabs fabs(x) fabs(-7.5) 7.5
only)
Rounds to nearest
round round(x) round(4.6) 5
integer

exp exp(x) Returns (e^x) exp(1) 2.718

log log(x) Natural logarithm log(e) 1.0

log10 log10(x) Base-10 logarithm log10(100) 2.0


Function Syntax Description Example Output

sin sin(x) Sine (radians) sin(0) 0.0

cos cos(x) Cosine (radians) cos(0) 1.0

tan tan(x) Tangent (radians) tan(0) 0.0

Converts radians →
degrees degrees(x) degrees(π) 180.0
degrees
Converts degrees →
radians radians(x) radians(180) 3.1416
radians
Returns maximum
max max(a, b) max(4, 9) 9
value
Returns minimum
min min(a, b) min(4, 9) 4
value

Working with random module


To use random number generators in Python program, we need to import
random module.

Output
Function Syntax Description Example
(Example)

Returns a random
random() random() float between 0.0 random() 0.47
and 1.0
Returns random
randint() randint(a, b) integer between a randint(1, 6) 3
and b (inclusive)
Output
Function Syntax Description Example
(Example)

Returns random
randrange(start, randrange(2,
randrange() number from 6
stop, step) 10, 2)
given range
Returns random
uniform(1.5,
uniform() uniform(a, b) float between a 2.6
3.5)
and b
Returns random
choice() choice(sequence) element from choice("CS") C
list/tuple/string
Randomly
List order
shuffle() shuffle(list) rearranges list shuffle(L)
changes
elements

Working with statistics module


To use statistics module in Python program, we need to import statistics
module.

Function Syntax Description Example Output


Returns arithmetic mean
mean() mean(data) mean([2,4,6]) 4
(average)

median() median(data) Returns middle value median([1,3,5]) 3

mode() mode(data) Returns most frequent value mode([1,2,2,3]) 2

variance() variance(data) Returns variance (sample) variance([2,4,6]) 4

Returns standard deviation


stdev() stdev(data) stdev([2,4,6]) 2
(sample)

You might also like