Python Pioneers: A Beginner's Guide πŸ‡ͺπŸ‡Ή
8.66K subscribers
346 photos
40 videos
24 files
64 links
Welcome to Python Pioneers: Empowering Ethiopian students to embark on their coding journey.

πŸ“’ Channel: @python_pioneers
πŸ‘₯ Group: @python_pioneers_chat
Download Telegram
Hey everyone! Sorry it's been a while since our last meeting. Today, we're diving into an exciting lesson about lambda functions in Python.

Looking forward to seeing you all!

#LambdaFunctions

@python_pioneers
πŸ”₯5πŸ‘1
Lambda Functions in Python

Lambda functions α‰ αˆŒαˆ‹ αˆ΅αˆ›α‰Έα‹ anonymous functions α‰  Python Programming language α‹αˆ΅αŒ₯ at a runtime አጭር αŠ₯αŠ“ unnamed functions αŠ₯αŠ•α‹΅αŠ•αˆαŒ₯ር α‹«αŒα‹™αŠ“αˆα’ αˆŒαˆ‹ ቦታ αˆ˜αŒ α‰€αˆ α‹«αˆ›αŠ•αˆαˆαŒˆα‹αŠ• function α‰ αŠ αŠ•α‹΅ ቦታ α‰₯ቻ αŠ₯αŠ•α‹΅αŠ•αˆαŒ₯ር α‹­αˆ¨α‹±αŠ“αˆα’

What is a Lambda Function?

Lambda function α‹¨αˆαˆˆαŒαŠα‹αŠ• α‹«αˆ…αˆ argument α‹­α‹ˆαˆ΅α‹³αˆ ነገር αŒαŠ• αŠ αŠ•α‹΅ expression α‰₯ቻ αŠα‹ α‹¨αˆšα‰€α‰ αˆˆα‹α’ Expression ኑ evaluated αŠ¨αˆ†αŠ α‰ α‹ƒαˆ‹ value return α‹­α‹°αˆ¨αŒ‹αˆα’

The syntax for a lambda function is:

lambda arguments: expression

የlambda functions α‰αˆα α‰£αˆ…αˆͺያቡ

1. Anonymous: Lambda functions ሡም α‹¨αˆ‹α‰Έα‹αˆα£ that means they're anonymous.

2. Single Expression: αŠ αŠ•α‹΅ expression α‰₯ቻ αŠα‹ α‹¨αˆšα‹­α‹™α‰΅α£ α‹­αˆ… α‹°αŒαˆž ከregular functions less powerful α‹«α‹°αˆ­αŒ‹α‰Έα‹‹αˆα’

3. Can Take Multiple Arguments: α‰₯α‹™ argument α‹¨αˆ˜α‰€α‰ αˆ αŠ α‰…αˆ αŠ αˆ‹α‰Έα‹α’

Lambda function ከregular function α‰ syntax αŠ₯αŠ•α‹΄α‰΅ αŠ₯αŠ•α‹°αˆšαˆˆα‹­ ከሡር α‰£αˆˆα‹ ምሳሌ αˆ˜αˆ¨α‹³α‰΅ α‹­α‰»αˆ‹αˆα’

ምሳሌ : Basic Lambda Function

# Regular function
def add(x, y):
return x + y

# Lambda function
add_lambda = lambda x, y: x + y

print(add(2, 3)) # Output: 5
print(add_lambda(2, 3)) # Output: 5

to be contd...

#LambdaFunctions
#Functions
#Python

@python_pioneers
Lambda functions
Another cool expressions or patterns in python are lambda functions a.k.a anonymous functions. We use them to define functions inline (in one line).
# Syntax
lambda arguments: expresion

The subtle thing we need to understand about anonymous funtions/lambda functions is that they don't have a name unless we explicitly assign them to a variable. The return value of lambda function is the evaluated expression.

The concept actually exists in many programming languages as anonymous functions or function expression, even-though the syntax could be different.
# Examples

# This is valid - With no name
lambda x: x

# With a name
times2 = lambda x: x * 2

# With two argumetns
prefix = lambda pre, post: pre + post;
postfix = lambda post, pre: prefix(pre, post)

print(times2(10))
print(prefix("main", ".py"))
print(postfix(".py", "main"))

# ----------Output----------
20
main.py
main.py


#LambdaFunctions #PythonProgramming #PythonTips #GettingToKnowPython
❀1