0% found this document useful (0 votes)
37 views12 pages

Worksheet Functions

The document contains the code snippets and output predictions for 35 Python functions. The functions cover topics like defining functions, passing arguments, global and local scopes, recursion, and more. Students are expected to analyze the code and predict the output for each function.

Uploaded by

abelosbert01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views12 pages

Worksheet Functions

The document contains the code snippets and output predictions for 35 Python functions. The functions cover topics like defining functions, passing arguments, global and local scopes, recursion, and more. Students are expected to analyze the code and predict the output for each function.

Uploaded by

abelosbert01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ST. JOHN'S SR. SEC.

SCHOOL
MANDAVELI, CHENNAI – 600 028

CLASS : XII CHAPTER :3- WORKING WITH FUNCTION


SUBJECT : COMPUTER SCIENCE WITH PYTHON
WORKSHEET - 1
PREDICT THE OUTPUT FOR THE FOLLOWING CODE:
1. def Fun1():
print(“Python,let\’sfun with functions’)
Fun1()
2. def add(i):
if(i*3%2==0):
i*=i
else:
i*=4
return i
a=add(10)
print(a)
b=add(5)
print(b)
3. import math
def area(r):
return math.pi*r*r
a=int(area(10))
print(a)
4. def div5(n):
if n%5==0:
return n*5
else:
return n+5
def output(m=5):
for i in range(0,m):
print(div5(i),'@',end="")
print('')
output(7)
output()
output(3)
5. def sum(*n):
total = 0
for i in n:
total+=i
print(‘Sum=’,total)
sum()
sum(5)
sum(10,20,30)
6. def func(b):
global x
print(‘Global x=’, x)
y=x+b
x=7
z=x–b
print(‘Local x = ‘,x)
print(‘y = ‘,y)
print(‘z = ‘,z)
x=5
func(10)
7. def func(x,y=100):
temp= x+y
x+= temp
if(y!=200):
print(temp,x,x)
a=20
b=10
func(b)
8. def get(x,y,z):
x+=y
y-=1
z*=(x-y)
print(x,’#’,y,’#’,z)
def put(z,y,x):
x*=y
y+=1
z*=(x+y)
print(x,’$’,y,’$’,z)
a=10
b=20
c=5
put(a,c,b)
get(b,c,a)
put(a,b,c)
put(a,c,b)
9. a=1 def():
a=10
print(a)
10 def Change(P, Q = 30):
P=P+Q
Q=P-Q
print(P,"#",Q)
return (P)
R = 150
S = 100
R = Change(R,S)
print(R, "#",S)
S = Change(S)
11.def gcd(x,y):
while(y):
x, y = y, x % y
return x
def lcm(x, y):
lcm = (x*y)//gcd(x,y)
return lcm
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("The L.C.M. of", num1,"and", num2,"is", lcm(num1, num2))
print("The G.C.D. of", num1,"and", num2,"is", gcd(num1, num2))
12.def CALLME(n1=1,n2=2):
n1=n1*n2
n2+=2
print(n1,n2)
CALLME()
CALLME(2,1)
CALLME(3)
13.def check(n1=1, n2=2):
n1=n1+n2
n2+=1
print(n1,n2)
check()
check(2,1)
check(3)

14. val=0
def val_change(val):
val=val+1
return val
print(val,val_change(val))
15. def val_change(argone, *vararg):
print(type(vararg))
val_change(1,2,3,4)

16. def val_change(args1=1, arg2=2):


args1= args1+args2
args2= args2+1
print(args1,args2)
val_change(args2=1, args1=2)

17. globala=10
globalb=20
def val_change():
global, globalb
globala=45
globalb=54
val_change()
print(globala,globalb)
18. def name_title(*names):
title=”sir”
for name in names:
name=”sir”+” “+name
print(name)
return names
name_title(‘viv richard’, ‘don bradman’)
19. def sumargs(*args);
sumtotal=0
for arg in args:
sumtotal+=args
return sumtotal
print(sumargs(1,2,3))
print(sumargs(1,2,3,4,5))
20.def f(arg1,arg2,arg3):
return arg1+arg2**arg3
f(2,3,2)
21. def sumargs(*args):
sumtotal=0
for arg in args:
sumtotal+=arg
return sumtotal
print(sumargs(1,2,3))
print(sumargs(1,2,3,4,5))
22. def powerfn(arg1,arg2=2):
power = 1
for I in range(arg2):
power=power *arg1
return power
print powerfn(5)
print powerfn(3,5)
23. def max(arg1,arg2):
if arg1>arg2:
return arg1
elif arg1==arg2:
return ‘Equal’
else:
return arg2
print(max(12, 4))
24. var = 50
def func(var):
print(‘var is’,var)
var = 2
print(“local var is changed to”,var)
func(var)
print(‘var is now’, var)
25. def chk_global():
total+=1
return total
total = 0
print(chk_global())
26.def max(x,y):
if x>y:
print(x, “ x is maximum”)
elif x ==y:
print(x, “x is equal to y”)
else:
print(y, “y is maximum”)
max(3,4)
27. def chk_global():
return total+1
total = 0
print(chk_global())
28. def gettype(posarg, **kwargs):
print(type(kwargs))
gettype(‘arg1’, 1=’a’, 2=’b’)
29. def Hello():
print(“Welcome to python”)
Hello()
30. def makenew(mystr):
newstr=” “
count = 0
for i in mystr:
if count%2==0”
newstr= newstr+str(count)
else:
if i.lower():
newstr= newsr+i.upper()
else:
newstr= newstr+i
count + = 1
print(newstr)
makenew(“No@1”)
31. a=30
def call(x):
global a
if a %2==0:
x+= a
else:
x-=a
return x
x=20
print(call(35),end=’’)
print(call(40), end = “@”)
32. def Display(str):
m= “ “
for i in range(0,len(str)):
if(str[i].isupper());
m= m+str[i].lower()
elif str[i].islower():
m= m+str[i].upper()
else:
if i%2==0:
m=m+str[i-1]
else:
m=m+”#”
print(m)
Display(“[email protected]’)
33. def conver():
n=len(s)
m=” “
for i in range(0,n):
if(s[i]>=’a’ and s[i]<=’m’):
m=m+s[i].upper()
elif(s[i]>=’n’ and s[i]<=’z’):
m=m+s[i-1]
elif (s[i].isupper()):
m=m+s[i].lower()
else:
m = m + ‘#’
print(m)
34. def fun(a):
s= “CS”
m=a
return s, m
a=50
a, b = fun(a)
print(a , b)
35. def fun(scores):
total=0
for number in scores:
if number%10==0:
total+=number
return total
scores = [200, 456, 300, 100, 234]
s= fun(scores)
print(s)
36. def display(frequency, List):
for index in List:
if index in frequency :
frequency[index]+=1
else:
frequency[index]=1
print(frequency)
frequency={}
List=[‘a’,’b’, ‘c’,’a’,’c’]
display(frequency , List)
37. a=7
def printA():
print(“Value of a is”,a)
def foo(value):
global a
a= value
print(“Inside foo, a is” , a)
def bar(value):
a = value
print(“Inside bar, a is”, a)
bar(10)
printA()
foo(20)
printA()
38. def change(t1,t2):
t1=[100,200,300]
t2[0]=8
list1=[10,20,30]
list2=[1,2,3]
change(list1,list2)
print(list1)
print(list2)
39. def change(num1, num2=50):
num1=num1+num2
num2=num1-num2
print(num1, ‘#’,num2)
n1=150
n2=100
change(n1,n2)
change(n2)
change(num2=n1, num1=n2)
40. def revert(num, last =2):
if last%2==0:
last= last+1
else:
last = last-1
for c in range(1,last+1):
num+=c
return num
def main():
A , B = 20 ,4
A = revert( A, B)
print(A, “&”, B)
B-=1
B = revert(B)
print(A, “#”, B)
main()
41. def modify(n):
return n*3+10
def main():
L = [10,15,12,17]
for i in range( 3, -1,-1):
L[i]=modify(L[i])
for i in range(0,4):
print(L[i], end=” “)
main()
42. def big(a,b):
if a>b:
return a+1
else:
return b+2
A= [10, 3, 55, 66,9]
L=len(A)
for i in range(0, L-1):
A[i]= big(A[i], A[i+1])
for num in A:
print(num, end=’’)
43. def dot(a,b):
total = 0
for i in range(len(a)):
total+ = a[i] * b[i]
print(total)
x= [10,20,30]
y = [1,2,3,4]
dot(x,y)
44. def foo( u, v, w=3):
return u* v* w
def bar(x):
y=4
return foo(x,y)
print(foo(4,5,6))
print(bar(10))
45. def withdef(HisNum =30):
for i in range(20,HisNum+1,5):
print(I, end = “”)
print()
def control(MyNum):
MyNum= MyNum + 10
withdef(MyNum)
YourNum=25
Control(YourNum)
withdef()
print(“Number=”, YourNum)
46. x=100
def f():
global x
x=9
print(“x=”, x)
print(“x=”, x)
f()
print(“x=”,x)
47. x=20
def f():
x=50
print(“x=”,x)
print(“x=”, x)
f()
print(“x=”,x)
48. def foo():
print(“Three”)
def bar():
print(“Hello”);
foo()
print(“Two”)
foo()
bar()
49. V = 25
def Fun(ch):
V = 50
print(V, end=ch)
print(V, end=”*”)
Fun(“!”)
print(V)
50. V= 50
def change(N):
global V
V, N = N, V
print(V, N , sep=”#”, end=”@”)
Change(20)
print(V)
FIND OUT THE ERRORS IN THE FOLLOWING PROGRAMS AND
REWRITE THE CORRECT CODE:
51.def in(a,b):
a=a + b
print(a.b)
a=*b
print(a**b)
In(8,2)
52. void get(x=10,y):
x=x+y
print(x,n,y)
53. def res():
eng=56
mat=40
sci = 60
if eng<=35 ll mat<=35 ll
sci=35
print(“Not Qualified”)
Else:
print(“qualified”)
54. A=5, b=10
def swap(x,y):
x=a+b
y= x-y
x= x- y
swap(a)
swap(15,34)
swap(b)
swap(a,b)
55. def cal_dis(qty,rate=50,dis_rate): #discount rate = 5%
bil_amt= (qty*rate)*dis-rate
print(bil_amt)
caldis(10)
cal_dis(10,50,0.05)
56. defSum(C)
S=0
for I in range(1,C+1):
S+=1
RETURN S
Print(Sum[2])
Print(Sum[5])
57. def SI(p,t=2,r):
return(p*r*t)/100

You might also like