Example HTML page

A Python program to display the powers of ‘3’ using anonymous functions/lambda functions.

In python Lambda/Anonymous functions are those functions which are declared without name. Those anonymous functions are defined by a keyword known as “Lambda” and hence those are also called a lambda functions. The following is an example program that displays the powers of 3 using anonymous functions/lambda functions.

count = 10
b=int(input("Enter a number: "))
my_list = list(map(lambda x: b ** x , range(count)))
print("The powers of ",b," upto range of 10 are: ")
for i in range(count):
   print(b," to the power of ",i,"is",my_list[i])

Explanation

In the above program the ‘count’ is set to 10 and it states that to print the powers of the input number upto range of 10 (i.e 0 to 9). The map( ) function inside the statement my_list = list(map(lambda x: b ** x , range(count))) returns the values of lambda expression x: b ** x for each element in the list upto a specified range and is stored in a variable called ‘my_list’ here. We used for loop here to perform looping on the every element in a list upto the range and finally print the powers of input number.

Output

Enter a number: 5

The powers of  5  upto range of 10 are:

5  to the power of  0 is 1

5  to the power of  1 is 5

5  to the power of  2 is 25

5  to the power of  3 is 125

5  to the power of  4 is 625

5  to the power of  5 is 3125

5  to the power of  6 is 15625

5  to the power of  7 is 78125

5  to the power of  8 is 390625

5  to the power of  9 is 1953125

TestCase 2

Enter a number: 7

The powers of  7  upto range of 10 are:

7  to the power of  0 is 1

7  to the power of  1 is 7

7  to the power of  2 is 49

7  to the power of  3 is 343

7  to the power of  4 is 2401

7  to the power of  5 is 16807

7  to the power of  6 is 117649

7  to the power of  7 is 823543

7  to the power of  8 is 5764801

7  to the power of  9 is 40353607

 

                                      

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest