Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python에서 Anonymous Function을 사용하여 출력 기능을 사용하시겠습니까?

<시간/>

여기에서는 map() 내장 함수 내부에 익명(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