Chapter 7 Functions in Python Class 11 NCERT Solutions

Share with others

Chapter 7 Functions in Python Class 11 NCERT Solutions

Summary of Chapter 7 Functions in Python

In programming, functions are used to achieve modularity and reusability.

Function can be defined as a named group of instructions that are executed when the function is invoked or called by its name. Programmers can write their own functions known as user  defined functions.

The Python interpreter has a number of functions built into it. These are the functions that are frequently used in a Python program. Such functions  are known as built-in functions.

An argument is a value passed to the function  during function call which is received in a parameter
defined in function header.

Python allows assigning a default value to  the parameter.

A function returns value(s) to the calling function  using return statement.

Multiple values in Python are returned through a Tuple.

Flow of execution can be defined as the order in which the statements in a program are executed.

The part of the program where a variable is accessible is defined as the scope of the variable.

A variable that is defined outside any particular function or block is known as a global variable. It can be accessed anywhere in the program.

A variable that is defined inside any function or block is known as a local variable. It can be accessed only
 in the function or block where it is defined. It exists only till the function executes or remains active.

The Python standard library is an extensive collection of functions and modules that help the programmer in the faster development of programs. 

A module is a Python file that contains definitions of multiple functions.

A module can be imported in a program using import statement.

Irrespective of the number of times a module is imported, it is loaded only once.

To import specific functions in a program from a module, from statement can be used.
 

Chapter 7 Functions in Python Class 11 NCERT Solutions

EXERCISE : Functions in Python

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q1. Observe the following programs carefully, and identify the error:

a) def create (text, freq):
for i in range (1, freq):
print text
create(5) #function call

Ans. There are two errors in the above code

1. Mismatch of arguments in Function Call and Function create statements.

2. In third line it should be print(“text”)

b) from math import sqrt,ceil
def calc():
print cos(0)
calc( ) #function call

Ans. There are two errors in he above code:

1. Import function [sqrt( ), ceil( ) ] and used function [cos( ) ] are different.

2. Syntax of print statement is wrong. It should be print(cos(0)).

c) mynum = 9
def add9( ):
mynum = mynum + 9
print mynum
add9( ) #function call

Ans. There are two errors in the above code :

1. mynum is a global variable which can be used inside a function only by using a keyword “global” which is missing in above code.

2. Syntax of print statement is wrong. It should be print(mynum).

d. def findValue( vall = 1.1, val2, val3):
final = (val2 + val3)/ vall
print(final)
findvalue() #function call

Ans. There are three errors in the above code :

1. Function name is different in function call (findvalue( )) statement and function create statement (findValue( ))

2. First line of above code should be def findValue(val2, val3, vall = 1.0)

3. There should be minimum two arguments in function call statement which is missing in above code.

e. def greet( ):
return("Good morning")
greet( ) = message #function call

Ans. There is one errors in the above code :

  1. Third line of above code should be message = greet( )

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q2. How is math.ceil(89.7) different from math.floor(89.7)?

Ans. math.ceil(89.7) gives output 90 while math.floor(89.7) gives output 89.

Q3. Out of random() and randint(), which function should we use to generate random numbers between 1 and 5. Justify.

Ans. randint( ) function is used to generate random number between 1 and 5. This function generate a random number between two given integers as argument like randint(1, 5).

Q4. How is built-in function pow() function different from function math.pow() ? Explain with an example.

Ans. In built function pow( ) will return an integer while math.pow( ) function will return a floating point number like

print(pow(2, 3) will return 8

import math

print(math.pow(2, 3)) will return 8.0

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q5. Using an example show how a function in Python can return multiple values.

Ans. 

def fun():
   x = 3
   y = 7
   return x,y
 a, b = fun()
 print(a)
 print(b)

OUTPUT

3
7

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q6. Differentiate between following with the help of an example:
a) Argument and Parameter
b) Global and Local variable

Ans. a) A parameter is a variable/value which is given in function declaration statement while argument is a variable / value which is given in function call statement like

def fun(a, b):
    return a + b
