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

Posted in

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

Vinay Khatri
Last updated on October 31, 2024

    Here in this tutorial, we will talk about a Python program that can print the powers of 2 using the Python lambda or Anonymous function.

    Python Concepts Utilized in the Program

    • Python Input, Output
    • Lambda function
    • Python Operators
    • Python Loop
    • Map function

    Steps to Create the Program

    • First, we ask the user to enter a number n , which represents the number up to which the user wants to print the power of 2.
    • By using the int() function, we will convert the entered string into an integer value.
    • Next, we need to use the map and lambda functions to create a map object which contains all the values of 2 raised to the power n.
    • Now, we use a list that converts the map object into a list.
    • At last, by using the for loop, we will print all the 2 raised to the power n values.

    Note:

    map(function, container) : map() is an in-built and powerful function in Python that accepts 2 arguments, namely function and container (list, tuple, etc..). The map function picks a value from the container one by one and passes it into the function for each value.

    lambda function: It is also known as the anonymous function, and it is an easy and quick hand technique to write small user-defined functions.

    Python Program to Display Powers of 2 Using Anon ymous Function (lambda Function)

    Python Code:

    n = int(input("Enter the number up to which you want to print the power of 2:" ))
    
    pow_2_lambda = lambda x:2**x
    list_of_2_pow = list(map(pow_2_lambda,list(range(n+1))))
    for i in range(n+1):
        print("2 raised to the power", i, "is", list_of_2_pow[I])

    Output 1:

    Enter the number up to which you want to print the power of 2: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
    2 raised to the power 10 is 1024

    Output 2:

    Enter the number up to which you want to print the power of 2:17
    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
    2 raised to the power 10 is 1024
    2 raised to the power 11 is 2048
    2 raised to the power 12 is 4096
    2 raised to the power 13 is 8192
    2 raised to the power 14 is 16384
    2 raised to the power 15 is 32768
    2 raised to the power 16 is 65536
    2 raised to the power 17 is 131072

    To Conclude it All

    We hope that this tutorial helped you understand the Python program that display powers of 2 using the Anonymous function or lambda function. If you have issues or queries, feel free to share them with us in the comments section below.

    People are also reading:

    Leave a Comment on this Post

    0 Comments