Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

NIT POLYTECHNIC ,NAGPUR

MODEL ANSWER PAPER


DEPARTMENT: COMPUTER ENGINEERING B/S/M:CO6I COURSE:PWP(22616)
NAME OF EXAM:

Write Question first then , write the answer as per the note Remark on Answer in
Qus. Stepwise Hindi only to obtain full
No. given below marks mark

UNIT: 4 (Python Functions, modules, and Packages) 14M

Note :- Read and understand the question and write appropriate answer in bi-lingal language by writing technical terminology in english only, Draw
simlifiedwel-label neat scheme,whenever necessary, which is easy to understand and recall and write the answer in bilingal language only so that
students should not face language problem to write the answer or they dont do chatting the answer or rattta-fication. Ans. should be written brifely
(Give stepwise marks)
Remark on
Answer in
Write Question first then , write the answer as per the note Hindi only to
obtain full
Qus. No. given below Stepwise marks mark
Explain Local and Global variable.
Q.No.1
Local Variables: Local variables are those which are initialized inside a
function and belongs only to that particular function. It cannot be
2 अंक (1 अं क
accessed anywhere outside the function . local variable वे होते ह जो िकसी
function के अं दर start िकए जाते ह और केवल उस particular function से each)
संबंिधत होते ह। इसे function के बाहर कहीं भी accessed नहीं िकया जा सकता
है ।
Example:
S-22
def f(): # local variable
s = "I love Python Programming"
print(s) # Driver code f()
Output :
I love Python Programming
Global Variables: The global variables are those which are defined
outside any function and which are accessible throughout the program
i.e. inside and outside of every function. Global Variables वे होते ह जो
िकसी function के बाहर defined होते ह और जो पूरे program म यानी हर
function के अं दर और बाहर प ंच यो होते ह।
Example:
# This function uses global variable s
def f():
print("Inside Function", s)
# Global scope
s = "I love Python Programming"
f()
print("Outside Function", s)
Output:
Inside Function I love Python Programming
Outside Function I love Python Programming

Explain how to use user defined function in python with example.


Q.No.2
 In Python, def keyword is used to declare user defined functions.
• The function name with parentheses (), which may or may not
Include parameters and arguments and a colon: parentheses ()
के साथ function का नाम, िजसम पै parentheses और arguments
और एक कोलन शािमल हो भी सकता है और नही ं भी:
• An indented block of statements follows the function name and
arguments which contains the body of the function
Syntax:
def function_name():
statements
.
.
Example:
def fun():
4 अं क (2 अं क
print(“User defined function”)
for explanation
fun()
output: and 2 अं क for
User defined function example)
Parameterized function: The function may take arguments(s) also
called parameters as input within the opening and closing parentheses,
just after the function name followed by a colon.
Syntax:
def function_name(argument1, argument2, ...):
statements
.
.
Example:
def square( x ):
print("Square=",x*x)
# Driver code square(2)
Output:
Square= 4

Q.No.3 Write a program illustrating use of user defined package in python?

A package is a hierarchical file directory structure that defines a single


Python application environment that consists of modules and
subpackages and sub-subpackages, and so on. Packages allow for a
hierarchical structuring of the module namespace using dot notation.
Creating a package is quite straightforward, since it makes use of the
operating system’s inherent hierarchical file structure Consider the
following arrangement:
mod1.py
def m1():
print("first module") 4 अं क (2 अं क
mod2.py for defining
def m2(): package and 2
print("second module")
अंक for import
If the pkg directory resides in a location where it can be found, you can
package in
refer to the two modules with dot notation(pkg.mod1, pkg.mod2) and
import them with the syntax: program)
Syntax-1
import [, ...]
Example:
>>>import pkg.mod1, pkg.mod2
>>> pkg.mod1.m1()
first module
Syntax-2:
from import
Example:
>>> from pkg.mod1 import m1
>>> m1() First module
>>> Syntax-3: from import as Example:
>>> from pkg.mod1 import m1 as module
>>> module() first module

Syntax-3:
from import as Example:
>>> from pkg.mod1 import m1 as module
>>> module() first module You can import modules with these
statements as well:
from import [, ...] from import as
Example:
>>> from pkg import mod1
>>> mod1.m1()
First module

