Professional Documents
Culture Documents
SCHOOL - 601206
Namma Kalvi
UNIT CHAPTER 7 PYTHON FUNCTION
II ALGORITHMIC STRATEGIES
www.nammakalvi.in
15.What is meant by scope of variable? Mention its 16.What happens when we modify global variable
types. inside the function
15.Explain the types of Scope of Variables Thrown an error because,
Without using the global keyword we cannot
Local Scope modify the global variable inside the function.
A variable declared inside the function's body or in but we can only access the global variable
the local scope is known as local variable. Example:
c = 10 # global variable
Rules of local variable def add():
It can be accessed only within the function/block. global c
local variable is created inside the function/block. c=c+2
A local variable only exists while the function is print(c)
executing. add()
The formal arguments are also local to function. output: 12
Ex 17. Write a Python code to heck whether a given year
def loc(): is leap year or not.
Y=4 y=int(input("Enter a Year : "))
loc() if y%4==0:
print(y) print(y," is a leap year")
else:
Output print(y," is not a leap year")
error
The above error occurs because y is a local variable. 18.Explain the following built-in functions.
id()
Global Scope Returns address of an object
A global variable, can be used anywhere in the Syntax: id(object)
program. Ex. a=15.2
It can be created by defining a variable outside the Print(id(a)) # : output : 134526789
function/block. chr()
Rules of global Keyword Returns Unicode character for the given ASCII
To define a variable outside a function, it’s global value
by default. Syntax: chr(x)
global keyword used modify the global variable Ex.x=65 print(chr(x)) #output : A
inside a function. round()
Use of global keyword outside a function has no Returns the nearest integer to its input
effect First argument is used to specify the value to be
Example: rounded
c = 10 # global variable Second argument is used to specify the number
def add(): of decimal digits
global c Syntax : round(number,[,ndigits])
c=c+2 Ex. x=17.89 print(round(x,1)) #output : 17.9
print(c) type()
add() Returns the type of object.
output: 12 Syntax: type(object)
Ex.X=15.8 type(x) #output <class ‘ float’>
pow()
Return the computation of ab i.e a**b
Syntax: pow(a,b)
Ex .pow(3,2) # output 9
19. Write a Python code to find the L.C.M. of two 22.Explain about format() function
numbers. Returns the output based on the given format
x=int(input("Enter first Number...")) Syntax: format(value,[format_spec])
y=int(input("Enter second Number...")) Example : x=14 print(“the value id
“,format(x,’b’)
def lcm(x,y): Binary format_spec - > b
i=max(x,y) octal format_spec - > o
fixed point notation: format_spec - > f
s=min(x,y)
i=1 23.Explain mathematical function with an example
while(True): Sqrt()
if i % s == 0: Returns the square root of x
Syntax: sqrt(x)
return i A=25 sqrt(a) output : 5
i+=1 ceil() floor()
Returns the smallest Returns the largest
print(lcm(x,y)) integer >= x integer <= x
20.What is Composition in functions? math.ceil(x) math.floor(x)
The value returned by a function may be used as an X=26.7 y=-26.7 z=-23.2 X=26.7 y=-26.7 z=-23.2
argument for another function in a nested manner. Print(math.ceil(x)) = 26 Print(math.(x)) = 27
This is called composition. Print(math.ceil(y)) = -27 Print(math.ceil(y)) = -26
For example, if, we take the input string Print(math.ceil(z)) = -24 Print(math.ceil(z)) = -23
using the function input() and apply eval() function
to evaluate its value, 24..Differentiate ceil() and floor() function?
for example:
>>> n1 = eval (input ("Enter a number: ")) ceil() floor()
Enter a number: 234 Returns the smallest Returns the largest
>>> n1 integer >= x integer <= x
234 math.ceil(x) math.floor(x)
21.Explain some Built-in and Mathematical functions X=26.7 y=-26.7 z=-23.2 X=26.7 y=-26.7 z=-23.2
in python Print(math.ceil(x)) = 26 Print(math.(x)) = 27
Print(math.ceil(y)) = -27 Print(math.ceil(y)) = -26
abs() Print(math.ceil(z)) = -24 Print(math.ceil(z)) = -23
www.nammakalvi.in
#output: weol
www.nammakalvi.in