S = fun(3, 7)
print("Sum is ", S)

Here a and b in first line are parameter while 3 and 7 given in third line are argument

b) Global Variable : In Python, a variable that is defined outside any function or any block is known as a global variable. It can be accessed in any functions defined onwards

Local Variable : A variable that is defined inside any function or a block is known as a local variable. It can be accessed only in the function or a block where it is defined.

Chapter 7 Functions in Python Class 11 NCERT Solutions

LAB EXERCISES

Q1. Write a program to check the divisibility of a number by 7 that is passed as a parameter to the user defined function.

Ans. 

def check(a):
   if a % 7 == 0:
     print("It is divisible by 7")
   else:
     print("It is not divisible by 7")
 check(343)

OUTPUT :
It is divisible by 7

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q2. Write a program that uses a user defined function that accepts name and gender (as M for Male, F for Female) and prefixes Mr/Ms on the basis of the gender.

Ans. 

def check(n,g):
   if g == 'F' :
     print ("Name is : ", "Ms. "+ n)
   elif g == 'M' :
     print ("Name is : ", "Mr. "+ n)
   else :
     print("Pass only M or F as gender")
 check("Amit","M")

OUTPUT:

Name is :  Mr. Amit

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q3. Write a program that has a user defined function to accept the coefficients of a quadratic equation in variables and calculates its determinant. For example : if the coefficients are stored in the variables a,b,c then calculate determinant as b2-4ac. Write the appropriate condition to check determinants on positive, zero and negative and output appropriate result.

Ans.

import math
 def deter(a, b, c):
   D = b*2 - 4a*c
   if D > 0 :
        print("Roots of Quadratic equation are real and distinct")
   elif D == 0:
        print("Roots of Quadratic equation are real and equal")
   else:
        print("No real Roots of Quadratic equation")

 A = int(input("Enter Coefficient of square of x :"))
 B = int(input("Enter Coefficient of x :"))
 C = int(input("Enter Constant :"))
 deter(A,B,C)

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q4. ABC School has allotted unique token IDs from (1 to 600) to all the parents for facilitating a lucky draw on the day of their Annual day function. The winner would receive a special prize. Write a program using Python that helps to automate the task.(Hint: use
random module)

Ans. 

import random
win=random.randint(1, 600)
print("Winner is : Token Number : ", win)

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q5. Write a program that implements a user defined function that accepts Principal Amount, Rate, Time, Number of Times the interest is compounded to calculate and displays compound interest. (Hint: CI = ((P*(1+R/N))NT)

Formula given in the question is for Calculating Amount not the Compound Interest

Ans. 

def compound(p, r, t, n):
   amt = p * (1 + r/100) ** n * t
   cint = amt - p
   print("Compound Interest is  : ",cint)
p = int(input("Enter the Principal"))
r  = int(input("Enter the Rate of interest"))
t  = int(input("Enter the time period in Years"))
n = int(input("Enter the number of times the interest is compounded"))
compound(p, r, t, n)

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q6. Write a program that has a user defined function to accept 2 numbers as parameters, if number 1 is less than number 2 then numbers are swapped and returned, i.e., number 2 is returned in place of number1 and number 1 is reformed in place of number 2, otherwise the same order is returned.

Ans. 

def swap(n1, n2):
   if n1 < n2:
        n1, n2 = n2, n1
        return(n1,n2)
   else:
        return (n1,n2)
n1 = int(input("Enter First number"))
n2 = int(input("Enter Second number"))
x,y = swap(n1,n2)
print("After Swapping Result is")
print("First Number is  : ",x)
print("Second Number is  : ",y)

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q7. Write a program that contains user defined functions to calculate area, perimeter or surface area whichever is applicable for various shapes like square, rectangle, triangle, circle and cylinder. The user defined functions should accept the values for calculation as parameters and the calculated value should be returned. Import the module and use the appropriate functions.

Ans. 

import math
def sqarea(s):
   return s * s 
def sqperi(s):   
   return 4 * s
def recarea(l,b):
   return l * b 
def recperi(l,b):   
   return 2 * (l+b)
def Rtriarea(b,h):
   return 1/2 * b * h
def triarea(a,b,c):
   s = (a + b + c )/2
   return math.sqrt(s * (s-a) * (s-b) * (s-c))
def cirarea(r):
   return math.pi * r ** 2
def cirperi(r):
   return 2 * math.pi * r
def cylTarea(r,h):
   return 2*math.pi * r * (r + h)
 def cylCarea(r,h):
   return 2 * math.pi * r * h
 print("1. Calculate Area of Square")
 print("2. Calculate Perimeter of Square")
 print("3. Calculate Area of Rectangle")
 print("4. Calculate Perimeter of Rectangle")
 print("5. Calculate Area of Right angle triangle")
 print("6. Calculate Area of Scalene Triangle")
 print("7. Calculate Area of Circle")
 print("8. Calculate Circumference of Circle")
 print("9. Calculate Total Surface Area of Cylinder")
 print("10. Calculate Curved Surface Area of Cylinder")
 ch = int(input("Enter Your Choice"))
 if ch == 1:
   s = int(input("Enter Side of Square"))
   ar = sqarea(s)
   print("Area of Square is  : ",ar)
 elif ch == 2:
     s = int(input("Enter Side of Square"))
     p = sqperi(s)
     print("Perimeter of Square is  : ",p)
 elif ch == 3:
     l = int(input("Enter the length of Rectangle"))
     b = int(input("Enter the breadth of Rectangle"))
     a = recarea(l,b)
     print("Area of Rectangle is  : ",a)
 elif ch == 4:
     l = int(input("Enter the length of Rectangle"))
     b = int(input("Enter the breadth of Rectangle"))
     p = recperi(l,b)
     print("Perimeter of Rectangle is  : ",p)
 elif ch == 5:
     b = int(input("Enter base of triangle"))
     h = int(input("Enter height of triangle"))
     a = Rtriarea(b,h)
     print("Area of triangle is  : ",a)
 elif ch == 6 :
     a = int(input("Enter First side of triangle"))
     b = int(input("Enter Second side of triangle"))
     c = int(input("Enter Third side of triangle"))
     ar = triarea(a,b,c)
     print("Area of triangle is  : ",ar)
 elif ch == 7 :
     r = int(input("Enter radius of Circle"))
     a = cirarea(r)
     print("Area of Circle is  : ",a)
 elif ch == 8:
     r = int(input("Enter radius of Circle"))
     cr = cirperi(r)
     print("Circumference of Circle is  : ",cr)
 elif ch == 9 :
     r = int(input("Enter radius of base of Cylinder"))
     h = int(input("Enter height of Cylinder"))
     tsa = cylTarea(r,h)
     print("Total Surface area of Cylinder is  : ",tsa)
 elif ch == 10:
     r = int(input("Enter radius of base of Cylinder"))
     h = int(input("Enter height of Cylinder"))
     csa = cylCarea(r,h)
     print("Curved Surface area of Cylinder is  : ",csa)

Chapter 7 Functions in Python Class 11 NCERT Solutions

ACTIVITY BASED QUESTIONS

Q1. To secure your account, whether it be an email, online bank account or any other account, it is important that we use authentication. Use your programming expertise to create a program using user defined function named login that accepts userid and password as parameters (login(uid,pwd)) that displays a message “account blocked” in case of three wrong attempts. The login is successful if the user enters user ID as “ADMIN” and password as
“St0rE@1”. On successful login, display a message “login successful”.

Ans. 

c=0
def login(uid, pwd):
   global c
   if uid == "ADMIN" and pwd == "St0rE@1":
        print("Login Successful")
        return
   else:
        c=c+1
     if c == 3:
         print("Account Blocked")
         return
     else:
         ltry()
def ltry():
   global c
   print("Attempt : ",c+1)
   uid = input("Enter User Id")
   pwd = input("Enter Password")
   login(uid,pwd)

ltry() # This function is called to start the program

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q2. XYZ store plans to give festival discount to its customers. The store management has decided to give discount on the following criteria:

Shopping AmountDiscount Offered
>= 500 and < 10005%
>=1000 and <20008%
>= 200010%
Functions in Python

An additional discount of 5% is given to customers who are the members of the store. Create a program using user defined function that accepts the shopping amount as a parameter and calculates discount and net amount payable on the basis of the following conditions: Net Payable Amount = Total Shopping Amount –Discount.

Ans. 

def amount(sa, sd):
   print(sa,sd)
   dis=0
   if sa >=500 and sa <1000:     dis = 5 + sd   elif sa >=1000 and sa <2000 :
        dis = 8 + sd
   else:
        dis = 10 + sd
   Disc = dis/100 * sa
   amt = sa - Disc
   print("Total Discount offered is  :",Disc)
   print("Total Amount to pay after discount is  : ", amt)
sa = int(input("Enter Shopping Amount"))
sm = input("You are Special Member??(y/n)") 
if sm.lower() == 'y':
   amount(sa,5)
else:
   amount(sa, 0)

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q4. Take a look at the series below: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…To form the pattern, start by writing 1 and 1. Add them together to get 2. Add the last two numbers: 1+2 = 3.Continue adding the previous two numbers to find the next number in the series. These numbers make up the famed Fibonacci sequence: previous two numbers are added to get the immediate new number.

Ans. 

def fib(n):
   if n==1:
     print("1")
   elif n == 2:
     print("1, 1")
   elif n <= 0:
     print("Please enter positive number greater than 0")
   else:
     ft = 1
     st = 1
     print(ft,end=" ")
     print(st,end=" ")
     for i in range(2,n):
          nt = ft + st
          print(nt,end = " ")
          ft = st
          st = nt
 nt = int(input("How many terms you want in Fibinacci Series"))
 fib(nt)

Chapter 7 Functions in Python Class 11 NCERT Solutions

Q5. Create a menu driven program using user defined functions to implement a calculator that performs
the following:
a) Basic arithmetic operations(+,-,*,/)
b) log10(x),sin(x),cos(x)

