Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Practical7 Python Programming 1tsdkaMEsU

1

Uploaded by

sagargurav1812
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Practical7 Python Programming 1tsdkaMEsU

1

Uploaded by

sagargurav1812
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering/ School of Technology


Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

PRACTICAL 7
Part A (To be referred by students)

Built-in functions and User Defined Functions


SAVE THE FILE AND UPLOAD AS (RollNo_Name_Exp6)
Problem Statement:
Solve the following computational problems by recognizing python data structure and
apply them to implement following programs

User defined functions


1. Implement program using user defined function to check year is leap or not. A year is
a leap if it is divisible by 4 and centennial years (years divisible by 100) are leap years
only when they are divisible by 400.
2. Define a function ExtractEven(List) to return all even number from a list. Example
l1[1,2,3,4,5,6] method should return [2,4,6].
3. Write a function that returns add, sub, mul and division of two numbers.
4. Define a user defined function to find area of circle or triangle. If one parameter is
passed the given function finds the area of circle, if two parameters are passed the
same function finds the area of triangle. You have to defined only one parameterized
function which accepts either one parameters or two parameters to find area.
5. Write a program to find largest number using variable length argument. (if two
parameters are passed it will find largest of two, if three passed it will find largest of 3
and so on …)
6. Implement a program to demonstrate **kwargs
7. Write a program to print Fibonacci series upto n using recursion

Built-in Functions
8. Write map function to return a list of numbers which are square of first 10 Fibonacci
numbers.
9. Write a lambda function for adding 26 to its argument, also create a lambda function
that multiplies argument x with argument y and print the result.
10. A twitter database is given in which we have username and tweets.
users = [ {"username": 'seema', "tweets": ["i love chocolate", "i am a student"]},
{"username": 'arush', "tweets": []}, {"username": 'kumal', "tweets": ["India",
"Python"]}, {"username": 'sam', "tweets": []}, {"username": 'lokesh', "tweets":
["i am good"]}, ]
i) Filter out Users which don’t have any tweets/Inactive Users
ii) Filter inactive users with just username in uppercase.
iii) Return a new list with the string “your name is” + name, but only if length of
name is bigger than 4 using map , filter and Lambda

1|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

names=['Rohan','Rhea','bob','to']
Output:['your name is Rohan', 'your name is Rhea']

Topic covered: User Defined Functions and Built in functions

Learning Objective: Learner would be able to


1. Learn how to define, and invoke user defined functions
2. Apply user defied/ built in functions to solve problems
3. Work on recursion.

Theory:

• Functions are sub program & in Python, interpreter creates an object of function
internally.
• Functions are used to perform a specific task
• If there are several tasks, one can write multiple functions
• When we define function inside a class, it is called as method.
• ‘def’ keyword is used to define functions
• Syntax of function definition:
def functionname(parameter list):
#function body
• A functions can not executes its own, it needs to call/invocation when required.
• Function is called using its name and passing values if any.
• Syntax:
functionname(value/values)

Example:-

• It is also possible to return values back to the calling place from function.
• Return is used to send value back to calling place.

• Advantages of Functions:-
• Reusability

2|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

• Easy to maintain and write


• Easy to debug
• Functions provides modularity(part of a program) for programming & work
distribution is possible

Retuning Values:-
• In Python’s function, we can send one or multiple values back to calling place
• If return is not used in function, it will send None back to the calling place
• Return can send one or multiple values back to the calling place
• Example.

• Returning None
def fun():
print(1)
a=fun()
print(a)

• Returning only one Value


def fun():
return 10
a=fun()
print(a)

• Returning Multiple Values


def fun():
return 1,2,3,4
a=fun() #a,b,c,d=1,2,3,4
print(a)
• Multiple Values are return in form of a tuple…

Pass by Objects
• There are various function calling mechanisms like
• Call/pass by Value
• Call/pass by Address
• Call/pass by Reference
• But, Python programming supports call/pass by object.
• Because, everything is considered as object in Pyhon.

3|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

Arguments:-
• Formal Arguments/Parameters
• Actual Arguments/Parameters
• Following Types of Arguments are supported by Functions in Python.
• Positional Arguments
• Keyword/named Arguments
• Default Arguments
• Variable Length Arguments
Recursion:-
• A Function having call to itself.
• Example:
def fun():
fun()
fun()

Variable Length Argument(varargs):


• You can specify arbitrary number of arguments(varargs).
• Variable length argument
• function(farg, *args)
• Args is implicit tuple
• Keyword variable length argument
• function(**kwargs)
• Kwargs is implicit dictionary
• Example:
def fun(**kwargs):
print(kwargs)
fun(a=2,b='3',c='4')
4|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

Built-in Functions:-

5|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

6|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

PRACTICAL 7
Part B (to be completed by students)

Built-in Functions & User Defined Function


1. All the students are required to perform the given tasks in Jupyter Notebook
2. Create a new notebook for each experiment. The filename should be
RollNo_Name_Exp7)
3. In the first cell, the student must write his/her Name, roll no and class in the form of
comments
4. Every program should be written in separate cells and in the given sequence
5. After completing the experiment, download the notebook in pdf format. The filename
should be RollNo_Name_Exp7.pdf).
Note: - To download as a PDF, go to File and choose Print Preview. This will open a
new tab. Now, press Ctrl + P and opt to save as PDF.
OR
Download notebook as HTML and open downloaded html(webpage), press CTRL+P
and opt to save as PDF.
6. Upload the pdf on the web portal

7|Page

You might also like