Tamilnadu State Board New Syllabus Samacheer Kalvi 12th Computer Science Guide Pdf Chapter 7 Python Functions Text Book Back Questions and Answers, Notes.

## Tamilnadu Samacheer Kalvi 12th Computer Science Solutions  Chapter 7 Python Functions

### 12th Computer Science Guide Python Functions Text Book Questions and Answers

I. Choose the best answer (I Marks)

Question 1.
A named blocks of code that are designed to do one specific job is called as
a) Loop
b) Branching
c) Function
d) Block
c) Function

Question 2.
A Function which calls itself is called as
a) Built-in
b) Recursion
c) Lambda
d) return
b) Recursion Question 3.
Which function is called anonymous un-named function PTA –
a) Lambda
b) Recursion
c) Function
d) define
a) Lambda

Question 4.
Which of the following keyword is used to begin the function block?
a) define
b) for
c) finally
d) def
d) def Question 5.
Which of the following keyword is used to exit a function block?
a) define
b) return
c) finally
d) def
b) return

Question 6.
While defining a function which of the following symbol is used.
a) ; (semicolon)
b) . (dot)
c) : (colon)
d) $(dollar) Answer: c): (colon) Question 7. In which arguments the correct positional order is passed to a function? a) Required b) Keyword c) Default’ d) Variable-length Answer: a) Required Question 8. Read the following statement and choose the correct statement(s). I) In Python, you don’t have to mention the specific data types while defining function. II) Python keywords can be used as function name. a) I is correct and II is wrong b) Both are correct c) I is wrong and II is correct d) Both are wrong Answer: a) I is correct and II is wrong Question 9. Pick the correct one to execute the given statement successfully, if ………… : print (x, ” is a leap year”) a) x%2=0 b) x%4==0 c) x/4=0 d) x%4=0 Answer: b) x%4==0 Question 10. Which of the following keyword is used to define the function testpython(): ? a) define b) pass c) def d) while Answer: c) def II. Answer the following questions (2 Marks) Question 1. What is a function? Answer: Functions are named blocks of code that are designed to do a specific job. If you need to perform that task multiple times throughout your program, you just call the function dedicated to handling that task. Question 2. Write the different types of functions. Answer: 1. User-defined functions 2. Built-in functions 3. Lambda functions 4. Recursive functions Question 3. What are the main advantages of function? Answer: • It avoids repetition and makes high degree of code reusing. • It provides better modularity for your application. Question 4. What is meant by scope of variable? Mention its types. Answer: • Scope of variable refers to the part of the program, where it is accessible, i.e., area where the variables can refer (use). • The scope holds the current set of variables and their values. • The two types of scopes are – local scope and global scope Question 5. Define global scope. Answer: A variable, with global scope can be used anywhere in the program. It can be created by defining a variable outside the scope of any function/block. Question 6. What is the base condition in a recursive function Answer: • A recursive function calls itself. Imagine a process would iterate indefinitely if not stopped by some condition. Such a process is known as infinite iteration. • The condition that is applied in any recursive function is known as a base condition. • A base condition is must in every recursive function otherwise it will continue to execute like an infinite loop. Question 7. How to set the limit for recursive function? Give an example. Answer: Python also allows you to change the limit using sys.setrecursionlimit (limit value). Example: import sys sys.setrecursionlimit(3000) def fact (n): if n = = 0: return 1 else: return n * fact (n – 1) print (fact (2000)) III. Answer the following questions (3 Marks) Question 1. Write the rules of the local variable. Answer: • A variable with a local scope can be accessed only within the function or block that it is created in. • When a variable is created inside the function/block, the variable becomes local to it. • A local variable only exists while the function is executing. • The format arguments are also local to function. Question 2. Write the basic rules for a global keyword in python. Answer: • When we define a variable outside a function, it’s global by default. We don’t have to use the global keyword. • We use a global keyword to read and write a global variable inside a function. • Use of global keyword outside a function has no effect. Question 3. What happens when we modify the global variable inside the function? Answer: It will change the global variable value outside the function also. Question 4. Differentiate ceil() and floor() function?  Cell() Floor () ceil () returns the smallest integer greater than or equal to the given value. floor() returns the largest integer less than or equal to the given value. Question 5. Write a Python code to check whether a given year is leap year or not Answer: n = int (input(“Enter any year”)) if (n % 4 = = 0): print “Leap year” else: print “Not a Leap year” Output: Enter any year 2001 Not a Leap year Question 6. What is a composition in functions? Answer: • The value returned by a function may be used as an argument for another function in a nested manner is called composition. • For example, if we wish to take a numeric value or an expression as a input from the user, we take the input string from the user using the function input() and apply eval() function to evaluate its value Question 7. How recursive function works? Answer: • Recursive function is called by some external code. • If the base condition is met then the program gives meaningful output and exits. • Otherwise, the function does some required processing and then calls itself to continue recursion. Question 8. What are the points to be noted while defining a function? Answer: • Function blocks begin with the keyword “def”followed by function name and parenthesis() . • Any input parameters or arguments should be placed within these parentheses when you define a function. • The code block always comes after colon(;) and is indented. • The statement “return [expression]” exits a function, optionally passing back an expression to the caller. • A “return” with no arguments is the same as return None. IV. Answer the following questions (5 Marks) Question 1. Explain the different types of function with an example. Answer:  Functions Description User-defined functions Functions defined by the users themselves Built-in functions Functions that are inbuilt within Python. Lambda functions Functions that are anonymous un-named function. Recursive functions Functions that call themselves is known as recursive. 1. User-defined function: Functions defined by the users themselves are called User-defined functions. Syntax: def : < Block of statement > return < expression / None> Example: def welcome(): print(“Welcome to Python”) return 2. Built-in functions: Functions which are using Python libraries are called Built-in functions. Example: x=20 y=-23 print(‘First number = ” ,x) print(‘Second number = ” ,y) Output: First number = 20 Second number = 23 3. Lambda function: • Lambda function is mostly used for creating small and one-time anonymous function. • Lambda functions are mainly used in combination with the functions like filter]), map]) and reduce]). Syntax of Lambda function (Anonymous Functions): lambda [argument(s)]: expression Example: sum = lambda arg1, arg2: arg1 + arg2 print (The Sum is :’, sum(30, 40) print (The Sum is sum(-30, 40) Output: The Sum is: 70 The Sum is: 10 4. Recursive function: • A recursive function calls itself. Imagine a process would iterate indefinitely if not stopped by some condition! Such a process is known as infinite iteration. • The condition that is applied in any recursive function is known as a base condition. • A base condition is must in every recursive function otherwise it will continue to execute like an infinite loop. • Overview of how recursive function works: • Recursive function is called by some external code. • If the base condition is met then the. program gives meaningful output and exits. • Otherwise, function does some required processing and then calls itself to continue recursion. Here is an example of recursive function used to calculate factorial. Example: def fact(n): if n==0: return 1 else: return n * fact (n-1) print (fact (0)) print (fact (5)) Output: 1 120 Question 2. Explain the scope of variables with an example. Answer: Scope of Variables: Scope of variable refers to the part of the program, where it is accessible, i.e., an area where you can refer (use) it. We can say that scope holds the current set of variables and their values. The two types of scopes are local scope and global scope. (I) Local scope: A variable declared inside the function’s body or in the local scope is called a local variable. Rules of local variable: 1. A variable with local scope can be accessed only within the function/block that it is created in. 2. When a variable is created inside the function/block; the variable becomes local to it. 3. A local variable only exists while the function is executing. 4. The formate arguments are also local to function. Example: Create a Local Variable def loc ( ): y = 0 # local scope print (y) loc ( ) Output: 0 (II) Global Scope: A variable, with global scope can be used anywhere in the program. It can be created by defining a variable outside the scope of any function/block. Rules of global Keyword: The basic rules for global keyword in Python are: 1. When we define a variable outside a function, it’s global by default. You don’t have to useglobal keyword. 2. We use global keyword to read and write a global variable inside a function. 3. Use of global keyword outside a function has no effect Example: Global variable and Local variable with same name x = 5 def loc ( ): x = 10 print (“local x:”, x) loc ( ) print (“global x:”, x) Output: local x: 10 global x: 5 In the above code, we used same name ‘x’ for both global variable and local variable. We get a different result when we print same variable because the variable is declared in both scopes, i.e. the local scope inside the function loc() and global scope outside the function loc ( ). The output:- local x: 10, is called local scope of variable. The output: – global x: 5, is called global scope of variable. Question 3. Explain the following built-in functions. Answer: a) id() b) chr() c) round () d) type() e) pow()  Question 4. Write a Python code to find the L.C.M. of two numbers. Answer: Program: # Python Program to find the L.C.M. of two input number defcompute_lcm(x, y): # choose the greater number if x > y: greater = x else: greater = y while (True): if((greater % x == 0) and (greater % y == 0)): 1cm = greater break greater += 1 return 1cm num1=int(input(//Enter first number=”)) num2=int(input(“Enter second number=”)) print (“The L.C.M. is”, compute_lcm(num1, num2)) Output: Enter first number=8 Enter second number=4 The L.C.M. is 8 Question 5. Explain the recursive function with an example. Answer: Python recursive functions 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. A recursive function calls itself. Imagine a process would iterate indefinitely if not stopped by some condition! Such a process is known as infinite iteration. The condition that is applied in any recursive function is known as base condition. A base condition is must in every recursive function otherwise it will continue to execute like an infinite loop. Working Principle: 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. Here is an example of recursive function used to calculate factorial. Example: def fact (n): if n = = 0: return 1 else: return n * fact (n – 1) print (fact (0)) print (fact (5)) Output: 1 120 ### 12th Computer Science Guide Python Functions Additional Questions and Answers I. Choose the best answer Question 1. The name of the function is followed by …………………………. (a) ( ) (b) [ ] (c) <> (d) { } Answer: (a) ( ) Question 2. Which of the following provides better modularity for your python application a) tuples b) function c) dictionaries d) control structures Answer: b) function. Question 3. How many types of functions are there in python? a) 3 b) 2 c) 4 d) 5 Answer: c) 4 Question 4. Functions that call itself are known as a) User-defined b) Built-in c) Recursive d) Lambda Answer: c) Recursive Question 5. If the return has no argument, …………………………….. will be displayed as the last statement of the output. (a) No (b) None (c) Nothing (d) No value Answer: (b) None Question 6. In which of the following the number of arguments in the function call should match exactly with the function definition? a) Keyword arguments b) Required arguments c) Default arguments d) Variable-length arguments Answer: b) Required arguments Question 7. Which of the following is used to define variable-length arguments? a)$
b) *
c) #
d) //
b) * Question 8.
What is the symbol used to denote variable-length arguments?
(a) +
(b) *
(c) &
(d) ++
(b) *

