;

Python Program To Print Powers of 2 Using an Anonymous Function


Tutorialsrack 22/04/2020 Python

In this Python Program, we will learn how to print the powers of 2 using an anonymous function.

Here is the code of the program to print the powers of 2 using an anonymous function.

Python Program To Print Powers of 2 Using Anonymous Function

# Python Program To Print Powers of 2 Using Anonymous Function

# To take input from the user
terms = int(input("How many terms? "))

# use anonymous function
result = list(map(lambda x: 2 ** x, range(terms)))

print("The total terms are:",terms)
for i in range(terms):
   print("2 raised to power",i,"is",result[i])
Output

How many terms? 5

The total terms are: 5

2 raised to power 0 is 1

2 raised to power 1 is 2

2 raised to power 2 is 4

2 raised to power 3 is 8

2 raised to power 4 is 16


Related Posts



Comments

Recent Posts
Tags