+ 2
Expression vs Statement in lambdas
I never understood the quiz question "lambdas can have no statement" . What else do they have? I guess expressions but whats the difference and why do we have this constraint?
13 Respostas
+ 7
Lambdas can have no statements
Lambdas cannot have if and else statements or loops (there a lot more statements!)
Lambdas are just like
def name(arguments):
return """body of lambda"""
But you can have ternary operators and list comprehension and warlus operators.
ternary operator:
value_if_true if Boolean else value_if_false
List comprehension :
[i for i in range(3)] # [0, 1, 2]
Warlus operator:
(x := 5) # returns x and x is a variable now
Research more on those operators.
But for why we have this limitation is because there's no difference between a lambda and a function in python so why not make a function, and it will probably be controversial for example the warlus operator was so controversial that Guido Van Rossum (creator of python) resigned as BDFL
+ 5
Hima , your comment, what lambda can't have, seems to be not correct.
- return, pass, assert, raise -> can not be used
- expressions are possible. see sample of some valid lamdbas
- y = x*2 is an assignment with an expression. These can not be used in lambdas
here some samples of valid lambdas:
map_ = list(map(lambda x: x*2, list_))
result = list(filter(lambda x: (x % 2 == 1) and (x >= 10), fib))
print(reduce(lambda x,y:x*y,range(1,n+1)))
res = sorted(A,key=lambda x : x[2].lower())
+ 4
Lothar
That was just to show a difference between expression and statement .
I never said lambdas can have statements .
I should have put it in a better way .
đ
+ 3
a lambda can't have these:
return
pass
assert
raise
x**2 # an expression
y=x**2 # a statement
+ 3
Oma Falk
expressions :
a*b+c
max(a,b,c)
"aa"+"aa"
statements :
def fun()
return something
try :
something
If clause:
something
+ 2
Hima can u give me
3 statement examples
and
3 expression examples please ?
+ 2
Expressions refer to anything that can be evaluated to produce a value. An Expression must return a value.
Statements do not return a value. They perform an action, and evaluate nothing. Statements may contain expressions, which must be evaluated. But the statement itself is not evaluated.
Examples for expressions:
1
4*3
âHello Worldâ
âHelloâ + â Worldâ
False
Examples for statements:
return
x = 17
if x:
print()
+ 1
BDFL?
+ 1
print() is an expression. Just because it has a side effect doesnt mean it's not an expression. It still returns None
+ 1
Zachiah sawyer You are right, âprintâ was a statement in Python 2, but since Python 3 it is a function.
0
from __future__ import print_function
Lol
0
Hello,
A lambda expression is a syntax that allows you to create a function without name directly inside your code, as an expression.
There are two kinds of lambda expressions, depending on their body:
expression lambdas, whose body is just an expression, e.g. (i, j) => i + j
statement lambdas, whose body is a full statement, e.g.. (i, j) => { return i + j; }
i hope this will help to you..