1. Home
2. Docs
3. Python 201
4. Lambda Functions

# Lambda Functions

In this lesson you learn about lambda functions in Python,

They are simple, and yet confusing at times.

Let’s try understand them, by exploring some simple examples.

### Anonymous Functions or lambda functions

Anonymous function is a function without a name.

Like in other programming languages, in Python we can create anonymous functions.

Anonymous functions are created using a lambda keyword.

Generally these functions just look like an expression, that’s why there

are also known as lambda expressions

syntax:

```lambda [argument list] : code statement

```

lambda is keyword, argument list is optional, and a ( : ) colon, fallowed by

code statement.

Let’s look at some examples

### Creating a lambda function

Ex 1:

simplest lambda function.

```lambda : 10

output:

<function __main__.>lambda

```

Creates a anonymous function which returns 10.

Note there are no ( ) brackets and return keyword.

lambda functions evaluates the expression

and automatically returns the result.

You can assign lambda function to a variable and call it with ( ).

```lambda : 10

value = lambda : 10

value()

output:

10

```

### lambda function with one argument

Ex 2:

```Multiple x with 10

ten_times = lambda x : x * 10

ten_times(2)

output:

20

```

Here lambda function takes x as argument and multiples it by 10

and return result as 20.

### lambda function with two arguments

Ex 3:

```Multiply x by y

multiply = lambda  x , y : x * y

multiply(3,4)

output:

12

```

this lambda function takes two arguments, x and y, separated by ( , ) comma.

Evaluates the express x * y and returns the product.

### lambda function with conditional evaluation

Ex 4:

get the biggest of x and y

```bigger = lambda  x , y : x if x > y else y

print(bigger(4,5))

print(bigger(4,3))

```

here lambda takes, x and y, evaluates x if x > y else y

it’s ternary expression, its same as

```if x > y:

return x

else:

return y

```

### Using built methods of string

Ex 4:

Return uppercase string.

```to_upper = lambda s : s.upper()

to_upper('just learn python')

output:

'JUST LEARN PYTHON'

```

Ex 5:

Compare two string for their length.

```is_bigger = lambda s1,s2 : len(s1) > len(s2)

is_bigger('just learn python','learn python')

output:

True

```