You are on page 1of 5

Type A

Q. What is the significance of Modules?

Answer =

Significance of Modules are as follows:-

• It reduces complexity of programs.


• It content can be reused in other programs, without having to rewrite them.
• It create a number of well defined, documented boundaries within the program.

Q. What are doc-strings? What is their significance? Give


example to support your answer.

Answer =

A doc-string is just a regular Python triple-quoted string that is the first thing in a function body/ a
module/ a class. When executing a function body (or a module/ class), the doc-string doesn't do
anything like comments, but Python stores it as part of the function documentation. This
documentation can later be displayed using the help() function.

So, even though doc-strings appear like comments (no execution), they are different from comments

For example:-

def add(x, y):


'''this function add two numbers'''
     return x + y

Q. What is a package? How is a package different from


module?

Answer =

Package: A package is a directory that contains sub-packages and modules in it along with some
special files such as __init__.py.

While A module is a file with some Python code and is saved with a .py extension.
Q. What is a library? Write procedure to create own library in
Python.

Answer =

A Python library is a reusable chunk of code that is used in program/script using import command. A
package is a library if it is install-able or gets attached to site-packages folder of Python installation.

To create own library or package in Python, we should do the following:-

(a) Create a package folder having the name of the package/library.


(b) Add module files (.py files containing actual code functions) to this package folder.
(c) Add a special file _init__.py to it (even if the file is empty).
(d) Attach this package folder to site-package folder of Python installation.

Q. What is the use of file __init__.py in a package even when it


is empty?

Answer =

It indicates that, it is an importable python package, without __init__.py a folder is not


considered a Python package.

Q. What is the importance of site-packages folder of Python


installation?

Answer =

In order to import a package using import command, the packages and its contents must be
attached to site-package folder of Python installation as this is the default place from where
Python interpreter import Python library and packages. So, in order to import our package with
import command in our programs, we must attach it to site package folder of Python installation.
Q. What is the importance of site-packages folder of Python
installation?

Answer =
In order to import a package using import command, the packages and its contents must be
attached to site-package folder of Python installation as this is the default place from where
Python interpreter import Python library and packages. So, in order to import our package with
import command in our programs, we must attach it to site package folder of Python installation.
Q. What is PYTHON PATH variable? What is its significance?

Answer =

PYTHONPATH 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.

Q. In which order Python looks for the function/module names


used by you.

Answer =

In sys. order.

Q. What is the usage of help() and dir() functions.

Answer =

help() function help, when it is provided with a program-name or a module-name or a function-name


as an argument, it displays the documentation of the argument as help.

The dir() function returns a list of all the attributes within in an object - these could be either data
values or methods, and the list includes private and magic methods.

Q. Name the Python Library modules which need to be


imported to invoke the following functions:-

(i)log()
(ii) pow()

Answer =
Math module

Q. What is dot notation of referring to objects inside a module?

Answer =

After importing a module, to access one of the function, you have to specify the name of the
function, separated by dot. This format is called dot notation.

Q. Why should the from <module> import <object> statement


be avoided to import objects?

Answer =

Because it may lead to name clashes. If you use plain import <module>, no problem occur.

Q. What do you understand by standard library of Python?

Answer =

Python standard library is very extensive that offers many built in functions. Python standard
library is by default available, you don't have to import it separately.

Q. Explain the difference between import <module> and from


<module> import statements, with examples.

Answer =

import <module> statement, import the given module.

while from <module> import statement import the function which is present in given module.

Example :-

(i)  
import math
a = 144
print( math.sqrt(a) )

Output :-

12.0
>>> 

(ii)

from math import sqrt as sq


a = 144
print( sq(a) )

Output:-

12.0
>>>

You might also like