Display Powers of 2 Using Anonymous Function (Lambda) in Python

(Last Updated On: 18/11/2022)

Python Program to Display Powers of 2 Using Anonymous Function (Lambda)

# Display Powers of 2 Using Anonymous Function (lambda) in python

# we are taking a number from user as input
# entered value will be converted to int from string
total_terms = int(input("Enter the Total Number of Terms: "))

anoms = lambda x: 2 ** x

for i in range(total_terms):
	print("2 raised to the power ", i, " is ", anoms(i))

Output:

Enter the Total Number of Terms: 10
2 raised to the power  0  is  1
2 raised to the power  1  is  2
2 raised to the power  2  is  4
2 raised to the power  3  is  8
2 raised to the power  4  is  16
2 raised to the power  5  is  32
2 raised to the power  6  is  64
2 raised to the power  7  is  128
2 raised to the power  8  is  256
2 raised to the power  9  is  512

Leave a Reply

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