Welcome to our tutorial on how can we display the powers of 2 using an anonymous function (lambda) in Python! In this tutorial, we will be discussing how to use a lambda function in combination with the built-in “range()” function to display the powers of 2 up to a certain number of terms.
Python Program to Display Powers of 2 Using Anonymous Function (Lambda)
The first step in displaying the powers of 2 upto a certain number of terms is to define a lambda function that will calculate the power of 2 for a given number. In this example, we have defined a lambda function called “anoms” which takes one argument “x”, and returns 2 raised to the power of “x”.
The lambda function “anoms” is a small anonymous function that accepts a single argument x and returns the result of 2 raised to the power of x. This function is also known as an anonymous function because it is not bound to a specific name and can be used only once.
Once we have the function, we can take a number as input from the user using the “input()” function and store it in the variable “total_terms”. It’s worth noting that, the input() function returns the value as a string, so we will have to convert the string to an int before using it.
The input is being taken from the user and stored in the variable total_terms. The input function returns the input as a string, so it is important to convert the value to int before using it in further calculations.
After that, we can use the built-in “range()” function to loop through a range of numbers upto the number of terms. The “range()” function takes one or more arguments, and returns an iterator that generates a sequence of numbers.
The range(total_terms) function returns a range of numbers from 0 to total_terms-1, which is then being looped through using the for loop. Inside the loop, we can use the lambda function that we have defined earlier, and pass the current value of “i” to it. This will calculate the power of 2 for each value of “i” in the range.
# 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))
Finally, we can use the “print()” function to display the result of the calculation.
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
The result of the lambda function anoms(i) is stored in the variable result, and it is then printed along with the value of i using the print function.
One of the advantages of using a lambda function in this example is that it makes the code more concise and efficient. Lambda functions are also known as inline or anonymous functions as they are defined without a name and are used only once. They are useful when you need a small function for a single task, without the need to give it a specific name.
Another advantage of using lambda function is that it can be embedded inside a list, dictionary, or other data structure. This can be useful when you need to pass a function as an argument to another function or when you need to store a function in a data structure for later use.
In conclusion, displaying the powers of 2 upto a certain number of terms in Python is a simple task that can be achieved using a lambda function in combination with the built-in “range()” function and the “**” operator. By understanding how to use these functions and operators, you’ll be able to control the number of terms and the layout of your output in a more precise way.
Additionally, this example illustrates the use of lambda function, input function, type conversion, and looping in Python. With this tutorial, you now have a solid understanding of how to display the powers of 2 upto a certain number of terms using a lambda function in Python and how to use these techniques in your own projects.
It is important to note that using lambda function in Python can help you write more efficient and concise code, making it a useful tool to have in your programming toolbox. So, you can use this code snippet as a starting point and can build upon it to create more complex and sophisticated programs.
Overall, this tutorial has provided a strong foundation for you to understand and use lambda functions in Python and to display the powers of 2 upto a certain number of terms use the lambda function.
For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions