0 ratings0% found this document useful (0 votes) 133 views17 pagesPython Functions 1690184728
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Function docstrings are placed in the immediate line after the function header and are placed in between,
quotation marks.
def change(x):
"*check call by value”
(7)
print(x)
print(change._doc_)
2(5,2,3,1]
change(a)
print(a)
OUTPUT
check call by value
a)
[5, 2, 3, 1]
OUTPUT: FUNCTIONS
1. Give the output 2
(i) print(*hi") 1
def abc(): 3. def sum(a,b):
print(“hell print(“sum is “,atb)
int(“bye” x5
a.
Gi) def abe: een
| Ans. Output:
print(“hi”) :
‘bye sum is 15
a ct) sum is 60
i) print(“hi") 4, Give the output of the following program
print(“bye”) def check(a):
def abe(): for i in range(len(a)):
print(“hello”) afi}=afi}+5
abe() return a
Ans. OUTPUT b=[1,2,3,4]
(i),Gi),Gii) heck(b)
hi print(c)
bye Ans, OUTPUT
hello (6. 7, 8, 9]
2, def cal(): [2] [SP 16] | 5. Give the output
def sum(*a):
s
for i in a:
s=sti
print(s)
sum(2,3)
sum(2,6,3)
xi sum(1,2,3,4)
print (x*#2) Ans. OUTPUT
cal() 5
‘Ans. 49 Wl
25 10
12OUTPUT: DEFAULT PARAMETER
if w%2
ewurn ut 10;
else:
return u+2;
def pattern(M,B=2)
for CNT in range(0,B):
print(cale(CNT),M,end:
print)
10 #3 #12 HS
10@3 @12 @
. def Execute(M
if M%3=
retum M*3
else:
retum M+10;
dof Output(B=2):
for T in range (0,B):
print(Execute(T),"*",end:
print()
Output(4)
Output)
Output(3)
or *i2*9*
ome
ome2*
{, Find and write the output of the following Python
code : 3 [comptt 19]
def Alter(P=15,Q=10):
ae)
Q-PIQ
print (P,
retum Q
A=100
Be200
AAlter(A,B)
print (A,"S".B)
BeAlter(B)
print (A,°S".B)
Av Alter(A)
print (A,"$"\B)
20000 # 100.0
1000 $ 200
ns.
Q)
Ans.
10.
Ans.
ML,
2000 # 200.0
100.0 $ 200.0
1000.0 # 100.0
100.0 $ 200.0
Find and write the output of the following python
program code:
def Revert(Num,Last=2):
if Last%42:
Last+
for C in range(1,Last+1):
Num+=C
print(Num)
AB=204
Revert(A,B)
B-B-1
Revert(B)
35
9
Find and write the output of the following python
code: [3] [2019]
def Convert(X=45,Y=30):
B=Convert(B)
print (A,"&".B)
A=Convert(A)
print (A,"&".B)
400 & 250
400 & 150
180 & 150
400 & 180
430 & 400
430 & 180
Find and write the output of the following python
code: (3} [2019]
def Changer(P,Q=10):
P=PIQ
Q=P%Q
print (P,"#",Q)
retum P
3t
12, Find and write the output of
fe
A=200 code:
B-20 | det Change(P .Q=30) >
AeChanger A.B) | PoP
print (ASB) | QP-d
BeChangerB) priate P"=.Q)
print (ASB) | return (P)
eth s
AeChangertA) | iss ie
an AED) So ns)
Tan $30 print(R."E".S)
; 2 S-Change(S)
ere | ans. 250 # 150
Ua | 350 # 100
ise | 130 # 100
10 $20
OUTPUT: RANDOM
13, What's the difference between randrange and randint?
Ams. randint eae
| randint(ny) will return a valor >= a and <= y. Wt will | rarangey.y) will return a vahie x and ="0" and Stel [I}<="
Str2-St2+ (Str [I-1))
else:
St2=Str+"*"
IFi+1
print (Str2)
X*M2M201
Give the output
def makenew(mystr):
if count’42!=0:
newstr= newstr+str(count)
.islower()
newstr=newstr+i.upper()
else:
newstrnewstr#i
count+=1
newstr = newstr+mystr[:1]
print (“The new string is :”,newstr)
makenew(“sTUdeNT”)
‘The new string is: S1U3ESTs
Find and write the output of the following python
code: (21 (2019
TextI="AISSCE 2018”
Text2="”
0
while I="0" and Text! [I]}<=
Val = int(Text![I])
Val = Val +1
Text2=Text2 + str(Val)
elif Textt(I]>="A" and Text! (I) <=
Text2=Text2 + (Textl[1+1])
else:
Text2=Text2 +
Ti+
print (Text2)
ISSCE *3129
Find and write the output of the following python
code: [2019]
Msg!="WeL.cOME"
Msg2="GUeSTS"
Msg3="
for L in range(0,len(Msg2)+1):
if Msgi{IJ>="A" and MsgI[I]}<=
Msg3=Msg3+Msg1 (1)
elif Msgi{I]>"N’ and Msgi{t]<—"2"
Msg3=Msg3+Msg2{I]
else:
‘Msg3=Msg3+"*"
print (Msg3)
G*L*TME
Give the output
Text=”Mind@Work!”
Ans,
Me
Ans,
35.
c=0
for i in Text:
if not i.isalpha():
“poe
elif not iisupper():
T=T+(Text{e+1])
else
T=T+iupper()
c-etl
print(T)
Mnd@*Wrk!*
Give the output
Text="Mind@Work!”
Ans,
36,
for i in Text:
if not i.isalpha():
T=T"
clif not iisupper():
T=T+(Text(c+1))
else:
val=ord(i)
19val=val+1
T=T+chr(val)
cet!
print(T),
‘Ans. Nnd@*Xtk!*
37. Mystring="What@OUTPUT!”
str="
for i in range(len(Mystring)
if not Mystringfi).isalpha():
str=str-"*"
Mystring[i).isupper():
val=ord(Mystring[i]) ¢ 7
val-valt] ¢&
str=str+chr(val)
else:
str=str+Mystring{i+1]
print(str)
Ans. Xat@*PVUQVU*
eli
38. Give the output of the following program segment:
NAME = “IntRAneT”
Ne
for x in range(len(NAME)):
if NAME[x].islower():
N=N+NAME|x].upper()
lif NAME[x} isupper(:
if x%2=0:
N=N+NAME{x] lower()
else:
N+NAME[x - 1]
print(N)
Ans. iNTtaNEe
39. Find the output of the following program :
def Changelt(Text,C):
eet
for K in range(len(Text)):
if Text[K]>='F? and Text[K]="M” and S{I}
WS=WSt"@”;
else:
WS=WS+S{I-1:1]
print(WS)
2Word="CRACKAJACK"; ‘Ans. AHMRNOOISU
0 ;
MIXITNOW(Word); AH@@OI@
Ans. RCCAAKAJKC 50. Find the output of the following Progra
@RCCAAKAIK def Big(A,B): m
i i : if (A>B):
49. Find the output of the following program :
det JumbleUp(T): retum chr(ord(A)+1)
Cla else:
=len(T) retum chr(ord(B)+2)
for C in range(0,L,2): WX="Exam”
CT=CT#T[C+] wer
CT=CT#T(C] L=len(WX)
TCT
for i in range(0,L-1,1):
ct=" W=W+Big(WX(i), WX[i+1])
print(T) W=W+WX{(L-1]
for C in range(|,len(T),
print(W)
i“U>=TICh="M": #iE TICP="M" and | ang. aaa
2 chore 51. Give the output
else; a="Hello”
CT=CT+T[C-1:C+1] Lelist(a)
int(CT) ‘join(reversed(L))
St="HARMONIOUS” print(a)
JumbleUp(Str) Ans. olleH
OUTPUT: GLOBAL VARIABLE
52. What do you understand by local and global scope | §4. a=S
of variables? How can you access a global variable def fang)
inside the function, if function has @ variable with a=10
same name, ISP 2019-20] print(a)
Ans. A global variable is a variable that is accessible fund)
globally. A local variable is one that is only accessible rina)
fo the current scope, such as temporary variables E
used in a single function definition, oe
A variable declared outside of the function or in :
global scope is known as global variable, Ss
This means, global variable can be accessed inside def fund):
or outside of the function where as local variable can global a
be used only inside of the function. We can access a=10
by declaring variable as global A. print(a)
Note: In case you have a local variable with the fund)
same name, use the globals() function to access I
slobal variable. brint(a)
slobals()[‘your_global_var’] Ans. 10
for ex: 10
y=globals([‘a" e
Give the ca : ae
= def fara
=
on, print(a)
fun() fun(a)
rings) print(a)
‘Ans. 5 Ans. 10
5 5
22
~~prnt(inside"a)
print("y="y)
a)
print(“outside”,a)
‘Ans, inside 10
y=10
outside 10
58, a-5
def fun():
a=10
yra
print(“inside”,a)
print(“y=",y)
fun()
print(“outside”,a)
‘Ans. inside 10
yrl0
outside 5
59, Find and write the output of the following python
code: ISP 2019-20
a=10
def call:
global a
a5
b=20
print(a)
call()
Ans. 15
60, a5
def fund):
a
y-globals()[‘a")
print(“inside”,a)
print(“y=",y)
fun()
print(“outside”,a)
‘Ans. inside 10
Ans.
02.
Ans.
Ans.
64,
Ans.
fune(g.h)
print(g.h)
fune(g)
Print(g.h)
3:10 50
710
5250
710
e-10
def fune(x,
x
zextytg
return z
g=func(g,15)
print(g)
g=fune(g)
print(g)
45
-650
a3
def demo(x,y):
global a
amxty
zaty
yinx
print(x,y.2)
bes
demo(a,b)
print(a,b)
demo(a,b)
3813
85
813 18
def pa(a,b,c):
x4
ctx
c*=globals()['x"]
bise
retum a,b,c
yl
x2
28
X,Y.Z=Palyx,2)
(x,y,2)
Yo2X=PA(ZY,X)
print(x,y,2)
12018
518 25
23FUNCTIONS/METHODS/MODULES NAME
Math Module
Description
Fanetion Retums the smallest integer greater than or equal to x.
Soe Retums the absolute value of x :
factorialie) Retums the factorial of x :
Floor(x) Reruns the largest integer less than or equal to x
inod(x, y) Returns the remainder when x is divided by y a
isnan(x) Retums True if x is a NaN
runc(x) Retums the truncated integer value of x
rexptx) Retums ¢**x
ToglO(x) Returns the base-10 logarithm of x
pow(x, y) Retums x raised to the power y
‘sqri(x) Retums the square root of x
cos(x) Retums the cosine of x
sin(x) Retums the sine of x
tan(x) Retums the tangent of x
|degrecs(x) Converts angle x from radians to degrees
pi Mathematical constant, the ratio of circumference of a circle to Ws diameur Gi Dy
je mathematical constant e (2.71828.
Random module Csv module Pickle module
fandint() | random() [randrange() [uniform | [dump() _]load) Dictreadwe() | DictWriten) Tsaayh
Matplotlib.pyplot Statistics module
faxes() bar) bath) plot) [pied mean() median Tod
show () title() xlabel() [ylabel() |mean()
median Today
65. Name the Python Library modules which need to be imported to invoke the following fun
(Heeil() (ii) randint() “ty
‘Ans (i) math (ji) random
66. sere te following Python functions and write the names) ofthe modules o which they belong ilies
(b) findall) Hy
Ans. (a) random (b) re
67. Kame the Python Library modules which need to be imported to invoke the following functions “
Gi) randintQ) 11) [0
Ans. (i) math (ii) random
SS. Hen and writ the name of the module to which the following functions belong: (i) ei) (i) finda (m
Ans. (i) ceil()- math module (ii) findall( ) ~ re module
© Mame the Python Library modules which need to be imported to invoke the following functions (i)
ii) pow() i
‘Ans. (i) pickle (ii) math a
70. Name the Python Library modules which need to be imported to invoke the following functions ()
Gi) dumpg hii [0
Ans. (i) math (ii) pickle
71. Name the Python Library modules which need to be imported to invoke the following functions: ()
(ii) search() wt
Ans, (i) math (ii) re
n. cirod required to (i) check if a string contains only uppercase |
: of the list, oe
ns,
273, Name the function/method required to (i) check if a string contains only alphabets (ii) give the total length of
the list. [1] (OD 15}
‘Ans. isalpha()
len()
74, Name the Python Library modules which need to be imported to invoke the following functions: (i) open()
(i) factorial() [1] [compet 2019]
‘Ans. (i) 08 (ii) math
75, Name the Python Library modules which need to be imported to invoke the following functions (i) search()
ona [1] [2019]
‘Ans. (i) re (ii) datetime
76, Name the Python Library modules which need to be imported to invoke the following functions (i) sqrt()
Gi) start () Osa
‘Ans. (i) math (i) re
ERRORS
1, Rewrite the following Python program after removing all the syntactical errors (if any),underlining each
correction.: 2] [SP 16]
def checkval:
x= input(“Enter a number")
ifx%2= 0:
print x,"is even”
else if x<0 :
print x,"should be positive”
else ;
print x,"is odd”
‘Ans. def checkval():
x = inl(input(“Enter a number”)
ifx%2==0:
print (x,7is even”)
lif x<0
print (x,"should be positive”)
else =
print (x,"is odd")
2. Rewrite the following code in python after removing all syntax error(s). Underline each correction done in the
code. (2) [D 15]
def Tot(Number) #Method to find Total
Sum=0
for C in Range (1, Numbert1):
Sum+=C
RETURN Sum
print Tot{3} Function Calls
print Tot{6}
‘Ans, def Tot(Number):
Sum=0
for C in range (1, Numbert1):
Sum+=C
retum Sum
print (Tot(3))
print (Tot(6)
3. Rewrite the following code in python after removing all syntax error(s). Underline each correction done in the
code. [2] [OD 15]
ef Sum(Count) #Method to find sum
Fos-0
for I in Range(1,Count+1):
Set
RETURN S
print Sum[2] #Function Call
print Sum{5]
Ans. def Sum(Count): #Method to find sum
S=0
for I in range(1,Count+1):
StI
return S
print (Sum(2)) #Function Call
print (Sum(5))
4. Rewrite the following code after removing the syntactical errors (if any). Underline each co;
def chksum:
int(input(“Enter a number")
if (%%2 = 0)
for i range(2*x):
print (i)
ection fy
else
print (“#")
‘Ans, def chksum():
int(input(“Enter a number”)
if x2
for i in range(2*x)
print (i)
else:
print (“#")
5. Observe the following Python code very carefully and rewrite it after removing all syntactical errors .
correction underlined,
DEF execmain():
iput(“Enter a number”)
Iflabs(x)=x):
Print”you entered a positive number"
else:
x=*-1
print “Number made positive:"x
execmain()
Ans. def execmain():
(input("Enter a number")
abs(x)= =x:
print(“you entered a positive number")
else:
a
print (“Number made positive:”,x)
execmain()
6. Rewrite the following code in Python after removing all syntax error(s). Underline each correction don
code. 12] {con
for 1 in range(O,len(String)-1)
a.Ans.
if Stringl1]=>"M":
print String(t},"*"
Else:
print Stringft-1]
‘String="HELLO”
for | in range(0,len(String)-1):
if String(l]>="M'
print (String(t},"*”)
else:
print (String{l-1])
. Rewrite the following code in python after removing all syntax error(s). Underline each correction done in the
code. ID 16)
for Name in [Amar, Shveta, Parag)
Ans.
print(Name)
8, Rewrite the following code in python after removing all syntax error(s). Underline each correction done in the
code. [2] [OD 16]
for Name in [Ramesh,Suraj,Priya]
IF Name[0]='S":
print(Name)
Ans. for Name in [“Ramesh’,“Suraj?,“Priya”]:
if Name
print(Name)
DEFINITION FUNCTION(DICTIONARY)
1, Write a user defined function findname(name) where name is an argument in Python to delete phone number from
a dictionary phonebook on the basis of the name where name is the key. {2] [SP 16)
Ans. def findname(name):
if phonebook.has_key():
del phonebook(name]
els
ns,
print”Name not found”
print “Phonebook Information”
print “Name”,'\v,”Phone number”
for i in phonebook.keys():
print i, phonebook{i]
DEFINITION FUNCTION
|. Write a python method/function REVERSAR (Number) to find a new number Reverse from Number with each
of the digits of Number in reversed order and display the content of Reverse on screen. [eomptt 17]
For Example:
If the value of Number is 3451
The method/function should be displayed as 1543, 12) 12019)
def REVERSAR(Number):
S-0
while Number>0;
S=S*10+Number%10
Number=Number//10
print(S)
aOR
REVERSAR(Number):
Number=str(Number)
print(Number{:-1])
2. Write a method in python to find and dis
method.
Ans. def composite_numbers(N):
for I in range(2, N):
M=1//2
for J in range(2, M+1):
if 1% J
print(1)
break
3. Write a method in python to find and display the prime numbers between 2 to N. Pass N as Bn
method. mh
Ans. def prime(N):
for a in range(2,N);
for i in range(2,a):
if a%i =o:
break
play the composite numbers between 2 10 N. Pass N as 4
by
else:
print (a)
| 4. Write a Python method/funetion Count
ervals of Step
For example :
If the values of Start as 14, End as 35 and Step as 6
The method should be displayed as
14
20
26
32
‘Ans, def Count(Start,End,Step): Z
for i in range(Start,End,Step):
print(i)
5: Mite a python method/function Count3and7(N) to find and display the count of all those numbers wks
between 1 and N, which are either divisible by 3 or by 7.
For Example:
If the value of N is 15 :
The sum should be displayed as 7
(as 3,6,7,9,12,14,15 in between 1 t0 15 are either divisible by 3 or 7)
Ans, def Count3and7(N):
0
for i in range(1,N+1):
if i%3—=0 or i%67==0;
cot!
print (c)
Count3and7(15)
(Start, EndStep) to display natural numbers from Star to. Eng
[2] [compe
pm
as,
Soe
28
You might also like
E.G. - If Input Is (12, 54, 49, 86, 23, 36) Then Modified List Is (32, 18, 16, 106, 43, 12)
E.G. - If Input Is (12, 54, 49, 86, 23, 36) Then Modified List Is (32, 18, 16, 106, 43, 12)
6 pages