Python Functions
Functions are one of the most powerful feature in Python. They helps to organize code, avoid repetition and make program easier to understand.
def my_func():
print("Hello")
my_func()We can write function with parameters or returning value. There is a difference between these two types. If we use parameters with print statement it only display the value but when we use return value it sends the value back to the caller so it can be stored or reused.
Arbitrary Arguments
Sometimes we don’t know how many values user will provide. Then * collects multiple arguments and Python stores them as a tuple and ** converts it to a dictionary.
def numbers(*values):
print(values) OUTPUT - (10, 20, 30, 40)
print(tupe(values)) <class 'tuple'>
numbers(10,20,30,40)
def func(**p_x):
print(p_x) OUTPUT - {"x" : 10, "y" : 20, "z" : 30}
print(p_x,keys()) dict_keys(['x', 'y', 'z'])
func(x=10,y=20,z=30)Lambda Functions
These like nameless functions and not actually store it does only perform.
map() — Applies a function to every element of an iterable.
Get Kavisha Nethmini’s stories in your inbox
Join Medium for free to get updates from this writer.
filter() — Each element is passed into the function individually.
reduce() — Combines all elements into a single value.





