I'm running a simple code in Python 2.7, but it is giving me syntax error.
hello = lambda first: print("Hello", first)
The error reported is SyntaxError: invalid syntax.
Python disallows the use of statements in lambda expressions:
Note that functions created with lambda expressions cannot contain statements or annotations.
print is a statement in Python 2, unless you import the print_function feature from __future__:
>>> lambda x: print(x)
File "<stdin>", line 1
lambda x: print(x)
^
SyntaxError: invalid syntax
>>> from __future__ import print_function
>>> lambda x: print(x)
<function <lambda> at 0x7f2ed301d668>
lambda x: print(x) then 1+1 inside a lamda, where the then represents something that just drops the None type and returns the right expression?print (the function) returns None, you can use the or operator: lambda x: print(x) or other_value.
print()is not a function in Python 2 unless you addfrom __future__ import print_functionto the top of your script. Why are you trying to use it as a function?