🐍
Python For Starters
  • Overview
  • Python for starters
  • Content
  • Foreword
  • Introduction To Python
    • Python Installation
    • Variables
    • Python Data Types
    • Python Data Structures
    • Python numbers
    • Python strings
    • Python Boolean
    • Python constants
    • Python Comments
    • Type Conversion
    • Control flow in python
  • Python Functions
    • Function Definition
    • Function Parameter
    • Recursive Functions
    • Using Global Variables in Functions
    • Lambda Functions
  • What are Data Structures
    • Python Lists
    • Python Tuples
    • Python Dictionaries
    • Python Sets
    • Iterable in python
    • Python Map, Filter & Reduce Functions
  • Error handling in Python
  • Loop ... Else Clause, Partial Functions & Type Hints
    • Partial Functions
    • Type Hints
  • Python Modules
  • Python Libraries & Packages
    • Python Packages
  • Directory & IO
    • Python IO Module
  • Pyenv
  • Virtual Environments
    • Pipenv
    • Virtualenv
  • Object Oriented Programming in Python
    • Class
    • Class Methods
    • Python Class Inheritance
    • Python Class Polymorphism
    • Python special methods
  • Appendix
  • Contributing
Powered by GitBook
On this page
  1. Python Functions

Lambda Functions

Working with lambda functions

In Python, a lambda function is a small anonymous function. It can take any number of arguments, but can only have one expression. Lambda functions are useful when you need a simple function for a short period of time. They are not meant to be used as standalone functions and are not as flexible as regular functions.

The syntax for a lambda function is:

lambda arguments: expression

This is an example of a lambda function to multiply two numbers:

add = lambda a,b : a * b
print(add(5,5))

There are several scenarios where lambda functions can be useful in Python:

  • When you need a small anonymous function for a short period of time.

  • When you want to pass a function as an argument to another function (e.g. as a callback).

  • When you want to define a function inline (i.e. within another function).

  • As a way to avoid creating a function with a def statement when the function is only going to be used once.

PreviousUsing Global Variables in FunctionsNextWhat are Data Structures

Last updated 1 year ago