使用 Python 匿名函式列印冪?
在此,我們在 map() 內建函式中使用了匿名(lambda)函式。在 Python 中,匿名函式是無名稱定義的,使用 lambda 關鍵字進行定義。
演算法
Step 1: input n Step 2: input p Step 3: use anonymous function. Step 4: display result.
示例程式碼
# To display the powers of any number using anonymous function
n = int(input("Enter how many terms want to display??"))
p = int(input("Enter the number want to calculate power ::"))
# use anonymous function
cal = list(map(lambda i: p ** i, range(n)))
# display the result
print("The total terms is ::>", n)
for j in range(n):
print(p," raised to power", j, "is", cal[j])
輸出
Enter how many terms want to display??10 Enter the number want to calculate power ::3 The total terms is ::> 10 3 raised to power 0 is 1 3 raised to power 1 is 3 3 raised to power 2 is 9 3 raised to power 3 is 27 3 raised to power 4 is 81 3 raised to power 5 is 243 3 raised to power 6 is 729 3 raised to power 7 is 2187 3 raised to power 8 is 6561 3 raised to power 9 is 19683
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP