0% found this document useful (0 votes)
6 views18 pages

PYHTON LIBRARIES

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Computer Science

Class XII
Python Libraries
USING PYTHON STANDARD LIBRARY’S FUNCTIONS AND MODULES
Python’s standard library is very extensive, offering a wide range of modules
and functions. The library contains built-in modules (written in C) that provide
access to system functionality such as file I/O, that would otherwise be
inaccessible to Python programmers, as well as modules written in Python
that provide standardized solutions for many problems that occur in everyday
programming. Some of these important modules are explicitly designed to
encourage and enhance the portability of Python programs.

1. Python Standard Library:

math module: for mathematical functions


cmath module:- mathematical functions for complex numbers.
random module :- functions for generating pseudo- random
numbers.
string module: for string/text-related functions.
2. NumPy Library- NumPy is the fundamental package for scientific
computing and manipulate numeric array with Python.
3. SciPy Library(pronounced “Sigh Pie”) is a Python-based ecosystem
of open-source software for mathematics, science, and engineering.

4. Tkinter Library :Tkinter is actually an inbuilt Python module


used to create simple GUI apps. It is the most commonly used
module for GUI apps in the Python.

5. Matplotlib Library: Matplotlib is one of the most popular Python


packages used for data visualization. It is a cross-platform library
for making 2D plots from data in arrays.
Processing of import <module> command
1.Imported module’s code gets executed after interpretation.
2.All the programs and variables of imported module are present in the program.
Python import statement
import math
print(“2 to the power 3 is ", math.pow(2,3))
Import with renaming
import math as mt
print(“2 to the power 3 is ", mt.pow(2,3))

Processing of from <module> import <object> command


1.Imported module’s code gets executed after interpretation.
2.Only asked programs and variables of imported module are present in the program.
Python from...import statement
from math import pow
print(“2 to the power 3 is ", pow(2,3))
Import all names
from math import *
print(“2 to the power 3 is ", pow(2,3))
Python - Math Module
We have already discussed major functions using math module. Python
provides many other mathematical built-in functions as well. These include
trigonometric functions, representation functions, logarithmic functions, angle
conversion functions, etc. In addition, two mathematical constants are also
defined in this module.
Pie (π) is a well-known mathematical constant, which is defined as the ratio of the circumference
to the diameter of a circle and its value is 3.141592653589793.

import math from math import pi


or print(pi)
math.pi
3.141592653589793 3.141592653589793

Another well-known mathematical constant defined in the math module is e. It is called Euler's
number and it is a base of the natural logarithm. Its value is 2.718281828459045.

math.e
2.718281828459045
The math module presents two angle conversion functions: degrees() and radians()
, to convert the angle from degrees to radians and vice versa.
For example, the following statements convert the angle of 30 degrees to radians
and back.

math.radians(30)
0.5235987755982988
math.degrees(math.pi/6)
29.999999999999996

math.log()
The math.log() method returns the natural logarithm of a given number. The natural
logarithm is calculated to the base e.
math.log(10)
2.302585092994046
Python - Random Module
Functions in the random module depend on a pseudo-random number generator
function random(), which generates a random float number between 0.0 and 1.0.

random.random(): Generates a random float number between 0.0 to 1.0. The function doesn't
need any arguments.
import random
print(random.random())

random.randint(): Returns a random integer between the specified integers.


import random
print(random.randint(3, 9))

randrange() Method
import random
print(random.randrange(3, 90,2))
String Methods
Python has a set of built-in methods that you can use on strings.
Method Description
capitalize() Converts the first character to upper case
count() Returns the number of times a specified value occurs in a string
find() Searches the string for a specified value and returns the position of where it was
found
index() Searches the string for a specified value and returns the position of where it was
found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isupper() Returns True if all characters in the string are upper case
lower() Converts a string into lower case
replace() Returns a string where a specified value is replaced with a specified value
split() Splits the string at the specified separator, and returns a list
Relation Between Python
Libraries, Module and
Package

•A module is a file containing


python definition, functions,
variables, classes and statements.
The extension of this file is “.py”.
•While Python package, is directory
(folder) of python modules.
•A library is collection of many
packages in python. Generally
there is no difference between
python package and python library.
Module in Python
•A module is a file containing python definition, functions, variables, classes and
statements. The extension of this file is “.py”.

Advantages–
–Its biggest advantage is that we can import its functionality in any program and
use it.
–Reusability is one of the biggest advantages.
–It helps in logically organization of Python code.
–Programming becomes very easy when we use the collection of same types of
codes.
–Categorization : same attributes can be stored in one module.
Namespaces in Python
•We imported a module in to a program which was referred as a namespace.
•To define a Namespace it is necessary to define its name.
•Python name is a kind of identifier which we use to access any python object. And in Python each and
everything is an object.
•Namespaces is used to separate the different sections of a program.
•Python has 3 types of namespaces -
–Global
–Local
–Built-in

Each module creates its own global namespace.


When we call a function then a local python namespace
is created where all the names of functions are exist.
PACKAGE / LIBRARY
•Python Package is the collection of same type of modules.
•You can use built in package as well as your own package.
•Python Package is a simple directory. Modules are stored in that directory.
•Package or library are generally same.

•Steps to make a package is as follows – (geometry)


1.We create a folder (directory) named geometry which will contain two files area.py
and volume.py
2.In the same directory we will put another file named “__init__.py”
3.__init__.py is necessary because this is the file which tells Python that directory is
package. And this fileinitializes the package
4.Then we import the package and use the content.
Standard Python Libraries
We have already discussed about math and string module. Same way we will
discuss two more important libraries. - datetime library or datetime module.
Python supports yyyy-mm-dd format for date. It has tow important classes -
–date class –time class
today ( ) now ( )
year( ) hour ( )
month ( ) minute ( )
day ( ) second ( )

You might also like