PYHTON LIBRARIES
PYHTON LIBRARIES
PYHTON LIBRARIES
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.
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())
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
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