Adobe Scan 16 Apr 2024
Adobe Scan 16 Apr 2024
. The function's header has acolon missing at the end and the next statement must be indented.So, wera
to add colon (:) at the end of the header line and the next line will be indented.
Thus, the corrected code will be:
def minus (total, decrement):
oupu =total - decrement Indented statements
What is the utility of Python standard library'smath module and random module?
(1) math module: The math module is used for math-related functions that work with all number E
except for complex numbers. For example, pow(), sqrt(), etc.
(ii) random module: The random module is used for different random number generator functiors
For example,randint(), random(), etc.
Vhat is Python module? What is its significance?
"module" is a chunk of Python code that exists in its own (.py) file and is intended to be used by Ayt
ode outside itself.
iodules allow one to bundle together code in a form in which it can easily be used later. The motue
in be "imported" in other programs so the functions and other definitions in imported modules beco
ailable to code that imports them.
hat is the utility of the built-in function help()?
thon 's built-infunction help() is very useful. When it is provided with aprogram name or a module-r
afunction-name as an argument, it displays the documentation of the argument as help. It also disoa
docstring within its passed-argument's definition. For example,
>help (math)
display the documentation related to module math.
en even take function name as argument. For example,
help (math.sqrt0)
above code will list the documentation of math.sqrt() function only.
Python-
Computer Science with
16. What is the significance of having functions in a program?
Ans. Creating functions following advantages:
in a program is very useful. It offers the
(i) The program is easierto understand.
Main block of the functions issnot a
program becomes compact as the code of part of it an
is easier to read and
understand.
) Redundant code is at one place. so making changes is easlel
Instead of writing code again when we need to use it more than once, we can write the code in
of a
function and cal it
at one place only.Thus, itmore
than once. If we later need to change the
code, we will
need to
saves our time also.
( ) Reusable functions can be put ina
library in modules.
wbStore the reusable functions in the form of amodule. These modules can be imported :
when
17. needed in other programs.
What is a
Ans. To use a function call?
function that has been defined earlier, a function call state ment is written in Python.
Its syntax is:
e.g., print() <function nane(<value to be passed to argunent>)
18. From the
def
program code given below, identify the parts mentioned below:
processNumber
X=72
(x):
return x +3
y = 54
res =
ldentify these
main program.
processNumber
parts:
(y)
function header,function call, actual arguments, formal parameters, functi
Ans. Function header
Function call def processNumber(x):
Actual arguments processNumber(y)
Formal Parameters : x
Function body :X =72
return x+ 3
Main program :y= 54
res = processNumber(y)
. Find errors, if any, in the following
() import math
code:
(ii) import random
math.power (4,2) random. randint (4,16,4)
(ii1) import random
(iv) import math
random. random (2,10)
(i) Error: math. sqrt(-256)
function name is pow() instead of power
(ii) Error: randint() function does not take
step value as an argument.
iii) Error: random() function does not
take any argument.
iv) Error: sqrt() function does not take
negative number as an argumen
lame the built-in mathematical
and y.
function/method that is used to returi
d (x, y ) which is a part of math module in Python.
Also, give asuitable Python codeto itlyshr
25. Differentiate between local variable and global variable.
Ans. Global Variable
LocalVariable
S.No.
functionIt is a variable which is declared outside
It is a variable which is declared within
a
1 the functionsor in aglobal space.
or within ablock. accessible throughout the prograr
outside the function butIt is
2. It cannot be accessed which it is declared.
only within afunction/block of a program.
xCubed are global variables and n and cn are local variable,
x,
For example, in the followingcode variables
#n and cn are local
def cube (n):
Cn = n * n *n
return cn
X=10 #x is a global variable
variable
xCubed= cube (x) # xCubed is a global
print (x, "cubed is", xCubed)
26. What is the output of the following code?
mus
Anx, ie xitonat agument must not be followed by kevword atument, Le, posltlonal argument
tNr tbefore a keyword argunent.
Lie there ix no keyworntargument with the nanne tlme'
Line S AMIxsing value for positlonal argument, R
the following two return statements:
a0, eturn statement optionalP Compare and comment on
function d
the functlon is vold, or we can say that when the
Ans, the return statement is optienal only when value must have at least one return statement.
hot retn a value. A netlon that returns a
From the given tWo return statements, the statement:
etun
along with empty value None. And
net eturning any value. Rather, it returns the control to caller
is
statement:
teturn val
with the value contalned In varlable val.
0s returning the eontrolto ealler along him to define a function
python programmer, is working on a project whlch requlres
31 Divyansh, a it as:
name Caleulatelnterest(). He detines incipal, Rate06,Time): #code
(Pr
tef Caleulatelntereat Divyansh to ldentify the error in the
above function and
working. Can you help
But this code is not
per Python's synt
default argument which is wrong as
the solution?
argument is followed by
ns. Yes, here non-default
Solutlon
as the last argument: Rate-.06):
way is by putting Rate(Prinetpal,
(a) The first
Caleulatelnterest Time,
def also as:
(b) Or, e0ve any default value to Time
(Prnetpal,
Rate-.06,Time-12) :
Caleulatelnterent
def accepts alist of numbers and replace each even
listchange(Arr) in Python, which
2. Writeafunctlon
value 10 and multiply odd number by 5.
by
list Is:
Sample nput Data of the
a-|10,20, 23, 451
lintchange ( )
|
Output: |L0, l0, TI 5,
ctions