{"id":6845,"date":"2023-10-31T17:17:10","date_gmt":"2023-10-31T11:47:10","guid":{"rendered":"https:\/\/python-programs.com\/?p=6845"},"modified":"2023-11-10T12:10:01","modified_gmt":"2023-11-10T06:40:01","slug":"python-program-to-compute-the-power-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-compute-the-power-of-a-number\/","title":{"rendered":"Python Program to Compute the Power of a Number"},"content":{"rendered":"
Power:<\/strong><\/p>\n A power is an expression that describes repeated multiplication of the same factor.<\/p>\n Example1:<\/strong><\/p>\n Positive Exponent:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Negative Exponent:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n This article will teach you how to compute the power of a number.<\/p>\n Below are the ways to calculate the power of a number :<\/p>\n Explore more instances related to python concepts from Python Programming Examples<\/a> Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n We can calculate the power value using pow() function.<\/p>\n It works for negative exponent too.<\/p>\n 1)Calculating the power of positive exponent<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Calculating the power of negative exponent<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can calculate the power value using **<\/strong> .<\/p>\n It works for negative exponent too.<\/p>\n 1)Calculating the power of positive exponent<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Calculating the power of negative exponent<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Power: A power is an expression that describes repeated multiplication of the same factor. Examples: Example1: Positive Exponent: Input: base = 7 power = 4 Output: The result is 2401 Example2: Negative Exponent: Input: base = 7 power = -2 Output: The result is 0.02040816326530612 This article will teach you how to compute the power …<\/p>\nExamples:<\/h3>\n
base = 7 power = 4<\/pre>\n
The result is 2401<\/pre>\n
base = 7 power = -2<\/pre>\n
The result is 0.02040816326530612<\/pre>\n
Calculate the Power of a Number<\/h2>\n
\n
Method #1:Using While loop<\/h3>\n
\n
# given base value and power value\r\nbaseValue = 7\r\npowerValue = 4\r\n# initialize the result value to 1\r\nresultValue = 1\r\n# Loop till power becomes 0\r\nwhile powerValue != 0:\r\n resultValue = resultValue * baseValue\r\n powerValue = powerValue-1\r\n\r\nprint(\"The result is\", resultValue)\r\n<\/pre>\n
The result is 2401<\/pre>\n
Method #2: Using For loop<\/h3>\n
\n
# given base value and power value\r\nbaseValue = 7\r\npowerValue = 4\r\n# initialize the result value to 1\r\nresultValue = 1\r\n# Loop till power becomes 0 using for loop\r\nfor i in range(powerValue):\r\n resultValue = resultValue*baseValue\r\n # decrement the powerValue\r\n powerValue = powerValue-1\r\n\r\nprint(\"The result is\", resultValue)\r\n<\/pre>\n
The result is 2401<\/pre>\n
If you have a negative exponent, none of the above methods will function.\r\nYou must use the pow() function from the Python library to accomplish this.<\/pre>\n
Method #3:Using pow() function<\/h3>\n
# given base value and power value\r\nbaseValue = 7\r\npowerValue = 4\r\n# calculating the power of base\r\nresultValue = pow(baseValue, powerValue)\r\n# print the result\r\nprint(\"The result is\", resultValue)\r\n<\/pre>\n
The result is 2401<\/pre>\n
# given base value and power value\r\nbaseValue = 7\r\npowerValue = -2\r\n# calculating the power of base\r\nresultValue = pow(baseValue, powerValue)\r\n# print the result\r\nprint(\"The result is\", resultValue)\r\n<\/pre>\n
The result is 0.02040816326530612<\/pre>\n
Method #4:Using ** operator in Python<\/h3>\n
# given base value and power value\r\nbaseValue = 7\r\npowerValue = 4\r\n# calculating the power of base\r\nresultValue = baseValue**powerValue\r\n# print the result\r\nprint(\"The result is\", resultValue)\r\n<\/pre>\n
The result is 2401<\/pre>\n
# given base value and power value\r\nbaseValue = 7\r\npowerValue = -2\r\n# calculating the power of base\r\nresultValue = baseValue**powerValue\r\n# print the result\r\nprint(\"The result is\", resultValue)\r\n<\/pre>\n
The result is 0.02040816326530612<\/pre>\n
\n