Docsity

Maths notes .........., Study Guides, Projects, Research of Mathematics

Toggle sidebar
bg1
Govt.Model Sanskriti Senior Secondary School,Vyaspur Distt-Yamunanagar(Haryana)
Prepared By:Ravinder Chauhan 1
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
1.Built-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())
3.User 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 function.The statement written after return statement will not
executed.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Maths notes .......... and more Study Guides, Projects, Research Mathematics in PDF only on Docsity!

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

1.Built-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() )

3.User 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 function.The statement written after return statement will not

executed.

Passing Parameter:

1.Positional Required Parameter:

Positional Parameter are arguments passed to a function in correct positional order.If we change their order

then the result will be changed.If we change the number of arguments passed then it gives an error.

2.Default Arguments: Default arguments is an argument that assumes a default values if a value is not

provided in the function call for that argument.If you are not passing any value default value will be

considered.In a function header ,any parameter can not have a default value unless all parameter appearing

on its right have their default value.

3.Keyword/Named Arguments:

To get control and flexibility over the values sent as argument ,python offers keyword arguments.This 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

Arguments Parameters

Values being passed are

called arguments.

Values being received

are called Parameters.

Arguments appear in the

function call statement.

Parameters appears in

function Header or

Function Definition.

Arguments are also

known as Actual

Arguments or Actual

parameters

Parameters are also

known as Formal

Parameters or Formal

Arguments.

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)

1.What is the default value of a function that does

not return any value.

a) None b) int c)double d) null

2.Which of the following items are present in the

function header?

a) Function name only

b) parameter List

c) Both function name and parameter list only

d) Return value.

3. Which of the following keyword marks the beginning

of the function block.

a) func b) define c) def d) function

4. Pick one of the following statements to correctly

complete the function body in the given snippet:

def f (number):

#missing function body

print(f(5))

a) return โ€œnumberโ€

b) print(number)

c) print(โ€œnumberโ€)

d) return number

5. Which of the following function header is correct.

a) def f (a=1,b):

b) def f (a=1,b,c=1):

c) def f (a=1,b=1,c=2):

d) def f( a=1,b=1,c,d=2):

6.Which of the following statement is not true for

parameter passing to function?

a) You can pass positional arguments in any order.

b) You can pass keyword arguments in any order.

c) You can call a function with positional and

keyword arguments.

d) Positional arguments must be before keyword

argument in a function call.

7. Which of the following function header is correct?

a)def cal_si(p=100,r,t=2)

b) def cal_si(p=100,r=8,t)

c) def cal_si(p,r=8,t)

d) def cal_si(p,r=8,t=2)

8.What is the result of this code?

def print_double(x)

print(2**x)

print_double(3)

a) 8 c) 8

b) 6 d) 10

9.Which of the following is a correct way to call the

function.

a) my_fun()

b) def my_fun()

c) return my_fun()

d) call my_fun()

10.which of the given argument types can be skipped

from the function call.

a) Positional arguments.

b) keyword arguments

c) Default arguments

d) variable arguments.

Q11. For a Function header as follows:

def Calc(X,Y=20):

Which of the following function calls will give an Error?

a) Calc(15,25) c) Calc(X=15,Y=25)

b) Calc(Y=25) d) Calc(X=25)

Q12.Which of the following is not correct in context of

scope of variable.

a) Global keyword is used to change value of a global

variable in a local scope

b) Local keyword is used to change value of a local

variable in global variable.

c) Global variable can be accessed without using

global keyword in a local scope.

d) Local variable can not be used outside its scope.

Q13.Which type of function are already provided by

the programming language.

a) User-defined function

b) Built in function

c) Module function

d) None of the above

Q14. Which of the following functions are defined

in external files.

a) User-Defined Function

b) Built in Function

c) Module Function

d) None of the above

Q15. What is a parameter in function.

a) The input passed to the function

b) The output return to the function

c) The name of the function

d) The type of function

Q16. What is an argument in a function?

a) The input passed to the function

b) The output passed to the function

c) The name of the function

d) The type of the function

Q17.What is the difference between a parameter

and an argument?

a) Parameters are input passed to the function

,while arguments are variable defined inside the

function.

b) Parameters are variables defined inside the

function, while arguments are input passed to

the function.

c) Parameters and arguments are the same thing.

d) None of above.

Q18.What is the Difference between positional

parameters and keyword parameters?

a)Positional parameters are specified by their

position in the function call ,while keyword

parameters are specified by their name.

b) Positional parameters are specified by their

name in the function call, While keywords

parameters are specified by their position.

c) Positional parameters and keyword parameters

are the same thing.

d) None of the above.

Q19.Which of the following is not correct in context

of scope of a variable?

a) Global keyword is used to change value of a

global variable in local variable.

b) Local keyword is used to change value of a local

variable in a global scope.

c) Global variable can be accessed without using

the global keyword in a local scope.

d) Local variables can not be used outside its

scope.

Q 20 .Which of the following is not correct in context

of Positional and Default parameters in python

functions?

a) Default parameters must occur to the right of

Positional parameters.

b) Positional parameters must occur to the right

of Default parameters.

c) Positional parameters must occur to the left of

Default parameters.

d) All parameters to the right of a Default

parameters must also have Default values.

Q21. What are the two main types of functions?

(i) Custom function

(ii) Built in function

(iii) User define function

(iv) System function

a) (i) and (ii) c)(iii) and (iv)

b) (ii) and (iii) d) (i) and (iv)

Ans. Option (A) is correct.

Assertion: Every function returns a value if the function does not explicitly return a value, then it will return โ€˜Noneโ€™. Reason :Zero is equivalent to None.

Ans. Option (C ) is correct.

Q8.Assertion(A): A variable declared outside a function has global scope in python. Reason: It means that it can be accessed and modified from inside any function.

Ans. Option (A ) is correct.

Q9.Assertion(A): A local variable define inside a function has local scope in Python. Reason: This means that it can only be accessed and modified from inside the same function.

Ans. Option (A ) is correct.

Q10.Assertion(A): A function can have both positional and keyword arguments in Python. Reason: This allows the caller to specify some arguments by position and others by name.

Ans. Option (A ) is correct.

Q11.Assertion(A): For changes made to a variable defined with in the function to be visible outside the function, it should be declared as global. Reason: Variable defined with in the function a function are local to that function by default ,unless explicitly specified with the global variable.

Ans. Option (A ) is correct.

Q12.Assertion(A): If the arguments in a function call statement match the number and order of arguments as defined in the function definition such arguments are called positional arguments. Reason: Variable defined with in the function a function are local to that function by default ,unless explicitly 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.

Q1 4. 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 positional arguments. Reason: During a function call the arguments list first contains default arguments followed by positional arguments.

Ans. Option (C ) is correct.

Q1 5. Assertion(A): Keyword arguments in python function can be passed in any order. Reason: This is because python assign values to keyword arguments based on their names not their position.

Ans. Option (A ) is correct.

Q16.Assertion: A function can return multiple values in Python. Reason: Python functions can return values as a tuple.

Ans. Option (A ) is correct.

Fill in the blank: 1.The variable defined outside the function definition is called as Global Variable. 2.The variable defined inside the function definition is called as Local Variable. 3.If a function is returning multiple values ,by default it returns in the form of Tuple. 4.The parameter used in the function call statement are called Actual Parameters. 5.The parameters used in the function Header are called Formal Parameters. 6.A parameters having default value in the function header is known as Default Arguments. 7.A Default Argument can be skipped in the function call statement.

  1. Keyword Argument are the named arguments with associated values being passed in the function call statement.

Output Based Questions: (3 Marks)

Q

Str1='EXAM2018' (CBSE SPQ- 19 - 20) Str2='' I= while I='A' and Str1[I]<='M': Str2=Str2+Str1[I+1] elif Str1[I]>='0' and Str1[I]<='9': Str2=Str2+(Str1[I-1]) else: Str2=Str2+'' I=I+ print(Str2)*

Solution:

