0% found this document useful (0 votes)
17 views25 pages

Python Libraries: Usage & Differences

The document explains Python libraries, modules, and packages, detailing their definitions and differences. It provides examples of commonly used libraries such as math, random, and statistics, along with their functions and usage. Additionally, it covers import methods and includes exercises to predict outputs from provided code snippets.

Uploaded by

mamtapuri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views25 pages

Python Libraries: Usage & Differences

The document explains Python libraries, modules, and packages, detailing their definitions and differences. It provides examples of commonly used libraries such as math, random, and statistics, along with their functions and usage. Additionally, it covers import methods and includes exercises to predict outputs from provided code snippets.

Uploaded by

mamtapuri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

UT 1 Revision Work

Ch 4
Using Python Libraries
What is a Library in Python?

A library in Python is a collection of pre-written code (functions, classes, and


modules) that you can use to perform common tasks like math operations, random
number generation, or statistics — without writing the code yourself.

Example:
math library helps with mathematical calculations like square root, power, etc.
random library is used to generate random numbers.
statistics library is used to calculate mean, median, mode, etc.
What is the difference between Module, Library, and Package

Term Definition Example


A single Python file (.py)
Module that contains functions, [Link], [Link]
variables, or classes.
A collection of modules
Library that provide tools to math, statistics, numpy
perform specific tasks.
A folder containing
Package multiple modules and a numpy, pandas, email
special __init__.py file.

In Simple Terms:
•A module is like a single tool.
•A library is like a toolbox.
•A package is like a suitcase carrying many
toolboxes.
LIBRARY 1: math – Mathematical Functions

Common Functions

Function Description Example


sqrt(x)
:
Square root of x [Link](25) → 5.0
pow(x, y) x raised to the power y [Link](2, 3) → 8.0
ceil(x) Smallest integer ≥ x [Link](4.2) → 5
floor(x) Largest integer ≤ x [Link](4.8) → 4
pi Value of π [Link] → 3.14159…
Difference Between pow() in Core Python vs [Link]()
LIBRARY 2: random – Random Number Generation
comparison of the three functions: randint(), randrange(), and random() from Python's
random module
LIBRARY 3: statistics – Statistical Calculations
two common ways to import a module in Python

1. Regular Import (Import Entire Module)

import module_name
Example:
import math
print([Link](25)) # Output: 5.0

Use When:

You want access to all functions in the module.


You want to avoid name conflicts (functions are called as [Link]()).
2. Selective Import (Import Specific Function/Class)

from module_name import specific_function

Example:
from math import sqrt
print(sqrt(25)) # Output: 5.0

Use When:
•You need only one or two functions.
•You want to use functions without the module name prefix.

Caution:

You can also use:


from math import *

…but it is not recommended because:


It imports everything into the global namespace.
It may cause name conflicts.
ference Between import module and from module import

Feature import module from module import name


Only specific function(s) or
Imports Entire module
object(s)
Must use
Function Access Can use function() directly
module_name.function()
Namespace pollution Safe (no conflicts) Can cause name conflicts
Clear where the function Less clear if multiple
Readability
comes from modules used
Slightly more (entire Less (only selected items
Memory Usage
module loaded) are imported)
Predict the Output (Using Python Libraries)

import math
print([Link](64))

import math
x = 7.9
print([Link](x))
print([Link](x))

import math
a = [Link](3, 2)
b = [Link]
print(round(a, 2), round(b, 2))
import random
print([Link](1, 3))

import random
colors = ['Red', 'Green', 'Blue']
print([Link](colors))

import random
for i in range(3):
print([Link]())
import statistics
data = [5, 3, 5, 2, 5, 3]
print([Link](data))

import statistics
marks = [70, 85, 90, 75, 65]
print([Link](marks))

import statistics
lst = [4, 5, 6, 7]
print([Link](lst))
import math, random
x = [Link](10, 20)
print("Random Number:", x)
print("Square Root:", [Link](x))

import statistics, random


data = [[Link](1, 10) for _ in range(5)]
print("Data:", data)
print("Mean:", [Link](data))

import random
data = [] # Step 1: Create an empty list
for i in range(5): # Step 2: Repeat 5 times
num = [Link](1, 10) # Generate a random number
[Link](num) # Step 3: Add it to the list
print("Data:", data)
print("Mean:", [Link](data))
What are the possible outcome(s) executed from the following code? Also specify the
maximum and minimum values that can be assigned to variable NUMBER.

STRING = "CBSEONLINE"
NUMBER = [Link](0, 3)
N=9
while STRING[N] != 'L' :
print(STRING[N] + STRING[NUMBER] + '#', end = '')
NUMBER = NUMBER + 1
N=N-1
ES#NE#IO#
LE#NO#ON#
NS#IE#LO# Answer
EC#NB#IS# The outcomes are as follows:
ES#NE#IO#
EC#NB#IS#
NUMBER is assigned a random integer between 0
and 3 using [Link](0, 3). Therefore, the
minimum value for NUMBER is 0 and the maximum
value is 3.
Explanation
Consider the following code :

import random
print(int(20 + [Link]() * 5), end = ' ')
print(int(20 + [Link]() * 5), end = ' ')
print(int(20 + [Link]() * 5), end = ' ')
print(int(20 + [Link]() * 5))
Find the suggested output options 1 to 4. Also, write the least value
and highest value that can be generated.

20 22 24 25
22 23 24 25
23 24 23 24
21 21 21 21
Answer
23 24 23 24
21 21 21 21
The lowest value that can be
generated is 20 and the highest value
that can be generated is 24.
Consider the following code :

import random
print(100 + [Link](5, 10), end = ' ' )
print(100 + [Link](5, 10), end = ' ' )
print(100 + [Link](5, 10), end = ' ' )
print(100 + [Link](5, 10))
Find the suggested output options 1 to 4. Also, write the least value and
highest value that can be generated.

102 105 104 105


110 103 104 105
105 107 105 110
110 105 105 110 Answer
105 107 105 110
110 105 105 110
The least value that can be generated
is 105 and the highest value that can
be generated is 110.
What are the possible outcome(s) executed from the following code ? Also specify the maximum
and minimum values that can be assigned to variable PICK.

import random
PICK = [Link](0, 3)
CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"];
for I in CITY :
for J in range(1, PICK):
Answer
print(I, end = " ")
print()
DELHIDELHI DELHI
MUMBAIMUMBAI MUMBAI
CHENNAICHENNAI CHENNAI
KOLKATAKOLKATA KOLKATA
DELHI
DELHIMUMBAI DELHIDELHI
DELHIMUMBAICHENNAI
MUMBAIMUMBAI
DELHI
MUMBAI CHENNAICHENNAI
CHENNAI KOLKATAKOLKATA
KOLKATA
DELHI The minimum value for PICK is 0 and the
MUMBAIMUMBAI maximum value for PICK is 3.
KOLKATAKOLKATAKOLKATA
Assertion/ Reasoning
Assertion. The documentation for a Python module should be
written in triple-quoted strings.
Reason. The docstrings are triple-quoted strings in Python that are
displayed as documentation when help <module> command is
issued.

(a)
Both Assertion and Reason are true
and Reason is the correct explanation
of Assertion.
Question 2
Assertion. After importing a module through import statement, all its
function definitions, variables, constants etc. are made available in the
program.
Reason. Imported module's definitions do not become part of the
program's namespace if imported through an import <module>
statement.

(b)
Both Assertion and Reason are true
but Reason is not the correct
explanation of Assertion.
Question 3
Assertion. If an item is imported through from <module> import
<item> statement then you do not use the module name along with
the imported item.
Reason. The from <module> import command modifies the
namespace of the program and adds the imported item to it.

(a)
Both Assertion and Reason are true
and Reason is the correct explanation
of Assertion.
Question 4
Assertion. Python's built-in functions, which are part of the standard
Python library, can directly be used without specifying their module
name.
Reason. Python's standard library's built-in functions are made
available by default in the namespace of a program.

(a)

Both Assertion and Reason are true and


Reason is the correct explanation of Assertion.
Question 5
Assertion. Python offers two statements to import items into the current
program : import and from <module> import, which work identically.
Reason. Both import and from <module> import bring the imported
items into the current program.

(e)
Both Assertion and Reason are false or not fully true.

Explanation
The import <module> statement imports the entire module into the new namespace setup with
same name as that of module, while the from <module> import <item> statement imports specific
items from the module into the current namespace. Hence, they both are different statements.

You might also like