Functions in Python
Instructor
Prashant Sahu
Manager (DataScience), Analytics Vidhya
Importance of Function in Python
Function: Scenario
Anna wants to send a personalized greeting message to a list of 50 people, ensuring every message is consistent except
for the recipient's name
Simple Approach Efficient Approach
Function: Introduction
Functions in Pythons is a block of statements that returns the specific tasks.
Idea is to put some commonly done tasks together and make a function to reuse code contained in
it over and over again.
Keyword Function name Inputs to the function
def function_name (parameters):
#Python code 1
Body of function containing Python statements
#Python code 2
return output
Keyword Output returned by the function call
User-defined
Built-in Fuctions
Function
Functions Types of
Functions
There are various types of functions
used to perform different operations.
Recursive Function Lambda Function
Built-in Function
Python offers a variety of built-in functions that can be used without any imports.
Input or Output Datatype conversion Mathematical Functions Other Functions
input() bool(), int(), dict(), float() abs(), sum(), max(), min() len()
print() list(), set(), str(), tuple() pow(), round(), divmod() range(), type()
To know more about this follow this link
User-Defined Function
A user-defined function is created by user to perform specific tasks, rather than using the built-in
functions provided by the Python.
To create a function, one need to use the "def" keyword followed by the function name and input
parameters within parentheses.
Lambda Function
Lambda functions in Python are small, anonymous, and
single-use functions that are defined using the "lambda"
keyword instead of "def".
Lambda functions are limited to a single expression, and their results are
returned automatically.
Lambda functions are commonly used in conjuction with higher-order
function like map(), filter(), or sorted()
Recursive Function
A Recursive function calls itself to solve a smaller instance of the same problem.
In Python, recursive functions are typically used when a problem can be defined in terms of itself,
like calculating factorials, or generating Fibonacci sequences.
Thank You