Itration-1: Str1=โ€EXAM2018โ€ Str2=โ€โ€ I= while 0<8: (True) if(Str1[0]>=โ€Aโ€ and Str1[0]<โ€Mโ€: (True) Str2=โ€โ€+Str1[0+1] Str2=โ€โ€+X Str2=X I=0+ I= Itration-2: while 1<8: (True) if(Str1[1]>=โ€Aโ€ and Str1[1]<=โ€Mโ€: (false) elif Str1[1]>=โ€0โ€ and Str1[1]<=โ€9โ€: (false) else: Str2=X+โ€โ€ Str2=X I=1+ I= Itration-3: while 2<8: (True) if(Str1[1]>=โ€Aโ€ and Str1[1]<=โ€Mโ€: (True) Str2=X+โ€Mโ€ Str2=XM I= 2 + I= 3 Itration-4: while 3<8: (True) if(Str1[1]>=โ€Aโ€ and Str1[1]<=โ€Mโ€: (True) Str2=XM+โ€2โ€ Str2=XM I=3+ I= Itration-5: while 4<8: (True) if(Str1[1]>=โ€Aโ€ and Str1[1]<=โ€Mโ€: (false) elif Str1[1]>=โ€0โ€ and Str1[1]<=โ€9โ€: (True) Str2=XM2+โ€Mโ€ Str2=XM2M I= 4 + I= 5 Itration-6: while 5<8: (True) if(Str1[1]>=โ€Aโ€ and Str1[1]<=โ€Mโ€: (false) elif Str1[1]>=โ€0โ€ and Str1[1]<=โ€9โ€: (True) Str2=XM2M+โ€2โ€ Str2=XM2M I=5+ I= Itration-7: while 6<8: (True) if(Str1[1]>=โ€Aโ€ and Str1[1]<=โ€Mโ€: (false) elif Str1[1]>=โ€0โ€ and Str1[1]<=โ€9โ€: (True) Str2=XM2M2+โ€0โ€ Str2=XM2M I=6+ I= Itration-8: while 7<8: (True) if(Str1[1]>=โ€Aโ€ and Str1[1]<=โ€Mโ€: (false) elif Str1[1]>=โ€0โ€ and Str1[1]<=โ€9โ€: (True) Str2=XM2M20+โ€1โ€ Str2=XM2M I=7+ I= Itration-8: while 8<8: (False) (Loop Terminates`) Q

def fun(s): (CBSE SPQ- 19 - 20)

k=len(s)

m=''

for i in range(0,k):

if(s[i].isupper()):

m=m+s[i].lower()

elif s[i].isalpha():

m=m+s[i].upper()

else:

m=m+"bb"

print(m)

fun("school2@com")

Solution:

K=

for i in range(0,11): (i=0,1,2,3โ€ฆโ€ฆ.10) i= if(s[0].isupper()): (false) elif s[0].isalpha(): (True) m=m+s[0].upper() m=โ€™โ€™โ€™โ€™+S m=S i= if(s[1].isupper()): (false) elif s[ 1 ].isalpha(): (True) m=m+s[1].upper() m=S+C m=SC i= if(s[2].isupper()): (false) elif s[2].isalpha(): (True) m=m+s[ 2 ].upper()

i= if(s[8]>=โ€aโ€ and s[8]<=โ€mโ€: (false) elif(s[8]>=โ€nโ€ and s[8]<=โ€zโ€: (True) m=gIiI#jJo+n m=gIiI#jJon Final output: gIiI#jJon Q 4. def makenew(mystr): mystr="sTUdeNT" newstr="" count= for i in mystr: if count%2!=0: newstr=newstr+str(count) else: if i.islower(): newstr=newstr+i.upper() else: newstr=newstr+i count+= newstr=newstr+mystr[:1] print(newstr) makenew("sTUdeNT") Ans: mystr=sTUdeNT newstr=โ€โ€ count= Iteration 1: for i in sTUdeNT: if 0%2!=0 : (false) else: if i.islower(): (True) newstr=โ€โ€+ S newstr=S count=0+ count= Iteration 2 : count= for i in sTUdeNT: if 1%2!=0 : (True) newstr=S+str(1) newstr=S count= 1 + count= 2 Iteration 3 : count= 2 for i in sTUdeNT: if 2%2!=0 : (false) else: if i.islower(): (false) else: newstr=S1+U newstr=S1U count= 2 + count= 3 Iteration 4 : count= 3 for i in sTUdeNT: if 3%2!=0 : (True) newstr=S1U+str(3) newsrt=S1U count=3+ count= Iteration 5 : count= 4 for i in sTUdeNT: if 4%2!=0 : (false) else: if i.islower(): (True) newstr=S1U2+E newstr=S1U2E count=4+ count= Iteration 6 : count= 5 for i in sTUdeNT: if 5 %2!=0 : (True) newstr=S1U2E+ 5 newstr=S1U2E 5 count= 5 + count= 6 Iteration 7 : count= 6 for i in sTUdeNT: if 6%2!=0 : (false) else: if i.islower(): (false) else: newstr=S1U2E newstr=S1U2E5T count=6+ count= newstr=S1U2E5T+s Final Output: S1U2E5Ts Q5. def makenew(mystr): newstr="" count= for i in mystr: if count%2!=0: newstr=newstr+str(count) else: if i.islower(): newstr=newstr+i.upper() else: newstr=newstr+i count+= print(newstr) makenew("No@1") Solution: mystr=โ€No@1โ€ newstr=โ€โ€

count= for i in mystr: (value of i=N,o,@,1) i=N (1st^ value of i) if 0%2!=0 โ†’ (false) else: if i.islower(): โ†’ (false else: newstr=newstr+I (Put the value of newstr and i) newstr=โ€ โ€+ N newstr=N count=count+ count=0+ count= i=o (2nd^ value of i) if 1%2!=0 โ†’ (True) newstr=newstr+str(count) newstr=N+str(1) newstr=N count=1+1= i=@(3rd^ value of i) if 2%2!=0 โ†’ (false) else: if i.islower(): โ†’ (false) else: newstr=newstr+i (Put the value of newstr and i) newstr=N1+@ newstr=N1@ count=count+ count= 2 + count= 3 i= 1 ( 4 th^ value of i) if 3%2!=0 โ†’ (True) newstr=newstr+str(count) newstr=N1@+str(3) newstr=N1@ count=3+1= Loop completed and terminated Final output: N1@ Q def fun(str1): n=len(str1) str2='' for i in range(0,n): if str1[i]>="A" and str1[i]<="M": str2=str2+str1[i+1] elif str1[i]>="0" and str1[i]<="9": str2=str2+str1[i-1] else: str2=str2+"" print(str2) fun("EXAM2025") Solution:* str1=EXAM n=len(str1) n= str2=โ€โ€ for i in range(0,8) value of i=0,1,2,3,4,5,6, Now i=0 (value at 0 index is E) if str1[0]>=โ€Aโ€ and str1[0]<=โ€Mโ€: (True) str2=str2+str1[i+1] str2=โ€™ โ€˜ +str1[0+1] str2=โ€™ โ€˜+X str2=X i= 1 (value at 1 index is X) if str1[1]>=โ€Aโ€ and str1[1]<=โ€Mโ€: (False) elif srt[1]>=โ€0โ€ and str1[1]<=โ€9โ€: (False) else: str2=str2+โ€โ€ str2=X+โ€โ€ str2=X* i= 2 (value at 2 index is A) if str1[2]>=โ€Aโ€ and str1[2]<=โ€Mโ€: (True) str2=str2+str1[i+1] str2=โ€™X* โ€˜ +str1[2+1] str2=XM i= 3 (value at 3 index is M) if str1[2]>=โ€Aโ€ and str1[2]<=โ€Mโ€: (True) str2=str2+str1[i+1] str2=โ€™XM โ€˜ +str1[3+1] str2=XM+ str2=XM i= 4 (value at 4 index is 2) if str1[2]>=โ€Aโ€ and str1[2]<=โ€Mโ€: (False) elif str1[i]>="0" and str1[i]<="9": (True) str2=str2+str1[i-1] str2=XM2+str1[4-1] str2=XM2+M str2=XM2M i=5 (value at 5 index 0) if str1[2]>=โ€Aโ€ and str1[2]<=โ€Mโ€: (False) elif str1[i]>="0" and str1[i]<="9": (True) str2=str2+str1[i-1] str2=XM2+str1[5-1] str2=XM2M+ str2=XM2M i=6 (value at 6 index 2) if str1[2]>=โ€Aโ€ and str1[2]<=โ€Mโ€: (False) elif str1[i]>="0" and str1[i]<="9": (True) str2=str2+str1[i-1] str2=XM2+str1[6-1] str2=XM2M2+ str2=XM2M i=7 (value at 7 index 5) if str1[2]>=โ€Aโ€ and str1[2]<=โ€Mโ€: (False) elif str1[i]>="0" and str1[i]<="9": (True) str2=str2+str1[i-1] str2=XM2+str1[7-1] str2=XM2M20+ str2=XM2M Final Output: XM2M*

After first call the value of P=250 returned and stored in variable R ,Now R become 250 Then print statement will get executed print(R,โ€™#โ€™,S) โ†’Second Print it will print 250 # 100 Second Call (S=change(S)): change(s) have only one variable , The value of S=100 which is assigned to P and the value of Q remain same not changed) P=100 Q= P=P+Q P=100+ P= Q=P-Q Q=130- 30 Q= print(P,โ€#โ€,Q) โ†’Third Print 130 # 100 Final Output: 250 # 250 # 100 130 # 100 Q 9 : def change(P,Q=30): P=P+Q Q=P-Q print(P,"#",Q) return(P) R= S= R=change(R,S) print(R,"#",S) S=change(S) print(R,"#",S) S=change(R) print(R,"#",S) Solution: R=150 S= First Call(R=change(R,S)) Value of R assigned to P value of S is assigned to Q Now P=150 Q= P=P+Q P=150+100= Q=P-Q Q=250-100= print(250 ,โ€™#โ€™ ,150) โ†’ First print return P ( Value of P is 250) Now R= print( 250,โ€#โ€,100) โ†’ Second Print Second Call(S=change(S)) Value of S assigned to P ,Q Remained Unchanged P=100 Q= P=P+Q P=100+ P= Q=P-Q

Q=130- 30

Q=

print(130 ,โ€™#โ€™ ,100) โ†’ Third print return P ( Value of P is 130) S= print( 250 ,โ€#โ€,130) โ†’ Fourth Print Third Call(S=change(R)) R= Value of R is allocated to P=250 ,Q= P=P+Q P=250+ P= Q=P-Q Q=280- 30 Q= print( 280,โ€#โ€,250) โ†’ Fifth Print return P ( Value of P is 280) now S= print( 250 ,โ€#โ€ 280) โ†’ Sixth Print Final Output 250 # 150 250 # 100 130 # 100 250 # 130 280 # 250 250 # 280 10 : def alter(P=15,Q=10): P=PQ Q=P/Q print(P,"#",Q) return Q A= B= A=alter(A,B) print(A,"$",B) B=alter(B) print(A,"$",B) A=alter(A) print(A,"$",B) Solution:* A=100 B= First Call ( A=alter(A,B)) P=100 Q= P=PQ P=100 P= Q=P/Q Q=20000/ Q=100. print(P,โ€#โ€,Q) โ†’ First Print 20000 # 1 00. return Q (Q=200.0) Now return value assigned to A ,Now A=100. print( A,โ€$โ€,B) โ†’ Second Print 100.0 $ 200

Second Call ( B=alter(B)) B= P= 200 Q= 10 P=PQ P=20010= Q=P/Q Q=2000/ Q=200. print(P,โ€#โ€,Q) โ†’ Third Print 2000 # 2 00. return Q (Q=200.0) Now return value assigned to B ,Now B=100. A=100. print( A,โ€$โ€,B) โ†’ Fourth Print 100.0 $ 200. Third Call ( A=alter(A)) A=100.0 ,B=200. Value of A is assigned to P=100.0 and Q= P=PQ P=100.0 P=1000. Q=P/Q Q=1000.0/ Q=100. print(P,โ€#โ€,Q) โ†’ Fifth Print 1000.0 # 1 00. return Q (Q=100.0) Now return value assigned to A ,Now A=100.0 and B=200. print( A,โ€$โ€,B) โ†’ Sixth Print 100.0 $ 200. Final Output 20000 # 100. 100.0 $ 200 2000 # 200. 100.0 $ 200. 1000.0 # 100. 100.0 $ 200. Q 11

def change(P,Q=10): (CBSE Compartment-20)

P=P/Q

Q=P%Q

print(P,"#",Q) return P A= B= A=change(A,B) print(A,"$",B) B=change(B) print(A,"$",B) A=change(A) print(A,"$",B) Solution: A=200 B= First Call(A=change(A,B)) Value of A is assigned to P and value of B is assigned to Q P=200 Q=

P=P/Q

P=200/

P=10.

Q=P%Q

Q=10.0%

Q=10.

print(P,โ€#โ€,Q) โ†’ First Print 10.0 # 10. return P (value of P is 10.0 it will return to the calling function and stored in a variable A,Now A=10.0) print(A,โ€$โ€,B) โ†’ second print 10.0 $ 20 Second Call(B=change(B)) Only one argument is here the value of B is assigned to P i.e 20 and value of Q is 10 P=P/Q P=20/ P=2. Q=P%Q Q=2.0/ Q=2. print(P,โ€#โ€,Q) โ†’ Third Print 2.0 # 2. return P, (value of P is 2 .0 it will return to the calling function and stored in a variable B ,Now B= 2 .0) print(A,โ€$โ€,B) โ†’ Fourth Print 10.0 $ 2. Third Call(A=change(A)) Only one argument is here the value of A is assigned to P i.e 10.0 and value of Q is 10 P=P/Q P=10.0/ P=1. Q=P%Q Q=1.0/ Q=1. print(P,โ€#โ€,Q) โ†’ Fifth Print 1.0 # 1. 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) print(A,โ€$โ€,B) โ†’ Sixth Print 1.0 $ 2. Final Output: 10.0 # 10. 10.0 $ 20 2.0 # 2. 10.0 $ 2. 1.0 # 1. 1.0 $ 2.

Q4.

Def checkvel X=int(input(โ€œEnter numberโ€)) If x%2= Print(โ€œx is evenโ€) Else if x<0: Print x is should be positive Else; Print x is odd Solution: def checkvel(): X=int(input(โ€œEnter numberโ€)) If X%2== 0 print(โ€œx is evenโ€) elif x<0: print(โ€œ x is should be positiveโ€) else: print(โ€œ x is oddโ€) Question Based on Scope of variable: Q1. v=25 #global variable def fun(ch): v=50 #local variable print(v,end=ch) print(v,end="") fun("!") print(v) Solution:* v= print(v,end==โ€โ€) 25 ch=! 50! print(v) 25 Final Output: 2550! 25 Q2. x=25 #global variable def change(): x=10 #local variable x=x+ print(x) print(x) change() Solution:* x= print(x) 25 Function call x= x=10+ x= print(x)

Final output: 25 15 Q3. x= def change(): x= print(x) x= print(x) print(x) change() Solution: print(x) 25 Function call x= print(x) 4 x= print(x) 2 Final output 25 4 2 Q4. c= def add(): global c c=c+ print(c,end="#") add() c= print(c,end="%") Solution: Function call add() c=c+ c=10+ c= print(c,end=โ€#โ€) 12 # c= print(c,end=โ€%โ€) 15 % Final Output: 12#15%

Q5.

p= q= def change_values(): global p q= p=p+q return p change_values() print(p,q) Solution : Final Output: 6 6 Q x= def multiply(): global x x=x print(x,end="@") multiply() x= 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+num result=add(10) print(result) Output: 15