You are on page 1of 9

Experiment No.

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

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming


language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code
is also available under the GNU General Public License (GPL).

Open the Python command window or IDLE shell script to type programs

Syntax:

INDENTATION: Matters a lot in python. We denote blocks of code through indentation

Notes on functions

def function_name(parameters):

"""docstring"""

statement(s)

Example of a function

def greet(name):

"""This function greets to


the person passed in as

parameter"""

print("Hello, " + name + ". Good morning!")

Function Call

>>> greet('Paul')

Hello, Paul. Good morning!

Example 2:

def my_func():

x = 10

print("Value inside function:",x)

x = 20

my_func()

print("Value outside function:",x)

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.

3. Write function to compute gcd, Lcm of two numbers.

4. Write a function unique to find all the unique elements of a list.


PART B

(PART B: TO BE COMPLETED BY STUDENTS)


(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the concerned
lab in charge faculties at the end of the practical in case the there is no Black board access
available)
Roll No. N009 Name: Palak Choudhary
Program: MBA Tech CS Division: E
Semester: V Batch : E1
Date of Experiment: 6/8/19 Date of Submission: 13/8/19
Grade :

B.1 Software Code written by student:


(Paste your Python code completed during the 2 hours of practical in the lab here)

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))

x=int(input("Enter number of elements in a list: "))

l=[]

for i in range(x):

a=int(input("Enter element: "))

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.

x=int(input('Enter no. of elements:'))

l=[]

for i in range(x):
n=input('Enter elements: ')

l.append(n)

print(l)

l=l[::-1]

print(l)

3. Write function to compute gcd, Lcm of two numbers.

a=int(input("Enter first number: "))

b=int(input("Enter second number: "))

def gcd(x,y):

if (x>y):

m=x

else:

m=y

for i in range(1,m):

if (x%i==0) and (y%i==0):

gcd=i

print("GCD: "+str(gcd))

def lcm(x,y):

if (x>y):

m=x

else:
m=y

while(True):

if (m%x==0) and (m%y==0):

lcm=m

break;

m+=1

print("LCM: "+str(lcm))

gcd(a,b)

lcm(a,b)

4. Write a function unique to find all the unique elements of a list.

x=int(input('Enter number of elements of list: '))

l=[]

for i in range(x):

l.append(input('Enter elements: '))

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==[]):

print('No unique elements found!')

else:

print(k)

unique(l,x)

B.2 Input and Output:


(Paste your program input and output in following format. If there is error then paste the
specific error in the output part. In case of error with due permission of the faculty extension
can be given to submit the error free code with output in due course of time. Students will be
graded accordingly.)

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.

You might also like