









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










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.
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.
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.
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.
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.
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.
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
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.
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.
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.
Q16.Assertion: A function can return multiple values in Python. Reason: Python functions can return values as a tuple.
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.
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)*
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
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
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
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=
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.
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%
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