def square(num): return num * numdef square(num): return num * numprint(square(9)) # 81"""A lambda function that does the same thinglambda (some_parameter) : (a single expression that is automatically returned)"""lambda num: num * num # think of it like a no named functionsquare2 = lambda num: num * num # you can store it in a variableprint(square2(9)) # calling this lambda functionadd = lambda a,b: a + bprint(add(1,2)) # 3
Common use case: when you pass in a function to another function as a parameter and that function will never be used again
The point is that it’s in line and we don’t need to make another function
# a simple button using tk button = tk.Button( frame, text="CLICK ME", fg="red", command=lambda: print("Hello") # prints "Hello" whenever button is clicked # we don't even need a parameter)