You are on page 1of 4

Narendra Srivastava, Doon International School

USING PYTHON LIBRARIES

Library: A library refers to a collection of modules that together cater to specific needs or
applications. For example. Math library of Python caters to mathematical computations, Numpy
library deals with scientific computing.

Modules are used to categorize Python code into smaller parts .The act of partitioning a program
into individual components is called modularity. A module is a file consisting of Python code that can
define functions, classes and variables related to a particular task. A module allows us to organize
out code by grouping related by grouping related code, which makes the code easier to understand
and use.

Docstring: String literals enclosed with triple quotes that appear right after the definition of a
function, class method or module. It is mainly used for documentation and can be accessed using.

import statement can be used to module a module. It is simplest and most common way to use
modules in our code. It gives access to all attributes and methods or function present in the module.
We can use any Python source(module)by executing an import statement. There are four different
ways to import statement.
1. import module name
this line will invoke module which is written with import and all functions of module can be
accessed.
2. import <module1, module2, modul3……
this line will invoke module more than one in the program which is written with import and
all functions of modules can be accessed.
3. From module name import function name(s)
This line will allow to use functions which are mentioned with import and from is used to
write module name. Using from statement, never use module name while invoking functions
from module.
4. From module name import *
This line will allow to use all functions from module.

NAMESPACE:- It is a system that has a unique name for each and every object in Python. An object
might be a variable or method. In Python every name has a place where it resides and
can be looked for. Python implements namespaces as directory, where a variable name
is mapped to the object placed. Whenever the variable is searched, the dictionary key
will be searched to retrieve the corresponding object.

PYTHONPATH:- this is the variable that tells the interpreter where to locate the module files
imported into a program. Hence, it must include the Python source library
directory and the directories containing Python source code. You can manually
set PYTHONPATH but usually, the Python installer will preset it.
LEGB( Local, Enclosing, Global, Built-in):-When you access a variable from withing a or function,
Python follows name resolution rule, also known as LEGB rule.
a. It checks within its Local environment or local namespace, if it has a variable then it uses
its value otherwise Python checks the Enclosing environment if it will find there then it
Using Python Libraries
Narendra Srivastava, Doon International School

will its value otherwise Python will check Global environment if it will find then its ok
otherwise it will move in Built-In environment, if it is available there then its ok otherwise
Python will generate the error:-
Name <variable> is not defined.

PACKEGE/LIBRARY:- Python package is a collection of related modules. We can create our own or
we can import a built in package. The main difference between a module and a
package is that a package is a collection of modules and has an __init__.py file.
__init__.py is simply a file that is used to consider the directories on the disk as
package of Python. It is basically used to initialize the Python packages.

Datetime Library/Module:- The datetime module in Python handles the extraction and formatting of
date and time variables. Objects for datetime library are created in Python
to hold and represent a date and a time according to a specific time zone.To
return the current local date today() function of date class is used. today()
function comes with several attributes (year, month and day). These can be
printed individually.
today = date.today()
print("Today's date is", today)
Output:
Today's date is 2022-08-19
We can get the year, month, and date attributes from the date object using
the year, month and date attribute of the date class.
today = date.today()
print("Current year:", today.year)
print("Current month:", today.month)
print("Current day:", today.day)

Output
Current year: 2021
Current month: 8
Current day: 19
time class:-time class holds attributes for hour, minute, second and microsecond, and can be
accessed through(.) operator along with time object.
now: this method returns the current date and time using datetime library/module.
hour: this attribute returns and displays hours from the datetime object.
minute: this attribute returns and displays minutes from the datetime.
second: this attribute returns and displays seconds from the datetime.
import datetime
ns=datetime.datetime.now()
print("hours are=",ns.hour)
print("Minutes are=",ns.minute)
print("Seconds are=",ns.second)
OUTPUT:
hours are= 16
Minutes are= 17
Seconds are= 30

Using Python Libraries


Narendra Srivastava, Doon International School

random.random() method is used to generate random floats between 0.0 to less than 1.
import random
print(random.random())
OUTPUT
0.91109887654
Random.randint() this method is used to generate any number form give a range in parameter, note
first number is starting number and second number is ending number and ending
number is inclusive
choose the possible output(s) of the following code ndom

NAV = ["LEFT","FRONT","RIGHT","BACK"]

NUM = random.randint(1,3)

NAVG = ""

for I in range(NUM,1,-1):

NAVG = NAVG+NAV[I]

print (NAVG)

(i) BACKRIGHT (ii) BACKRIGHTFRONT

(iii) BACK (iv) LEFTFRONTRIGHT


Answer:
i) BACKRIGHT
Maximum Value that can be assigned to NUM = 3
Minimum Value that can be assigned to NUM = 1

choose the possible output(s) of the following code

import random

N=random.randint(0,2)

M=random.randint(0,3)

DOCK = [[1,2,3],[2,3,4],[3,4,5]]

for R in range(N):

for C in range(M):

print(DOCK[R][C],end=" ")

Using Python Libraries


Narendra Srivastava, Doon International School

print()

i. 1 2 3 ii. 1 2 3 iii. 1 2 iv. 1 2

2 3 4 2 3 4 2 3 2 3

3 4 5 3 4
Answer:
ii) and iii)
Maximum Value that can be assigned to N = 2
Minimum Value that can be assigned to N = 0
Maximum Value that can be assigned to M = 3
Minimum Value that can be assigned to M = 0

What possible output(s) are expected to be displayed on the screen at the time of
execution of the program from the following code? Also, specify the minimum values
that can be assigned to each of the variables BEGIN and LAST.

import random

VALUES=[10,20,30,40,50,60,70,80]

BEGIN=random.randint(1,3)

LAST=random.randint(BEGIN,4)

for I in range(BEGIN,LAST+1):

print (VALUES[I],"-",)

i) 30 - 40 - 50 -

ii) 10 - 20 - 30 - 40 -

iii) 30 - 40 - 50 - 60 -

iv) 30 - 40 - 50 - 60 - 70 -
Answer: i) and iii)

Using Python Libraries

You might also like