0% found this document useful (0 votes)
3 views1 page

Python Assignment 01

The document outlines scenarios where Python programmers should prefer lambda expressions, such as for small logic pieces, sorting or filtering, and when using functions like map, filter, or reduce. It also highlights when to use standard functions, including cases of complex logic, the need for reuse, and the desire for documentation. Overall, it provides guidance on choosing between lambda expressions and standard functions based on specific programming needs.

Uploaded by

bc250425283isa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Python Assignment 01

The document outlines scenarios where Python programmers should prefer lambda expressions, such as for small logic pieces, sorting or filtering, and when using functions like map, filter, or reduce. It also highlights when to use standard functions, including cases of complex logic, the need for reuse, and the desire for documentation. Overall, it provides guidance on choosing between lambda expressions and standard functions based on specific programming needs.

Uploaded by

bc250425283isa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PYTHON ASSIGNMENT

Spacific cases or condition in which a Python programmer should prefer lambda expressions over (standard) functions

 when there is need of a tiny piece of logic, lambda saves from writing a whole fuction.e.g check_even = lambda x:
x%2 = = 0
 When there is need to sort or filter someting based on a simple rule.e.g

nanes = ["sarah" , "ali" , "badar"]

sorted = sorted (names , key = lambda x: len(x))

 When working with tools like map, filter, reduce : these functions expect another function as input. Lambda lets us
plug in the logic right there.

number = [1,2,4,5,]

squares = list ( map (lambda x:x**2, number))

 To get a quick response function without clutter.

button.on_click ( lambda e: print ("clicked!"))

When to use normal functions instead

 If the logic is long or complex (loops , multiple steps).


 If there is need to reuse the function in different places.
 If we want to document the function with a name and explanation.

You might also like