Question 9.
Which function can take any number of arguments and must return one value in the form of an expression?
a) user-defined
b) recursive
c) default
d) lambda
d) lambda Question 10.
How many return statement is executed at runtime?
a) 2
b) multiple
c) 3
d) 1
d) 1

Question 11.
How many types of scopes in Python?
a) 3
b) 4
c) many
d) 2
d) 2

Question 12.
Lambda functions cannot be used in combination with ………………………….
(a) Filter
(b) Map
(c) Print
(d) Reduce
(c) Print Question 13.
Function blocks begin with the keyword …………………
a) Fun
b) Definition
c) Function
d) Def
d) Def

Question 14.
………………. function can only access global variables.
a) user-defined
b) recursive
c) Lambda
d) return
c) Lambda Question 15.
Find the correct one:
(a) Global keyword outside the function has no effect
(b) Global keyword outside the function has an effect
(a) Global keyword outside the function has no effect

II. Answer the following questions (2 and 3 Marks)

Question 1.
Define nested blocks?
Nested Block:
A block within a block is called a nested block. When the first block statement is indented by a single tab space, the second block of statement is indented by double tab spaces.

Question 2.
Differentiate parameters and arguments.

 Parameters Arguments Parameters are the variables used in the function definition. Arguments are the values we pass to the function parameters. Question 3.
Differentiate parameters and arguments?
Parameters are the variables used in the function definition whereas arguments are the values we pass to the function parameters.

Question 4.
Write the syntax of variable-length arguments.
def function_name(*args):
function_body
return_statement

Question 5.
What are the methods used to parse the arguments to the variable length arguments?
In Variable Length arguments, we can parse the arguments using two methods.

• Non-keyword variable arguments
• Keyword variable arguments Question 6.
What is a local variable?
A variable declared inside the function’s body or in the local scope is known as a local variable.

Question 7.
What are the two methods of passing arguments in variable-length arguments?
In Variable Length arguments, we can pass the arguments using two methods.

1. Non-keyword variable arguments
2. Keyword variable arguments

Question 8.
Write a note on return statement?
The return Statement

1. The return statement causes your function to exit and returns a value to its caller. The point of functions in general is to take inputs and return something.
2. The return statement is used when a function is ready to return a value to its caller. So, only one return statement is executed at run time even though the function contains multiple return statements.
3. Any number of ‘return’ statements are allowed in a function definition but only one of them is executed at run time. Question 9.
Write a note on min (), max () and sum () with an example
Function: min ()
Description: Returns the minimum value in a list.
Syntax: min (list)
Example:
My List = [21,76,98,23]
print (‘Minimum of My List:’,
min(My List))
Output:
Minimum of My List: 21
Function.: max ()
Description:
Returns the maximum value in a list.
Syntax : min (list)
Example:
My List = [21,76,98,23]
print (‘maximum of My List :‘, max
(my list)
Output:
Maximum of My List: 98
Function : sum ()
Description:
Returns the sum of values in a list.
Syntax :sum (list)
Example:
My List = [21,76,98,23]
print (Sum of My List :‘, sum(My List))
Output:
Sum of My List :218 Question 10.
Write a note on the floor, cell () and sqrt () with an example
Function: floor ()
Description: Returns the largest integer
less than or equal to x.
Syntax: math.floor (x)
Example:
x=26.7
y=-26.7
print (math.floor (x))
print (math.floor (y))
Output:
26
-27
Function: ceil ()
Description: Returns the smallest integer greater than or equal to x.
Syntax: math.ceil (x)
Example:
x=26.7
y=-26.7
print (math.ceil (x))
print (math.ceil (y))
Output:
27
-26 . ‘
Function : sqrt ()
Description: Returns the square root of x (Note: x must be greater than zero) Syntax: sqrt (x)
Example:
a=49
b= 25
print (math.sqrt (a))
print (math.sqrt (b))
Output:
7.0
5.0 Question 11.
Write a note on the format () with an example.
Function: format ()
Description:
Returns the output based on the given format.

• Binary format: Outputs the number in base 2.
• Octal format: Outputs the number in base 8.
• Fixed-point notation: Displays the number as a fixed-point number. The default precision is 6.

Syntax : format (value [‚format_spec])
Example:
x=14
y=25
print (‘x value in binary :’,format(x/b’))
print (‘y value in octal ^formatfy/o’))
print(‘y value in Fixed-point no ‘,format(y/f’))
Output:
x value in binary: 1110
y value in octal: 31
y value in Fixed-point no : 25.000000 III. Answer the following questions (5 Marks)