Ans. a) 

ch ='y'
while (ch=='y' or ch == 'Y'):
   a = int(input("Enter first number: "))
   b = int(input("Enter second number: "))
   print("1. Addition")
   print("2. Subtraction")
   print("3. Multiplication")
   print("4. Division")
   print("5. Exit")
   n = int(input("Enter your choice:"))
   if n == 1:
        print("The result of addition : ", a + b)
   elif n == 2:
        print("The result of subtraction : ", a - b)
   elif n == 3:
        print("The result of multiplication :  ", a * b)
   elif n == 4:
        print("The result of division : " , a / b)
   elif n == 5:
        quit()
   else:
        print("Enter Correct Choice")
   ch = input("Do you want to continue(Y/N)")

b)

import math
ch ='y'
while (ch=='y' or ch == 'Y'):
   x = int(input("Enter value of x : "))
   print("1. Log10(x)")
   print("2. Sin(x)")
   print("3. Cos(x)")
   print("4. Exit")
   n = int(input("Enter your choice:"))
   if n == 1:
        print("Log Value of ",(x),"is :",math.log10(x))
   elif n == 2:
        print("Sin(x) is : ",math.sin(math.radians(x)))
   elif n == 3:
        print("Cos(x) is : ",math.cos(math.radians(x)))
   elif n == 4:
        quit()
   else:
        print("Enter Correct Choice")
   ch = input("Do you want to continue(Y/N)")

Chapter 7 Functions in Python Class 11 NCERT Solutions


Disclaimer : I tried to give you the correct “Chapter 7 Functions in Python Class 11 NCERT Solutions” , but if you feel that there is/are mistakes in the Chapter 7 Functions in Python Class 11 NCERT Solutions given above, you can directly contact me at csiplearninghub@gmail.com.


Chapter 7 Functions in Python Class 11 NCERT Solutions

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series


Share with others

Leave a Reply

error: Content is protected !!