+ 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?

5th Aug 2020, 11:51 AM
Oma Falk
Oma Falk - avatar
13 RĂ©ponses
+ 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
5th Aug 2020, 12:27 PM
12ksins
12ksins - avatar
+ 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())
5th Aug 2020, 3:28 PM
Lothar
Lothar - avatar
+ 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 . 👍
5th Aug 2020, 3:36 PM
Hima
Hima - avatar
+ 3
a lambda can't have these: return pass assert raise x**2 # an expression y=x**2 # a statement
5th Aug 2020, 12:01 PM
Hima
Hima - avatar
+ 3
Oma Falk expressions : a*b+c max(a,b,c) "aa"+"aa" statements : def fun() return something try : something If clause: something
5th Aug 2020, 4:17 PM
Hima
Hima - avatar
+ 2
Hima can u give me 3 statement examples and 3 expression examples please ?
5th Aug 2020, 12:43 PM
Oma Falk
Oma Falk - avatar
+ 2
Oma Falk B enevolent D ictator F or L ife Just means he decides if the pep (Python Enhancement Proposal) will be done or not.
5th Aug 2020, 12:45 PM
12ksins
12ksins - avatar
+ 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()
5th Aug 2020, 3:19 PM
Michael
Michael - avatar
+ 1
BDFL?
5th Aug 2020, 12:42 PM
Oma Falk
Oma Falk - avatar
+ 1
print() is an expression. Just because it has a side effect doesnt mean it's not an expression. It still returns None
7th Aug 2020, 3:57 PM
Zachiah sawyer
Zachiah sawyer - avatar
+ 1
Zachiah sawyer You are right, „print“ was a statement in Python 2, but since Python 3 it is a function.
7th Aug 2020, 4:29 PM
Michael
Michael - avatar
0
from __future__ import print_function Lol
7th Aug 2020, 4:31 PM
Zachiah sawyer
Zachiah sawyer - avatar
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..
13th Aug 2020, 6:10 AM
Ishan Shah
Ishan Shah - avatar