0% found this document useful (0 votes)
19 views3 pages

Example:: 3. Explain The Following Built-In Functions

The document discusses built-in Python functions like id(), chr(), round(), type(), pow() with examples. It also discusses recursive functions with an example to find the factorial of a number and how to change the default recursion limit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Example:: 3. Explain The Following Built-In Functions

The document discusses built-in Python functions like id(), chr(), round(), type(), pow() with examples. It also discusses recursive functions with an example to find the factorial of a number and how to change the default recursion limit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

12 – Computer Science

Example:
c=1 → global variable
def add():
print(c)
add()
Output:
1
3. Explain the following built-in functions.
(a) id() :
id( ) Return the “identity” of an object. i.e. the address of the object in
memory.
Syntax:
id (object)
Example:
x=15
print ('address of x is :',id (x))
Output:
address of x is : 1357486752
(b) chr() :
It returns the Unicode character for the given ASCII value.
Syntax:
chr (i)
Example:
c=65
print (chr (c))
Output:
A
(c) round()
It returns the nearest integer to its input. First argument (number) is used to
specify the value to be rounded. Second argument (n digits) is used to specify the
number of decimal digits desired after rounding.
Syntax:
round (number [,ndigits])
..44..
12 – Computer Science

Example:
n1=17.89
print (round (n1,1))
Output:
17.9
(d) type() :
It returns the type of object for the given single object.
Syntax:
type (object)
Example:
x= 15.2
print (type (x))
Output:
<class 'float'>
(e) pow() :
It returns the computation of ab i.e. (ab ) a raised to the power of b.
Syntax:
pow (a,b)
Example:
a= 5
b= 2
print (pow (a,b))
Output:
25
4. Write a Python code to find the L.C.M. of two numbers.
a = int(input("Enter First Number :"))
b = int(input("Enter Second Number :"))
if a>b:
min = a
else:
min = b
while 1:
if min % a ==0 and min % b ==0:

..45..
12 – Computer Science

print (min)
break
min = min + 1
Output :
Enter First Number :5
Enter Second Number :3
15
5. Explain recursive function with an example.
When a function calls itself is known as recursion. Recursion works like loop but
sometimes it makes more sense to use recursion than loop. You can convert any loop
to recursion.
Overview of how recursive function works
1) Recursive function is called by some external code.
2) If the base condition is met then the program gives meaningful output and exits.
3) Otherwise, function does some required processing and then calls itself to
continue recursion.
Example:
def fact(n):
if n == 0:
return 1
else:
return n * fact (n-1)
print (fact (0))
print (fact (5))
Output:
1
120
python stops calling recursive function after 1000 calls by default. It also allows
you to change the limit using
sys.setrecursionlimit (limit_value).
Example:
import sys
sys.setrecursionlimit(3000)
print(fact (2000))
..46..

You might also like