Q.No.4 Write use of lambda function in python.

The lambda function, which is also called anonymous function. A


lambda function can take any number of arguments, but can only have
one expression. 2 अं क for
Syntax: definition and
lambda arguments : expression use
Example:
x= lambda a,b :
a*b Print(x(10,5) )
Output: 50
Q.No.5 State use of namespace in python.
 Namespaces bring you three advantages: they group names into
logical containers, they prevent clashes between duplicate
names, and third, they provide context to names. Namespaces Correct/relevant
prevent conflicts between classes, methods and objects with use for 2 अंक
the same name that might have been written by different
people.
 A namespace is a system to have a unique name for each and
every object in Python.
 An object might be a variable or a method. Python itself
maintains a namespace in the form of a Python dictionary. A
namespace in python is a collection of names.
 So, a namespace is essentially a mapping of names to
corresponding objects.

Q.No.6 Explain package Numpy with example.


 NumPy is the fundamental package for scientific computing with
Python. NumPy stands for "Numerical Python". It provides a
high-performance multidimensional array object, and tools for
working with these arrays.
 An array is a table of elements (usually numbers), all of the
same type, indexed by a tuple of positive integers and
represented by a single variable. NumPy's array class is called
ndarray. It is also known by the alias array. In NumPy arrays, the
individual data items are called elements. All elements of an
array should be of the same type. Arrays can be made up of any
number of dimensions.
 In NumPy, dimensions are called axes. Each dimension of an
array has a length which is the total number of elements in that
direction
 The size of an array is the total number of elements contained
in an array in all the dimension. The size of NumPy arrays are 6 अं क (3 अं क
fixed; once created it cannot be changed again. A one for explanation
dimensional array has one axis indicated by Axis-0. That axis has and 3 अं क for
five elements in it, so we say it has length of five. A two example)
dimensional array is made up of rows and columns. All rows are
indicated by Axis-0 and all columns are indicated by Axis-1. If
Axis-0 in two dimensional array has three elements, so its
length it three and Axis-1 has six elements, so its length is six.

Execute Following command to install numpy in window,


Linux and MAC OS:
python -m pip install numpy
To use NumPy you need to import Numpy:
import numpy as np # alias np
Example: For NumPy with array object.
>>> import numpy as np
>>> a=np.array([1,2,3]) # one dimensional array
>>> print(a) [1 2 3]
>>> arr=np.array([[1,2,3],[4,5,6]]) # two dimensional array
>>> print(arr)
[[1 2 3]
[4 5 6]]
>>> type(arr)
>>> print("No. of dimension: ", arr.ndim)
No. of dimension: 2
>>> print("Shape of array: ", arr.shape)
Shape of array: (2, 3)
>> >print("size of array: ", arr.size)
size of array: 6
>>> print("Type of elements in array: ", arr.dtype)
Type of elements in array: int32
>>> print("No of bytes:", arr.nbytes)
No of bytes: 24

Q.No.7
1) Write python program using module, show how to write and use
module by importing it.
2) Example module. How to define module.

A module allows you to logically organize your Python code. Grouping


related code into a module makes the code easier to understand and use. Write module
A module is a Python object with arbitrarily named attributes that you 2 अं क and
can bind and reference Import
Simply, a module is a file consisting of Python code. A module can 2 अं क
define functions, classes and variables. A module can also include
runnable code. एक module म रन करने यो कोड भी शािमल हो सकता है ।
For creating a module write following code and save it as p1.py #p1.py

def add(a, b):


"This function adds two numbers and return the result"
result = a + b
return result
def sub(a, b):
"This function subtract two numbers and return the result"
result = a – b
return result
Import the definitions inside a module:
import p1
print(p1.add(10,20))
print(p1.sub(20,10))
Output: 30 10
Q.No.8 Write a program for importing module for addition and subtraction
of two numbers
calculation.py: 4 अं क (सही
def add(x,y): ो ाम और तक
return (x+y) के िलए)
def sub(x,y):
return (x-y)
operation.py:
import calculation
print(calculation.add(1,2))
print(calculation.sub(4,2))
Output:
3
2

You might also like