Functions
Introduction:
• A function is a collection of statements which is made to perform a particular task.
• Instead of writing large programs ,we can write the functions as specific part of the program to
accomplish the task.
• To execute a function we have to call it in the program. We can call a function “N” times in a
program
• Once written a function can also be used in other program as a library function.
• We can create “N” of functions in a program.
Types of Function:
1. Built-in-Function 2. Functions Defined in Module 3. User Defined Function
[Link]-in Function:
• These are pre defined functions and always available for use.
• In order to use these functions, there is no need to import any module.
• We can directly use it in our program.
Example: print(), len() etc.
2. Functions Defined in Module:
• These Functions are defined in particular module.
• These can only used when the corresponding Module is imported.
math Module( sin(),cos(), sqrt(),fabs(),ceil(),floor())
statistics Module( mean(),median(),mode())
random Module( random(),randrange(),randint(),uniform(),choice(),suffle())
[Link] Defined Function:
• The Functions are defined by the programmer.
• Keyword def marks the beginning of the function header.
• Function Name must follow the naming rules same as for identifiers.
• Function may or may not contain arguments.
• A colon(:) marks the end of function header.
• All statements within the same block must have the same indention.
Variations in user defined function:
• Function with no argument and no return type.
• Function with argument but with no return type.
• Function with arguments and return type.
• Function with no argument and return type.
➢ Points to Remember:
✓ If a function does not return any value, by default it return None.
✓ A function can return more than one value in the form of tuple.
✓ The return statement ends the function execution even if it is in the middle
of the [Link] statement written after return statement will not
executed.
Arguments Parameters
Values being passed are Values being received
called arguments. are called Parameters.
Arguments appear in the Parameters appears in
function call statement . function Header or
Function Definition.
Arguments are also Parameters are also
known as Actual known as Formal
Arguments or Actual Parameters or Formal
parameters Arguments.
Passing Parameter:
[Link] Required Parameter:
Positional Parameter are arguments passed to a function in correct positional [Link] we change their order
then the result will be [Link] we change the number of arguments passed then it gives an error.
[Link] Arguments: Default arguments is an argument that assumes a default values if a value is not
provided in the function call for that [Link] you are not passing any value default value will be
[Link] a function header ,any parameter can not have a default value unless all parameter appearing
on its right have their default value.
[Link]/Named Arguments:
To get control and flexibility over the values sent as argument ,python offers keyword [Link] allows
to call function with argument in any order using name of the arguments.
Flow of execution: The flow of execution refers to the order in which statements are executed during a
program run.
def Display1(): Function Declaration and Definition
print(“Hello”)
def Display2(): Function Declaration and Definition
print(“How are you”)
def Display3(): Function Declaration and Definition
print(“Hi where are you going”)
Display2() Function calling
Display1() Function calling
Display3() Function Calling
Output:
How are you
Hello
Hi where are you going
Scope of variable: Scope means part of the program in which a variable can be used.
[Link] scope: The variable declared inside the function definition are called local variable.
Example:
def abc():
L=20
print(L)
print(L)
abc()
Output: It give an error because L is not defined.
Because here L is a local variable for the function ,it can not be used outside the function. Local variable is
not accessible outside the function.
[Link] Scope: The variables declared outside the function definition are called Global variable.
L=20
def abc():
print(L)
print(L)
abc()
Note: [Link] will be given to the local variable inside the function.
[Link] in any program ,there are two variables with same name one is local and one is global to use the global
variable in a function we have to use the global keyword with variable name.
[Link] a function changes the value of a global variable it will be reflected back outside the function also.
Difference Between Local Variable and Global Variable:
Local Variable Global Variable
It is a variable which is declared with in the It is a variable which is declared outside the
function. function.
It is accessible with in the function only in which it It is accessible throughout the program.
is declared.
LEGB Rule:
if all variable in different scope ,are having same variable name preference will be given to Local variable.
If there is no variable in Local scope with that name ,it will check the variable in enclosing variable.
If there is no variable in a scope with that name ,it will check the Global variable.
If there is no variable in Global scope with that name ,it will check the Built-in variable.
Mutable and Immutable properties of Data Objects:
Depending upon the mutability or immutability of the data type a variable behaves differently.
Immutable Type: If a variable is referring to an immutable type then any change in its value will also change
the memory address it is referring to. Any change in the value of a immutable type will not get reflected in the
original value outside the function.
Mutable Type: If a variable if referring to mutable type ,and then any change in the value of mutable type, and
then any change the memory address of the variable ,it will change the value in place. Any change in the value
of a mutable type will get reflect in the original value outside the function also
Mutable Data Objects Immutable Data Objects
Mutable data Objects can be modified after their creation Immutable data Objects can’t be modified after their
creation
Mutable data objects are passed to functions with reference Immutable data objects are passed to function with value
Example of Mutable data objects are list and dictionaries Example of Mutable data objects are
integer,string,tuple,float etc.
MCQ(Multiple Choice Questions)
[Link] is the default value of a function that does
not return any value.
5. Which of the following function header is correct.
a) None b) int c)double d) null
a) def f (a=1,b):
[Link] of the following items are present in the b) def f (a=1,b,c=1):
function header? c) def f (a=1,b=1,c=2):
d) def f( a=1,b=1,c,d=2):
a) Function name only [Link] of the following statement is not true for
b) parameter List parameter passing to function?
a) You can pass positional arguments in any order.
c) Both function name and parameter list only
b) You can pass keyword arguments in any order.
d) Return value. c) You can call a function with positional and
keyword arguments.
3. Which of the following keyword marks the beginning
d) Positional arguments must be before keyword
of the function block.
argument in a function call.
a) func b) define c) def d) function
7. Which of the following function header is correct?
4. Pick one of the following statements to correctly
a)def cal_si(p=100,r,t=2)
complete the function body in the given snippet:
b) def cal_si(p=100,r=8,t)
def f (number): c) def cal_si(p,r=8,t)
d) def cal_si(p,r=8,t=2)
#missing function body [Link] is the result of this code?
print(f(5)) def print_double(x)
a) return “number” print(2**x)
b) print(number)
c) print(“number”) print_double(3)
d) return number
a) 8 c) 8
b) 6 d) 10
[Link] is the difference between a parameter
[Link] of the following is a correct way to call the
and an argument?
function.
a) Parameters are input passed to the function
a) my_fun() ,while arguments are variable defined inside the
b) def my_fun() function.
c) return my_fun() b) Parameters are variables defined inside the
d) call my_fun() function, while arguments are input passed to
[Link] of the given argument types can be skipped the function.
from the function call. c) Parameters and arguments are the same thing.
d) None of above.
a) Positional arguments. [Link] is the Difference between positional
b) keyword arguments parameters and keyword parameters?
c) Default arguments a)Positional parameters are specified by their
d) variable arguments. position in the function call ,while keyword
Q11. For a Function header as follows: parameters are specified by their name.
def Calc(X,Y=20): b) Positional parameters are specified by their
Which of the following function calls will give an Error? name in the function call, While keywords
a) Calc(15,25) c) Calc(X=15,Y=25) parameters are specified by their position.
b) Calc(Y=25) d) Calc(X=25) c) Positional parameters and keyword parameters
[Link] of the following is not correct in context of are the same thing.
scope of variable. d) None of the above.
a) Global keyword is used to change value of a global [Link] of the following is not correct in context
variable in a local scope of scope of a variable?
b) Local keyword is used to change value of a local a) Global keyword is used to change value of a
variable in global variable. global variable in local variable.
c) Global variable can be accessed without using b) Local keyword is used to change value of a local
global keyword in a local scope. variable in a global scope.
d) Local variable can not be used outside its scope. c) Global variable can be accessed without using
[Link] type of function are already provided by the global keyword in a local scope.
the programming language. d) Local variables can not be used outside its
a) User-defined function scope.
b) Built in function [Link] of the following is not correct in context
c) Module function of Positional and Default parameters in python
d) None of the above functions?
Q14. Which of the following functions are defined a) Default parameters must occur to the right of
in external files. Positional parameters.
a) User-Defined Function b) Positional parameters must occur to the right
b) Built in Function of Default parameters.
c) Module Function c) Positional parameters must occur to the left of
d) None of the above Default parameters.
Q15. What is a parameter in function. d) All parameters to the right of a Default
a) The input passed to the function parameters must also have Default values.
b) The output return to the function
c) The name of the function Q21. What are the two main types of functions?
d) The type of function (i) Custom function
Q16. What is an argument in a function? (ii) Built in function
a) The input passed to the function (iii) User define function
b) The output passed to the function (iv) System function
c) The name of the function a) (i) and (ii) c)(iii) and (iv)
d) The type of the function b) (ii) and (iii) d) (i) and (iv)
Explanation: The Python built in function are defined
as the functions whose functionality is pre-defined.
[Link] of the following is the use of id() The Python interpreter has several functions that are
function in python? always present for use.
Q2. Assertion (A): Key word arguments are related to
a) id() returns the identity of the object the function calls.
b) Every object doesn't have a unique ID Reason (R): When you use keyword arguments in a
c) All of the mentioned function call, the caller identifies the arguments by
d) None of These the parameter name.
Q23. Which of the following is a mathematical Ans. Option (A) is correct.
function? Explanation: Keyword argument is type of function
a) sqrt () argument are related to the function calls. When we
b) Rhombus () use keyword arguments in a function call, The caller
c) add () identifies the argument by the parameter name.
d) Rqrt Add ()
Q24.A function is said to be if it calls itself Q3. Assertion (A): A function is a block of organized
and reusable code that is used to perform a single
a)Built function related action.
b)Pre-defined function Reason (R): Function provide better modularity for
your application and a high degree of code
c)recursive function
reusability.
d)None of the above Ans. Option (A) is correct.
[Link]……..of a variable is the area of a Explanation: A function is a block of organized and
program where it may be referenced reusable code that is used to perform a single related
action. Function begin with the keyword def followed
a)Library function by function name and parenthesis().
b)Void function
Q4. Assertion (A): Default parameters are used in
c) Fruitful function
Python functions to specify values for arguments that
d) None of the above are not explicitly passed by the caller.
Q25.A Function that does not have any Reason (R): This allows the function to have a default
return value is known as……………… behaviour when the caller does not provide a value
for an argument.
a)External
Ans. Option (A) is correct.
b)Global Q5. Assertion (A): A function can return multiple
c)Scope values in python.
Reason (R): This is achieved by returning a tuple
d)Local
containing multiple values.
Ans. Option (A) is correct.
Assertion and Reason: Q6. Assertion (A): Key word arguments are related to
a)- Both A and R are true, and R is the correct the function calls.
explanation of A. Reason (R): When you use keyword arguments in a
b) - Both A and R are true, but R is NOT the correct function call, the caller identifies the arguments by
explanation of A. the parameter name.
c) - A is true, but R is false. Ans. Option (A) is correct.
d) - A is false, but R is true.
Q7. Assertion: len(),type(),int(),input() are the functions that
Q1. Assertion (A): Built in function are predefined in are always available to use.
the language that are used directly. Reason: Built in functions are predefined functions that
Reason (R): print () and input () are built in functions are always available for use. For using
Ans. Option (B) is correct. them we don’t need to import any
module.
Ans. Option (A) is correct. positional arguments.
Reason: During a function call the arguments list first
contains default arguments followed by
Assertion:Every function returns a value if the function
positional arguments.
does not explicitly return a value, then it
will return ‘None’.
Ans. Option (C ) is correct.
Reason :Zero is equivalent to None. Q15. Assertion(A): Keyword arguments in python function
can be passed in any order.
Ans. Option (C ) is correct. Reason: This is because python assign values to keyword
arguments based on their names not their
[Link](A): A variable declared outside a function has position.
global scope in python. Ans. Option (A ) is correct.
Reason: It means that it can be accessed and modified from [Link]: A function can return multiple values in
inside any function. Python.
Ans. Option (A ) is correct. Reason: Python functions can return values as a tuple.
Ans. Option (A ) is correct.
[Link](A): A local variable define inside a function
has local scope in Python.
Fill in the blank:
Reason: This means that it can only be accessed and
modified from inside the same function. [Link] variable defined outside the function definition is
Ans. Option (A ) is correct. called as Global Variable.
[Link](A): A function can have both positional and
keyword arguments in Python. [Link] variable defined inside the function definition is
Reason: This allows the caller to specify some arguments by called as Local Variable.
position and others by name.
Ans. Option (A ) is correct. [Link] a function is returning multiple values ,by default it
returns in the form of Tuple.
[Link](A): For changes made to a variable defined [Link] parameter used in the function call statement are
with in the function to be visible outside
the function, it should be declared as called Actual Parameters.
global.
[Link] parameters used in the function Header are called
Reason: Variable defined with in the function a function are
local to that function by default ,unless Formal Parameters.
explicitly specified with the global variable.
6.A parameters having default value in the function
Ans. Option (A ) is correct.
header is known as Default Arguments.
[Link](A): If the arguments in a function call 7.A Default Argument can be skipped in the function call
statement match the number and order of
arguments as defined in the function statement.
definition such arguments are called
[Link] Argument are the named arguments with
positional arguments.
Reason: Variable defined with in the function a function associated values being passed in the function call
are local to that function by default ,unless explicitly
statement.
specified with the global variable
Ans. Option (C ) is correct.
Q13. Assertion(A): Positional arguments in python function
must be passed in the exact order in which
they are defined in the function signature.
Reason: This is because Python Function automatically
assign default values to positional
argument.
Ans. Option (A ) is correct.
Q14. Assertion(A): If the arguments in the function call
statement match the number of order of
arguments as defined in the function
definition, such arguments are called
Output Based Questions: (3 Marks) Itration-6:
while 5<8: (True)
Q1 if(Str1[1]>=”A” and Str1[1]<=”M”: (false)
Str1='EXAM2018' (CBSE SPQ-19-20) elif Str1[1]>=”0” and Str1[1]<=”9”: (True)
Str2='' Str2=X*M2M+”2”
I=0 Str2=X*M2M2
while I<len(Str1): I=5+1
if Str1[I]>='A' and Str1[I]<='M': I=6
Str2=Str2+Str1[I+1] Itration-7:
elif Str1[I]>='0' and Str1[I]<='9': while 6<8: (True)
Str2=Str2+(Str1[I-1]) if(Str1[1]>=”A” and Str1[1]<=”M”: (false)
else: elif Str1[1]>=”0” and Str1[1]<=”9”: (True)
Str2=Str2+'*' Str2=X*M2M2+”0”
I=I+1 Str2=X*M2M20
print(Str2) I=6+1
Solution: I=7
Itration-1: Itration-8:
Str1=”EXAM2018” while 7<8: (True)
Str2=”” if(Str1[1]>=”A” and Str1[1]<=”M”: (false)
I=0 elif Str1[1]>=”0” and Str1[1]<=”9”: (True)
while 0<8: (True) Str2=X*M2M20+”1”
if(Str1[0]>=”A” and Str1[0]<”M”: (True) Str2=X*M2M201
Str2=””+Str1[0+1] I=7+1
Str2=””+X I=8
Str2=X Itration-8:
I=0+1 while 8<8: (False) (Loop Terminates`)
I=1
Itration-2: Q2
while 1<8: (True) def fun(s): (CBSE SPQ-19-20)
if(Str1[1]>=”A” and Str1[1]<=”M”: (false) k=len(s)
elif Str1[1]>=”0” and Str1[1]<=”9”: (false)
m=''
else:
Str2=X+”*”
for i in range(0,k):
Str2=X* if(s[i].isupper()):
I=1+1 m=m+s[i].lower()
I=2 elif s[i].isalpha():
Itration-3: m=m+s[i].upper()
while 2<8: (True) else:
if(Str1[1]>=”A” and Str1[1]<=”M”: (True) m=m+"bb"
print(m)
Str2=X*+”M” fun("school2@com")
Str2=X*M
Solution:
I=2+1
K=11
I=3
for i in range(0,11): (i=0,1,2,3…….10)
Itration-4:
i=0
while 3<8: (True)
if(s[0].isupper()): (false)
if(Str1[1]>=”A” and Str1[1]<=”M”: (True)
elif s[0].isalpha(): (True)
m=m+s[0].upper()
Str2=X*M+”2”
m=’’’’+S
Str2=X*M2
m=S
I=3+1
i=1
I=4
if(s[1].isupper()): (false)
Itration-5:
elif s[1].isalpha(): (True)
while 4<8: (True)
m=m+s[1].upper()
if(Str1[1]>=”A” and Str1[1]<=”M”: (false)
m=S+C
elif Str1[1]>=”0” and Str1[1]<=”9”: (True)
m=SC
Str2=X*M2+”M”
i=2
Str2=X*M2M
if(s[2].isupper()): (false)
I=4+1
elif s[2].isalpha(): (True)
I=5
m=m+s[2].upper()
m=SC+H Q3.
m=SCH def fun(s):
n=len(s)
i=3 m=''
if(s[3].isupper()): (false) for i in range(0,n):
elif s[3].isalpha(): (True) if(s[i]>='a' and s[i]<='m'):
m=m+s[3].upper() m=m+s[i].upper()
m=SCH+O elif(s[i]>='n' and s[i]<='z'):
m=SCHO m=m+s[i-1]
i=4 elif(s[i].isupper()):
if(s[4].isupper()): (false) m=m+s[i].lower()
elif s[4].isalpha(): (True) else:
m=m+s[4].upper() m=m+'#'
m=SCHO+O print(m)
m=SCHOO fun('Gini%Jony')
i=5 Solution:
if(s[5].isupper()): (false) n=9
elif s[5].isalpha(): (True) for i in range(0,9): (i=0,1,2,3…….8)
m=m+s[5].upper() i=0
m=SCHOO+L if(s[0]>=”a” and s[0]<=”m”: (false)
m=SCHOOL elif(s[0]>=”n” and s[0]<=”z”: (false)
i=6 elif(s[0].isupper()): (True)
if(s[6].isupper()): (false) m=””+g
elif s[6].isalpha(): (false) m=g
else: i=1
m=SCHOOL+’bb’ if(s[1]>=”a” and s[1]<=”m”: (True)
m=SCHOOLbb m=g+I
i=7 m=gI
if(s[7].isupper()): (false) i=2
elif s[7].isalpha(): (false) if(s[2]>=”a” and s[2]<=”m”: (false)
else: elif(s[2]>=”n” and s[2]<=”z”: (True)
m=SCHOOLbb+’bb’ m=gI+i
m=SCHOOLbbbb m=gIi
i=8 i=3
if(s[8].isupper()): (false) if(s[3]>=”a” and s[3]<=”m”: (True)
elif s[8].isalpha(): (True) m=gIi+I
m=m+s[8].upper() m=gIiI
m=SCHOOLbbbb+C i=4
m=SCHOOLbbbbC if(s[4]>=”a” and s[4]<=”m”: (false)
elif(s[4]>=”n” and s[4]<=”z”: (false)
i=9 elif(s[4].isupper()): (false)
if(s[9].isupper()): (false) else: (True)
elif s[9].isalpha(): (True) m=gIi+#
m=m+s[9].upper() m=gIiI#
m=SCHOOLbbbbC+O i=5
m=SCHOOLbbbbCO if(s[5]>=”a” and s[5]<=”m”: (false)
elif(s[5]>=”n” and s[5]<=”z”: (false)
i=10 elif(s[5].isupper()): (True)
if(s[10].isupper()): (false) m=gIi#+j
elif s[10].isalpha(): (True) m=gIiI#j
m=m+s[10].upper() i=6
m=SCHOOLbbbbCO+M if(s[6]>=”a” and s[6]<=”m”: (false)
m=SCHOOLbbbbCOM elif(s[6]>=”n” and s[6]<=”z”: (True)
m=gIiI#j+J
Final output: SCHOOLbbbbCOM m=gIiI#jJ
i=7
if(s[7]>=”a” and s[7]<=”m”: (false)
elif(s[7]>=”n” and s[7]<=”z”: (True)
m=gIiI#jJ+o
m=gIiI#jJo
i=8 count=3
if(s[8]>=”a” and s[8]<=”m”: (false) for i in sTUdeNT:
elif(s[8]>=”n” and s[8]<=”z”: (True) if 3%2!=0 : (True)
m=gIiI#jJo+n
m=gIiI#jJon newstr=S1U+str(3)
newsrt=S1U3
Final output: gIiI#jJon count=3+1
count=4
Iteration 5:
Q4. count=4
def makenew(mystr): for i in sTUdeNT:
mystr="sTUdeNT" if 4%2!=0 : (false)
newstr="" else:
count=0 if [Link](): (True)
for i in mystr: newstr=S1U2+E
if count%2!=0: newstr=S1U2E
newstr=newstr+str(count) count=4+1
else: count=5
if [Link](): Iteration 6:
newstr=newstr+[Link]() count=5
else: for i in sTUdeNT:
newstr=newstr+i if 5%2!=0 : (True)
count+=1 newstr=S1U2E+5
newstr=newstr+mystr[:1] newstr=S1U2E5
print(newstr) count=5+1
makenew("sTUdeNT") count=6
Iteration 7:
Ans: count=6
mystr=sTUdeNT for i in sTUdeNT:
newstr=”” if 6%2!=0 : (false)
count=0 else:
Iteration 1: if [Link](): (false)
for i in sTUdeNT: else:
if 0%2!=0 : (false) newstr=S1U2E5
else: newstr=S1U2E5T
if [Link](): (True) count=6+1
newstr=””+ S count=7
newstr=S newstr=S1U2E5T+s
count=0+1
count=1 Final Output: S1U2E5Ts
Iteration 2:
count=1 Q5.
for i in sTUdeNT: def makenew(mystr):
if 1%2!=0 : (True) newstr=""
newstr=S+str(1) count=0
newstr=S1 for i in mystr:
count=1+1 if count%2!=0:
count=2 newstr=newstr+str(count)
Iteration 3: else:
count=2 if [Link]():
for i in sTUdeNT: newstr=newstr+[Link]()
if 2%2!=0 : (false) else:
else: newstr=newstr+i
if [Link](): (false) count+=1
else: print(newstr)
newstr=S1+U makenew("No@1")
newstr=S1U
count=2+1 Solution:
count=3 mystr=”No@1”
Iteration 4: newstr=””
count=0 for i in range(0,8)
for i in mystr: (value of i=N,o,@,1) value of i=0,1,2,3,4,5,6,7
i=N (1st value of i) Now
if 0%2!=0 → (false) i=0 (value at 0 index is E)
else: if str1[0]>=”A” and str1[0]<=”M”: (True)
if [Link](): → (false str2=str2+str1[i+1]
else: str2=’ ‘ +str1[0+1]
newstr=newstr+I (Put the value of newstr and i) str2=’ ‘+X
newstr=” ”+ N str2=X
newstr=N i=1 (value at 1 index is X)
count=count+1 if str1[1]>=”A” and str1[1]<=”M”: (False)
count=0+1 elif srt[1]>=”0” and str1[1]<=”9”: (False)
count=1 else:
i=o (2nd value of i) str2=str2+”*”
if 1%2!=0 → (True) str2=X+”*”
newstr=newstr+str(count) str2=X*
newstr=N+str(1) i=2 (value at 2 index is A)
newstr=N1 if str1[2]>=”A” and str1[2]<=”M”: (True)
count=1+1=2 str2=str2+str1[i+1]
i=@(3rd value of i) str2=’X* ‘ +str1[2+1]
if 2%2!=0 → (false) str2=X*M
else: i=3 (value at 3 index is M)
if [Link](): → (false) if str1[2]>=”A” and str1[2]<=”M”: (True)
else: str2=str2+str1[i+1]
newstr=newstr+i (Put the value of newstr and i) str2=’X*M ‘ +str1[3+1]
newstr=N1+@ str2=X*M+2
newstr=N1@ str2=X*M2
count=count+1 i=4 (value at 4 index is 2)
count=2+1 if str1[2]>=”A” and str1[2]<=”M”: (False)
count=3 elif str1[i]>="0" and str1[i]<="9": (True)
i=1 (4th value of i) str2=str2+str1[i-1]
if 3%2!=0 → (True) str2=X*M2+str1[4-1]
newstr=newstr+str(count) str2=X*M2+M
newstr=N1@+str(3) str2=X*M2M
newstr=N1@3 i=5 (value at 5 index 0)
count=3+1=4 if str1[2]>=”A” and str1[2]<=”M”: (False)
elif str1[i]>="0" and str1[i]<="9": (True)
Loop completed and terminated str2=str2+str1[i-1]
Final output: str2=X*M2+str1[5-1]
N1@3 str2=X*M2M+2
str2=X*M2M2
Q6
def fun(str1): i=6 (value at 6 index 2)
n=len(str1) if str1[2]>=”A” and str1[2]<=”M”: (False)
str2='' elif str1[i]>="0" and str1[i]<="9": (True)
for i in range(0,n): str2=str2+str1[i-1]
if str1[i]>="A" and str1[i]<="M": str2=X*M2+str1[6-1]
str2=str2+str1[i+1] str2=X*M2M2+0
elif str1[i]>="0" and str1[i]<="9": str2=X*M2M20
str2=str2+str1[i-1] i=7 (value at 7 index 5)
else: if str1[2]>=”A” and str1[2]<=”M”: (False)
str2=str2+"*" elif str1[i]>="0" and str1[i]<="9": (True)
print(str2) str2=str2+str1[i-1]
fun("EXAM2025") str2=X*M2+str1[7-1]
str2=X*M2M20+2
Solution: str2=X*M2M202
str1=EXAM2025
n=len(str1) Final Output:
n=8 X*M2M202
str2=””
str2=str2+"$"
Q7. str2=EVENE+”$”
def fun(str1): str2=EVENE$
n=len(str1) i=6
str2='' if str1[6]>="A" and str1[6]<="M": (False)
for i in range(0,n): elif str1[6]>="N" and str1[6]<="Z": (False)
if str1[i]>="A" and str1[i]<="M": elif str1[6]>="0" and str1[6]<="9": (True)
str2=str2+str1[i+1] str2=str2+"#"
elif str1[i]>="N" and str1[i]<="Z": str2=EVENE$+”#”
str2=str2+str1[i-1] str2=EVENE$#
elif str1[i]>="0" and str1[i]<="9":
str2=str2+"#" i=7
else: if str1[7]>="A" and str1[7]<="M": (False)
str2=str2+"$" elif str1[7]>="N" and str1[7]<="Z": (False)
print(str2) elif str1[7]>="0" and str1[7]<="9": (True)
fun("HEVEN@2021") str2=str2+"#"
str2=EVENE$#+”#”
Solution: str2=EVENE$##
Str1=HEVEN@2021
n=10 i=8
str2=’ ‘ if str1[8]>="A" and str1[8]<="M": (False)
for i in range(0,10) elif str1[8]>="N" and str1[8]<="Z": (False)
value of i=0,1,2,3,4,5,6,7,8,9 elif str1[8]>="0" and str1[8]<="9": (True)
str2=str2+"#"
Now i=0 str2=EVENE$##+”#”
if str1[0]>="A" and str1[0]<="M": (True) str2=EVENE$###
str2=str2+str1[0+1] i=9
str2=’ ‘+str1[1] if str1[9]>="A" and str1[9]<="M": (False)
str2=’ ‘+E elif str1[9]>="N" and str1[9]<="Z": (False)
str2=E elif str1[9]>="0" and str1[i]<="9": (True)
i=1 str2=str2+"#"
if str1[1]>="A" and str1[1]<="M": (True) str2=EVENE$###+”#”
str2=str2+str1[1+1] str2=EVENE$####
str2=E+str1[2]
str2=E+V Final Output
str2=EV EVENE$####
i=2
if str1[2]>="A" and str1[2]<="M: (False) Q8.
elif str1[2]>="N" and str1[2]<="Z": (True) def change(P,Q=30): (CBSE SPQ-19-20)
str2=str2+str1[2-1] P=P+Q
srt2=EV+str1[1] Q=P-Q
str2=EV+E print(P,"#",Q)
str2=EVE return(P)
i=3 R=150
if str1[3]>="A" and str1[3]<="M": (True) S=100
str2=str2+str1[3+1] R=change(R,S)
str2=EVE+str1[4] print(R,"#",S)
str2=EVE+N S=change(S)
str2=EVEN
i=4 solution:
if str1[4]>="A" and str1[4]<="M": (False) R=150 S=100
elif str1[4]>="N" and str1[4]<="Z": ( True) First call (R=change(R,S))( The value of R assigned to P
str2=str2+str1[4-1] and Value of S assigned to Q)
str2=EVEN+E P=150 Q=100
str2=EVENE P=150+100
i=5 P=250
if str1[5]>="A" and str1[5]<="M": (False) Q=250-100
elif str1[5]>="N" and str1[5]<="Z": (False) Q=150
elif str1[5]>="0" and str1[5]<="9": (False) Print(P,”#”,Q) →First Print
else: 250 #150
After first call the value of P=250 returned and stored in Q=130-30
variable R ,Now R become 250 Q=100
Then print statement will get executed print(130 ,’#’ ,100) → Third print
print(R,’#’,S) →Second Print return P ( Value of P is 130)
it will print 250 # 100 S=130
print( 250 ,”#”,130) → Fourth Print
Second Call (S=change(S)): change(s) have only one
variable ,The value of S=100 which is assigned to P and Third Call(S=change(R))
the value of Q remain same not changed) R=250
P=100 Q=30 Value of R is allocated to P=250 ,Q=30
P=P+Q P=P+Q
P=100+30 P=250+30
P=130 P=280
Q=P-Q Q=P-Q
Q=130-30 Q=280-30
Q=100 Q=250
print(P,”#”,Q) →Third Print print( 280,”#”,250) → Fifth Print
130 # 100 return P ( Value of P is 280)
now S=280
Final Output: print( 250 ,”#” 280) → Sixth Print
250 #150
250 # 100 Final Output
130 # 100 250 # 150
250 # 100
Q9: 130 # 100
def change(P,Q=30): 250 # 130
P=P+Q 280 # 250
Q=P-Q 250 # 280
print(P,"#",Q)
return(P) 10:
R=150 def alter(P=15,Q=10):
S=100 P=P*Q
R=change(R,S) Q=P/Q
print(R,"#",S) print(P,"#",Q)
S=change(S) return Q
print(R,"#",S) A=100
S=change(R) B=200
print(R,"#",S) A=alter(A,B)
print(A,"$",B)
Solution: B=alter(B)
R=150 S=100 print(A,"$",B)
First Call(R=change(R,S)) A=alter(A)
Value of R assigned to P value of S is assigned to Q print(A,"$",B)
Now P=150 Q=100
P=P+Q Solution:
P=150+100=250 A=100 B=200
Q=P-Q First Call ( A=alter(A,B))
Q=250-100=150 P=100 Q=200
print(250 ,’#’ ,150) → First print P=P*Q
return P ( Value of P is 250) P=100*200
Now R=250 P=20000
print( 250,”#”,100) → Second Print Q=P/Q
Q=20000/200
Second Call(S=change(S)) Q=100.0
Value of S assigned to P ,Q Remained Unchanged print(P,”#”,Q) → First Print
P=100 Q=30 20000 # 100.0
P=P+Q return Q (Q=200.0)
P=100+30 Now return value assigned to A ,Now A=100.0
P=130 print( A,”$”,B) → Second Print
Q=P-Q 100.0 $ 200
Second Call ( B=alter(B)) P=P/Q
B=200 P=200/20
P=200 Q=10 P=10.0
P=P*Q Q=P%Q
P=200*10=2000 Q=10.0%20
Q=P/Q Q=10.0
Q=2000/10 print(P,”#”,Q) → First Print
Q=200.0 10.0 # 10.0
print(P,”#”,Q) → Third Print return P (value of P is 10.0 it will return to the calling
2000 # 200.0 function and stored in a variable A,Now A=10.0)
return Q (Q=200.0) print(A,”$”,B) → second print
Now return value assigned to B ,Now B=100.0 10.0 $ 20
A=100.0 Second Call(B=change(B))
print( A,”$”,B) → Fourth Print Only one argument is here the value of B is assigned to P
100.0 $ 200.0 i.e 20 and value of Q is 10
Third Call ( A=alter(A)) P=P/Q
A=100.0 ,B=200.0 P=20/10
Value of A is assigned to P=100.0 and Q=10 P=2.0
P=P*Q Q=P%Q
P=100.0*10 Q=2.0/10
P=1000.0 Q=2.0
Q=P/Q print(P,”#”,Q) → Third Print
Q=1000.0/10 2.0 # 2.0
Q=100.0 return P, (value of P is 2.0 it will return to the calling
print(P,”#”,Q) → Fifth Print function and stored in a variable B ,Now B=2.0)
1000.0 # 100.0 print(A,”$”,B) → Fourth Print
return Q (Q=100.0) 10.0 $ 2.0
Now return value assigned to A ,Now A=100.0 and Third Call(A=change(A))
B=200.0 Only one argument is here the value of A is assigned to P
print( A,”$”,B) → Sixth Print i.e 10.0 and value of Q is 10
100.0 $ 200.0 P=P/Q
P=10.0/10
Final Output P=1.0
20000 # 100.0 Q=P%Q
100.0 $ 200 Q=1.0/10
2000 # 200.0 Q=1.0
100.0 $ 200.0 print(P,”#”,Q) → Fifth Print
1000.0 # 100.0 1.0 # 1.0
100.0 $ 200.0 return P, (value of P is 1.0 it will return to the calling
function and stored in a variable A ,Now A=1.0)
Q11 print(A,”$”,B) → Sixth Print
def change(P,Q=10): (CBSE Compartment-20) 1.0 $ 2.0
P=P/Q
Q=P%Q Final Output:
print(P,"#",Q) 10.0 # 10.0
return P 10.0 $ 20
A=200 2.0 # 2.0
B=20 10.0 $ 2.0
A=change(A,B) 1.0 # 1.0
print(A,"$",B) 1.0 $ 2.0
B=change(B)
print(A,"$",B)
A=change(A)
print(A,"$",B)
Solution:
A=200 B=20
First Call(A=change(A,B))
Value of A is assigned to P and value of B is assigned to Q
P=200 Q=20
Q12. Note(if is a compression
def call(P=40,Q=20): (CBSE 2021) statement so == use for
P=P+Q compression.
Q=P-Q print[],print{} print()
print(P,"@",Q)
return P
R=200 Q1.
S=100 Def exam():
R=call(R,S) X=input(“Enter the number”)
print(R,"@",S) If (ab(X)=X):
S=call(S) Print(“You entered a positive number”)
print(R,"@",S) Else
X=*-1
Solution: Print(“Number made positive”)
R=200 S=100 Exam()
First Call ( R=call(R,S))
The value of R is assigned to P and value of S is assigned Solution:
to Q def exam():
P=200 and S=100 X=int(input(“Enter the number”))
P=200+100 if(ab(X)==X):
P=300 print(“You entered a positive number”)
Q=P-Q else:
Q=300-100 X*=-1
Q=200 print(“Number made Positive”)
print(P, “@”,Q) → First Print exam()
300 @ 200
return P, (value of P is 300 it will return to the calling
function and stored in a variable R ,Now R=300) Q2. def checksum:
print(R ,”@”,S) → Second Print X=int(input(“Enter the number”))
300 @ 100 If (X%2=0):
Second Call ( S=call(S)) For i range(2*X):
(Only one argument is here the Value of S is assigned to P print(i)
,Now P is 100 and Q remain same Q=20) loop else:
P=P+Q P=100+20 =120 print(“#”)
Q=P-Q Solution:
Q=120-20=100
print( P,”@”,Q) → Third Print def checksum():
120 @100 X=int(input(“Enter the number”))
return P, (value of P is 120 it will return to the calling if (X%2==0):
function and stored in a variable S ,Now S=120) for i range(2*X):
R is 300 print(i)
print(R,“@”,S) → Fourth Print else:
300 @ 120 print(“#”)
Final Output Q3.
300 @ 200 def T(Number)
300 @ 100 Sum=0
120 @ 100 for C in Range(I,Number+I):
300 @ 120 Sum=+C
Return Sum
Error Based Question (2M) Print [3]
Possible Errors Correct Form Print[6]
Def,If,Print,Else,Elif,For Def,if,print Solution:
,Return ,Range ,else,elif,for,return,range def T(Number):
Missing of : after function Put : after that Sum=0
definition ,if statement for C in range (I,Number+I):
,for loop Sum=+C
=>,=<,=! >=,<=,!= return Sum
A=+B A+=B print (3)
if(a=b): or if a=b: If(a==b): or if a==b: print(6)
15
Q4. Final output:
Def checkvel 25
X=int(input(“Enter number”)) 15
If x%2=0
Print(“x is even”) Q3.
Else if x<0: x=25
Print x is should be positive def change():
Else; x=4
Print x is odd print(x)
Solution: x=2
def checkvel(): print(x)
X=int(input(“Enter number”)) print(x)
If X%2==0 change()
print(“x is even”) Solution:
elif x<0: print(x)
print(“ x is should be positive”) 25
else: Function call
print(“ x is odd”) x=4
print(x)
Question Based on Scope of variable: 4
x=2
Q1. print(x)
v=25 #global variable 2
def fun(ch): Final output
v=50 #local variable 25
print(v,end=ch) 4
print(v,end="*") 2
fun("!")
print(v) Q4.
c=10
Solution: def add():
v=25 global c
print(v,end==”*”) c=c+2
25* print(c,end="#")
ch=! add()
50 ! c=15
print(v) print(c,end="%")
25
Final Output: Solution:
25*50 ! 25 Function call add()
c=c+2
Q2. c=10+2
x=25 #global variable c=12
def change(): print(c,end=”#”)
x=10 #local variable 12 #
x=x+5
print(x) c=15
print(x) print(c,end=”%”)
change() 15 %
Solution: Final Output:
x=25 12#15%
print(x)
25
Function call
x=10
x=10+5
x=15
print(x)
Q5.
p=1
q=6
def change_values():
global p
q=5
p=p+q
return p
change_values()
print(p,q)
Solution :
Final Output:
66
Q6
x=5
def multiply():
global x
x=x*2
print(x,end="@")
multiply()
x=10
print(x,end="!")
Solution
Final output:
10@ 10!
Q7.
def outer():
x="Hello"
def inner():
nonlocal x
x="World"
inner()
return x
print(outer())
Final Output:
World
Note:nonlocal keyword used in the nested function it is
work like as a global keyword.
Q8.
def add(num1,num2=5):
return num1+num2
result=add(10)
print(result)
Output:
15