Python - Find the Powers of 2 Using Anonymous Function

In this program, you will learn how to find the powers of 2 using anonymous function.


Program Code



    #Display the powers of 2 using anonymous function

    terms = int(input('Enter the number of terms: '))
    
    # Use anonymous function
    result = list(map(lambda x: 2 ** x, range(terms)))
    
    for i in range(terms):
        print("2 raised to power",i,"is",result[i])

Output



    Enter the number of terms: 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

In this program, the lambda (anonymous) function has used inside map() built-in function to find the powers of 2.



The following topics can be useful to understand this program.



Related Tutorials