Need Assistance?

support@tutorialshub.in

Functions and Modules

Default and keyword arguments

ADVERTISEMENT
Python Free Lesson
5 min read
ADVERTISEMENT

Functions are first-class objects

Python functions can be assigned to variables, stored in collections, passed as arguments and returned from other functions. This enables callback and higher-order programming patterns.

Where lambda fits

A lambda is a small anonymous function containing a single expression. It is convenient for short callbacks such as the key function passed to sorted(). For reusable or complicated logic, a named function is clearer.

Separate algorithm from policy

Functions such as sorted() can perform the algorithm while your callback defines the business-specific comparison or extraction rule. This is a powerful and reusable design pattern.

Practice: sort employee records by department and name, first with a lambda and then with a named key function.

ADVERTISEMENT