Question 1.
Explain different types of arguments used in python with an example.

• Arguments are used to call a function.
• There are primarily four types of functions namely:
1. Required arguments
2. Keyword arguments,
3. Default arguments
4. Variable-length arguments.

Required Arguments:

• “Required Arguments” are the arguments passed to a function in correct positional order.
• The number of arguments in the function call should match exactly with the function definition.
• Atleast one parameter to prevent syntax errors to get the required output.

Example:
defprintstring(str):
print (“Example – Required arguments”)
print (str)
return
# Now you can call printstring() function
printstring (“Welcome”)

Output:
Example – Required arguments Welcome
When the above code is executed, it
produces the following error.
Traceback (most recent call last):
File “Req-arg.py”, line 10, in < module >
printstring()
TypeError: printstring() missing 1
required positional argument: ‘str’
Instead of printstring() in the above code if we use printstring (“Welcome”) then the output is
Output:
Example – Required arguments Welcome

Keyword Arguments:

• Keyword arguments will invoke the function after the parameters are recognized by their parameter names.
• The value of the keyword argument is matched with the parameter name and so, one can also put arguments in improper order (not in order).

Example:
def printdata (name):
print (“Example-1 Keyword arguments”)
print (“Name : “:name)
return
# Now you can call printdatat() function
print data(name = “Gshan”) When the above code is executed, it produces the following output:

Output:
Example-1 Keyword arguments
Name: Gshan
Default Arguments:

• In Python the default argument is an argument that takes a default value if no value is provided in the function call.
• The following example uses default arguments, that prints default salary when no argument is passed.

Example:
def printinfo( name, salary = 3500):
print (“Name:”, name)
print (“Salary: “, salary)
return
printinfo(“Mani”)
When the above code is executed, it produces the following output

Output:
Name: Mani
Salary: 3500
When the above code is changed as print info(“Ram,”:2000) it produces the following

Output:
Name: Ram
Salary: 2000

Variable-Length Arguments:

• In some instances, it is needed to pass more arguments that have already been specified.
• These arguments are not specified in the function’s definition and an asterisk (*) is used to define such arguments.
• These types of arguments are called Variable-Length arguments.

Syntax:
def function_name(*args):
function_body
return_statement

Example:
def printnos (*nos):
for n in nos:
print(n)
return
# now invoking the printnos() function
print (‘Printing two values’)
printnos (1,2)
Print (‘Printing three values’)
printnos (10,20,30)

Output:
Printing two values
1
2
Printing three values
10
20
30