Python Function
Defination and usage of “Functions in python”
- A function is a block of code which only runs when it is called.
- You can pass data, known as parameters, into a function.
- A function can return data as a result.
- def is the keyword in the function
- to call a funtion we write in a paranthesis
Creating a Function
In Python a function is defined using the def keyword.
Example
def foo():
print(5)
Calling a Function
To call a function, use the function name followed by parenthesis().
Example
def foo():
print(5)
foo()
Python provides different ways of passing the arguments during the function call from which we will explore keyword arugements means passing the argument by using the parameter names during the function call.is
Types of arguments
- Keyword argument
- Positional argument
Keyword Arguments
You can also send arguments with the key = value syntax.
This way the order of the arguments does not matter.
Example
def foo(a,b,c):
print(a,b,c)
foo(10,11,12)
Here, we defined a function foo() and assinged some variable to that i.e., a,b,c with some int to that variables 10,11,12 when the code is excuted we expected some output for that
Output : a=10,b=11,c=12
Use of keyword arguments
> Parameter Names are used to pass the argument during the function call.
> Order of parameter Names can be changed to.
Example
When we change the integers to variable from c=10,b=11,a=12 to a=13,b=14,c=15
And then, final output we expect is
def foo(a,b,c):
print(a,b,c)
foo(10,11,12) #output : a=10,b=11,c=12
foo(c=10,b=11,a=12) #output : 12,11,10
Positional Arguments
Positional arguments are the arguments that are passed to a function or method in a specific order. The order in which you pass these arguments matters, because the function or method will use the arguments in the order that they are received.
Arguments(args) vs Keyword Arguments(kwargs)
** (double star/asterisk) and * (star/asterisk) do for parameters in Python, Here, we will also cover args and kwargs examples in Python. We can pass a variable number of arguments to a function using special symbols.
There are two special symbols:
*args and **kwargs in Python
Special Symbols Used for passing arguments in Python:
- *args (Non-Keyword Arguments)
- **kwargs (Keyword Arguments)
- *args are passed in list
- **kwargs are passed in dictionary
Example
def foo(*args):
x=0
for arg in args:
x+=args
return x
foo(*[1,52,62,2,3])
Example
def foo(**kwargs):
for key in kwargs.keys():
print(key)
foo(**{‘a’:1,’b’:2,’c’:3})
Recursion Function
The term recursion can be defined as the process of defining something in terms of itself. In simple words, it is a process in which a function calls itself directly or indirectly.
Advantages of using recursion
- A complicated function can be split down into smaller sub-problems utilizing recursion.
- Sequence creation is simpler through recursion than utilizing any nested iteration.
- Recursive functions render the code look simple and effective.
Disadvantages of using recursion
- A lot of memory and time is taken through recursive calls which makes it expensive for use.
- Recursive functions are challenging to debug.
- The reasoning behind recursion can sometimes be tough to think through.
Example
def foo(num):
print(num)
x = num-1
if x>0:
foo(x)
foo(5)
Lambda Function
- lambda function is a small annoymous function
- we can call then and there it self
- lambda can be written in single expressions it accepts positional arguments
- lambda can take any number of arguments but can only have one expression
- lambda does’nt have any print statement it default returns the value and apply any variable to future cases
- no statement can made in inside lambda function
- it we cant write a lambda func in multiple lines we pass lambda func within paranthesis
- it accepts positional keyword arguments and args and kwargs
Example
(lambda x,y:
x+y)(3,5)
Map Function
- map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.) Syntax :
- syntax : map(function,iterator)
Example
y = map (lambda x : x **2,[2,3,4])
print(y,list(y))
Filter Function
- The filter() function returns an iterator where the items are filtered through a function to test if the item is accepted or not.
- syntax : filter(function,iterator)
Example
b = alnum
a = filter(lambda x : x in b,[‘asdu8763’,’ajshgbca’,’8677646'])
print(a,list(a))
Reduce function
syntax : reduce(function,iterator)
- it will always returns exactly one value
Example
import functools as ft
x=ft.reduce(lambda x,y :x+y,[1,2,3,4,5,6,7,8,9,10])
x