2. Python Functions are First-Class
• Python functions are first-class objects, you can assign them to
variables
def say_hi(name):
return “Hi “+name
a = say_hi(“adnan”)
print(a)
>> Hi adnan
3. Functions are objects
• Like data types functions are also objects.
• You can assign them to variables.
greet = say_hi
print(greet(’adnan’))
>> Hi adnan
4. Functions are objects(Contd..)
• Since functions are objects, you can also delete them
del say_hi
>> name ‘say_hi’ is defined
• Calling greet will still work.
5. Storing functions in list and iteration
• Since functions are objects, you can store them in data structures like
list, dict etc.
fns =[greet,str.lower, str.upper]
6. Nested Functions
• You can also make your functions nested. Hence provide a
good way to encapsulate logic. They are also called Closures.
def greet(name):
def say_hi(name):
return ‘Hi ‘+name
return say_hi(name)
7. Lambda Functions
• Python provide you to declare anonymous functions.
Anonymous functions are not bound to an identifier, the function
name. They are usually good to avoid name collisions.
add = lambda n1,n2: n1+n2
print(add(3,5))