Experiment No.05: Part A
Experiment No.05: Part A
Experiment No.05: Part A
05
PART A
(PART A: TO BE REFFERED BY STUDENTS)
A.1 AIM: - To understand the syntax of function declaration and function call in Python
programming (Introduction)
A.2 Prerequisite
Computer Programming I, and II
A.3 Outcome
After successful completion of this experiment students will be able to
1. Understand the syntax of writing function definition and function call in Python
A.4 Theory
Open the Python command window or IDLE shell script to type programs
Syntax:
Notes on functions
def function_name(parameters):
"""docstring"""
statement(s)
Example of a function
def greet(name):
parameter"""
Function Call
>>> greet('Paul')
Example 2:
def my_func():
x = 10
x = 20
my_func()
Exercises on Functions
1. Find mean, median, mode for the given set of numbers in a list.
Hint: (mean: sum of the data/no of datapoints, median is middle value( use sort(), The syntax of
mode() Function in Python is as follows: statistics.mode(data))
2. Write a function reverse to reverse a list. Without using the reverse function.
1. Find mean, median, mode for the given set of numbers in a list.
Hint: (mean: sum of the data/no of datapoints, median is middle value( use sort(), The
syntax of mode() Function in Python is as follows: statistics.mode(data))
l=[]
for i in range(x):
l.append(a)
def mean(a):
c=0
for j in a:
c=c+j
mean=c/x
print("Mean: "+str(mean))
def median(a):
a.sort()
if (len(a)%2!=0):
b=int((len(a)+1)/2)
median=a[b-1]
print("Median: "+str(median))
elif (len(a)%2==0):
x=int((len(a))/2)
y=int((len(a)+2)/2)
m1=a[x-1]
m2=a[y-1]
m=(m1+m2)/2
print("Median: "+str(m))
def mode(a):
import statistics
print("Mode: "+str(statistics.mode(a)))
mean(l)
median(l)
mode(l)
2. Write a function reverse to reverse a list. Without using the reverse function.
l=[]
for i in range(x):
n=input('Enter elements: ')
l.append(n)
print(l)
l=l[::-1]
print(l)
def gcd(x,y):
if (x>y):
m=x
else:
m=y
for i in range(1,m):
gcd=i
print("GCD: "+str(gcd))
def lcm(x,y):
if (x>y):
m=x
else:
m=y
while(True):
lcm=m
break;
m+=1
print("LCM: "+str(lcm))
gcd(a,b)
lcm(a,b)
l=[]
for i in range(x):
def unique(l,x):
k=[]
for i in range(x):
if l[i] not in l:
print(l[i])
k.append(l[i])
if(k==[]):
else:
print(k)
unique(l,x)
1.
2.
3.
4.
B.3 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed above
and learning/observation noted in section B.1)
Understood the syntax of writing function definition and function call in Python. Also learnt how
to print a list in reverse without any predefined function.