As an Engineer — Python Advanced Lambda

更新於 發佈於 閱讀時間約 4 分鐘
A lambda function is a small anonymous function and can only have one expression.

Introduction

lambda is an anonymous and one-line function. It helps you replace simple functions with one line, achieving the simplicity that the Python programming language is most proud of.
Syntax:
lambda arguments : expression
Arguments are the variables you pass to this function, and expression are the logic your function performs.

Explanation

e.g. 1
def example1(x):
return x
lambda x : x
Let’s look at the first example, we can know that this function is called example1, and it will return the variable x passed in by the user.
lambda is an anonymous function, so it doesn’t need to be named example1. Before the : is the variable x passed in by the user, and after the : is the return value, because there is no math, so the expression ends here.
e.g.2
def example2(x):
if x > 10:
return x
else:
return 10
lambda x : x if x > 10 else 10
The second example should feel the power of lambda. When the original five-line program code is written in lambda, only one line is needed.
Analyze the way of writing lambda. The same user passes in a variable x, and returns x, but with some calculations. Returns x if x > 10, otherwise returns 10.
Right now, you should be familiar with the syntax of lambda. Then we combine some python syntax to write more magical lambda functions.
e.g.3
def example3(x):
y = []
for i in x:
y.append(i + 1)
return y
lambda x: list(map(lambda i : i+1, x))
By using a map instead of a for loop, one line of code can have for and addition logic.

Exercise

Now try changing the following function to a lambda.
def example4(x):
if x % 2 == 0:
return "even"
else:
return "odd"
Have a nice day!
為什麼會看到廣告
avatar-img
90會員
115內容數
除了翻譯各國新聞以外,會將過去演講的一些主題內容放上來。閒暇之餘,分享一些PM心得,歡迎參訪。
留言
avatar-img
留言分享你的想法!

































































你可能也想看
Google News 追蹤
Thumbnail
Python語法包括條件語句、迴圈、函數和變數的使用。條件語句如if、elif和else用於進行條件判斷,for和while是兩種主要的迴圈,def用於定義函數。變數可以被賦予數字或字符串,並可使用類型提示來指定變數的類型。註解可以是單行或多行,並可用於解釋函數或類的用途和作用。
在求學階段,你已經對代數的計算熟到不能再熟,所以變數(variable)對你來說應該不至於太陌生,先來看看以下這個例子:   
Thumbnail
Python 提供了一系列內建函式,其中一部分涉及數學和數學操作。 以下是一些常用的內建函式和數學相關的函式: 基本數學運算: abs(x): 返回 x 的絕對值。 result = abs(-5) print(result) # 輸出: 5 max(iterable) 和 min(
Thumbnail
本文介紹了Python中函式引數的*args和**kwargs用法,通過*args處理可變數量的位置引數,通過**kwargs處理可變數量的關鍵字引數。不僅介紹了相應的語法和程式範例,還解釋了它們的順序問題和建議的慣例用法。
Thumbnail
本文介紹了串列運算式的應用,以及與Lambda匿名函式方法的比較,並提供了程式範例。串列運算式提供了一種簡潔的語法,用於創建、轉換和過濾列表。lambda函式用於創建匿名函式,通常用於簡單的操作。建議在比較複雜的情況下使用一般for迴圈加if來表示。
Thumbnail
如果我只是想要重複做一些很簡單的運算,還有沒有更簡潔的方式,那就是Lambda匿名函式。 本文將介紹 : Lambda匿名函式的用法,也比較跟自定函式的差異之處。 結合map,filter,sorted函式做應用介紹
Thumbnail
在沒有分環境之前,每一隻lambda只有一個code console給所有人一起編輯,開發好了就deploy,根據設定的trigger觸發執行。 現在我們希望能夠在code console開發,然後deploy到不同的stage,目標是不同stage的api gateway能夠調用該lambda的
Thumbnail
本文將介紹自定函式及應用,利用程式範例解釋為什麼要用到自定函式 自定函式好處當然就是,讓你的程式碼看起來比較簡潔,在重複使用到的程式碼區塊,可以包裝成函式,讓你重複使用它。
Thumbnail
Python語法包括條件語句、迴圈、函數和變數的使用。條件語句如if、elif和else用於進行條件判斷,for和while是兩種主要的迴圈,def用於定義函數。變數可以被賦予數字或字符串,並可使用類型提示來指定變數的類型。註解可以是單行或多行,並可用於解釋函數或類的用途和作用。
在求學階段,你已經對代數的計算熟到不能再熟,所以變數(variable)對你來說應該不至於太陌生,先來看看以下這個例子:   
Thumbnail
Python 提供了一系列內建函式,其中一部分涉及數學和數學操作。 以下是一些常用的內建函式和數學相關的函式: 基本數學運算: abs(x): 返回 x 的絕對值。 result = abs(-5) print(result) # 輸出: 5 max(iterable) 和 min(
Thumbnail
本文介紹了Python中函式引數的*args和**kwargs用法,通過*args處理可變數量的位置引數,通過**kwargs處理可變數量的關鍵字引數。不僅介紹了相應的語法和程式範例,還解釋了它們的順序問題和建議的慣例用法。
Thumbnail
本文介紹了串列運算式的應用,以及與Lambda匿名函式方法的比較,並提供了程式範例。串列運算式提供了一種簡潔的語法,用於創建、轉換和過濾列表。lambda函式用於創建匿名函式,通常用於簡單的操作。建議在比較複雜的情況下使用一般for迴圈加if來表示。
Thumbnail
如果我只是想要重複做一些很簡單的運算,還有沒有更簡潔的方式,那就是Lambda匿名函式。 本文將介紹 : Lambda匿名函式的用法,也比較跟自定函式的差異之處。 結合map,filter,sorted函式做應用介紹
Thumbnail
在沒有分環境之前,每一隻lambda只有一個code console給所有人一起編輯,開發好了就deploy,根據設定的trigger觸發執行。 現在我們希望能夠在code console開發,然後deploy到不同的stage,目標是不同stage的api gateway能夠調用該lambda的
Thumbnail
本文將介紹自定函式及應用,利用程式範例解釋為什麼要用到自定函式 自定函式好處當然就是,讓你的程式碼看起來比較簡潔,在重複使用到的程式碼區塊,可以包裝成函式,讓你重複使用它。