Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

Funcții lambda


Normally we define a function using the def keyword somewhere in the code and call it whenever we need to use it.

def sum(a,b):
    return a + b

a = 1
b = 2
c = sum(a,b)
print(c)

Acum, în loc să definim funcția undeva și să o apelăm, putem folosi funcțiile lambda din Python, care sunt funcții inline definite în același loc în care le folosim. Astfel nu mai este necesar să declarăm o funcție undeva și să revenim la cod doar pentru o singură utilizare.

Nu trebuie să aibă un nume, așa că sunt numite și funcții anonime. Definim o funcție lambda folosind cuvântul cheie lambda.

your_function_name = lambda inputs : output

Așadar, exemplul de mai sus de sum folosind funcția lambda ar fi,

a = 1
b = 2
sum = lambda x,y : x + y
c = sum(a,b)
print(c)

Aici atribuim funcția lambda variabilei sum, și la introducerea argumentelor, adică a și b, funcționează ca o funcție normală.

Exercise

Scrieți un program folosind funcții lambda pentru a verifica dacă un număr din lista dată este impar. Imprimați "True" dacă numărul este impar sau "False" dacă nu, pentru fiecare element.

l = [2,4,7,3,14,19] for i in l: # your code here l = [2,4,7,3,14,19] for i in l: # your code here my_lambda = lambda x : (x % 2) == 1 print(my_lambda(i)) test_output_contains("False") test_output_contains("False") test_output_contains("True") test_output_contains("True") test_output_contains("False") test_output_contains("True") success_msg("Nice work!")

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy