Python function for power

Python Power | pow() | Python Power Operator

Python Power

The power (or exponent) of a number says how many times to use the number in a multiplication. Calculating the power of any number is a vital job. Because the power of a number can be needed in many mathematics as well as in big projects. It has many-fold applications in day to day programming. Calculating Python Power is easy and can be done in a few lines. Python comes with a host of different functions each built specifically to add more versatility to the interface than before.

To calculate the power of a number in Python we have basically three techniques or methods. The first one in the Python ecosystem is the power function, also represented as the pow(). The second way is to use the Power Operator. Represented or used as ** in Python. And the third one is using loops or iteration. In this article, we will discuss the Power function, Calculating the power of a number using loops and Power Operator in Python.

Читайте также:  Java как установить коды

What is Power pow() Function in Python?

One important basic arithmetic operator, in Python, is the exponent operator. It takes in two real numbers as input arguments and returns a single number. Pow can be used with 3 arguments to do modulo division.

Syntax of pow() Function

Parameters

The pow() function in Python takes three parameters:

  • x – a number, the base
  • y – a number, the exponent
  • z (optional) – a number, used for modulus

Meaning / Usage of x,y,z in pow()

The x,y,z parameters in pow() function can be used like the following.

  • pow(x, y) is equal to x y
  • pow(x, y, z) is equal to x y % z
  1. X: X can either be a non-negative integer or a negative integer whenever it is being used.
  2. Y: Y can also be a non-negative integer or a negative integer when used in the equation.
  3. Z: In most cases, z is an optional variable and may or may not be present.

Note: Here the third parameter in pow() is optional. So you can use the pow() function with only two parameters. And in place of x,y,z you can use any variable.

Return Value and Implementation Cases in pow() Function

pow() function

Examples to Calculate Python Power using pow() Function

In the following examples, we will calculate the Python Power of a number using pow() function.

Example 1: Basic Example to calculate the power of a number using pow() function

# taking x and y oth as positive print(pow(5, 2)) # taking x as negative and y as positive print(pow(-5, 2)) # taking x as positive and y as negative print(pow(5, -2)) # taking x as negative and y as negative print(pow(-5, -2))

Example 2: Using a floating number to calculate power of a number:

pow1 = pow(5,2.5) pow2 = pow(8,2.5) pow3 = pow(9,2.5) print("The exponent of 5**2.5 = ",pow1) print("The exponent of 8**2.5 = ",pow2) print("The exponent of 9**2.5 wp-block-code">The exponent of 5**2.5 = 55.90169943749474 The exponent of 8**2.5 = 181.01933598375618 The exponent of 9**2.5 = 243.0

Using a floating number to calculate power of a number.

Example 3: Using the third argument (modulo) in pow() function

It’s also possible to calculate a^b mod m.

This is very helpful in computations where you have to print the resultant % mod.

Note: Here, a and b can be floats or negatives, but, if a third argument is present, b cannot be negative.

Python program that uses pow with 3 arguments

x = 12 y = 2 z = 5 print(pow(x, y, z))

Explanation:

So in the above example, we have three parameters x,y and z. Here x is the base number. The y variable is the exponential or power of a number and z is the modulus.

The above example can be written in simple mathematics as

12^2%5 which is equal to 4

Example 4: Basic Example to calculate the power of a number using math.pow() function

Here in this example, we have to import the math module to calculate the power of a number.

import math print(math.pow(5,2))

Note: Python has a math module that has its own pow(). It takes two arguments and returns a floating-point number. Frankly speaking, we will never use math.pow().

What is Python Power Operator

In Python, power operator (**) and pow() function are equivalent. So we can use whichever one seems clearest. I feel pow is clearer, but I rarely need exponents in programs.

** operator used as a power(exponent) operator in python.

Raising a number to a power N multiplies the number by itself N times. For instance, 2 raised to the power of 3 is equal to 2 × 2 × 2 = 8 .

How to Use Power Operator in Python

To use the power operator (**) in Python, We have to put (**) between two numbers to raise the former number to the latter.

Syntax

Here a is Any expression evaluating to a numeric type. And b any expression evaluating to a numeric type.

Examples to Use the Power Operator in Python

#Python power operator example #Change the below value and try it yourself. a = 25 b = 3 result = a ** b print(result)

Examples to Use the Power Operator in Python

Note:

The python power operator accepts any numeric data type, such as an int or a floating-point number.

Calculating Python Power of a Number Using Loops

In this method to calculate the power of a number, we are using the for loop.
This method or way is also known as the naive method to compute power.

This Python program allows the user to enter any numerical value, exponent. Next, this Python program finds the power of a number using For Loop.

Program to calculate power of a number using for loop.

# Python Program to find Power of a Number using for loop number = int(input(" Please Enter any number : ")) exponent = int(input(" Enter Exponent Value : ")) power = 1 for i in range(1, exponent + 1): power = power * number print("The Result of Power = ".format(number, exponent, power))
Please Enter any number : 20 Enter Exponent Value : 2 The Result of 20 Power 2 = 400

This Python power of a number program using the while loop will be the same as above, but this time, we are using While Loop instead of for loop.

Must Read

Remarks

Python defines pow(0, 0) and 0 ** 0 to be 1, as is common for programming languages.

Conclusion

So we have covered all the possible ways or methods to calculate the Python Power of a number. Power function in Python, when used correctly can eliminate a lot of stress and confusion. We hope that from this article you have learned how to use the power function in Python correctly and thus will make use of the same in your day to day programming.

If you still have any doubts or suggestions regarding the python power do let us know in the comment section below.

Happy Coding!

Источник

Python pow() function Explained [Easy Examples]

The python pow() function can be used to derive the power of variable x to the variable y. If in a particular situation, the user includes a third variable that is z into the equation, then the math power function returns x to the power of y, modulus of z.

In this tutorial, we will learn about the python pow() function from scratch to an advanced level. We will take different cases with examples and see how we can use the python power function in various different cases. Moreover, we will also cover some of the alternative ways that we can use instead of the math power() function. Finally, we will also take some examples of the python power function with different formatted and complex numbers.

Getting started with python pow() function

The Python pow() function is a built-in function that returns x to the power of y (for parameters x and y ). If the third argument (let say z ) is given, it returns x to the power of y modulus z, i.e., pow(x, y) % z . In this section, we will see the syntax of the python pow() function and will solve some real examples.

Syntax of python pow() function

The python pow() function is a built-in function that comes with python installation. Here is a simple syntax of power function in Python.

It can take up to three-parameter ( c as an optional parameter).

  • a is the number in which power will be calculated. It can be a negative or positive.
  • b is the value raised to compute power.
  • c is a mod where the result can be taken as a mod. The m parameter is optional.

Check type of python pow() function

The python pow() method returns an integer value if the integer is given as an argument and it will return float data type if a float number is given. See the example below which confirm it.

# python pow() function num = pow(2, 3) # python data type print("data type is: <>".format(type(num)))

Notice that the return type is int because we had given integer as an argument. Now see the example below, which returns float data type.

# python pow() function num = pow(2.4, 3) # python data type print("data type is: <>".format(type(num)))

Notice that this time we get float data type as an output. Let us now take real examples and find the power of a number by using the python power function.

# python pow() function num = pow(2, 3) # printing the answer print("Power is: <>".format(num))

Now let us take an example of a float number and see what we get when we apply the python pow() function to the floating number.

# python pow() function num = pow(2.4, 3) # printing the answer print("Power is: <>".format(num))
Power is: 13.823999999999998

Notice that this time we get a floating number as an output.

Calculate power and mod using python pow() function

So far we have calculated the power of a number using the power function. We also know that the python pow() function takes an optional parameter as well, which returns the mod of the numbers. Let us now take an example and use that optional parameter to find the mod.

See the following example.

# python pow() function num = pow(2, 3) # printing the answer print("Power is: <>".format(num)) # now apply mode num1 = pow(2, 3, 2) # printing mod print("mod of the given number is: <>".format(num1))
Power is: 8 mod of the given number is: 0

Notice that the mod is zero because we know that 8 mod 2 is zero . Now let us find the mod with any other number. See the example below:

# python pow() function num = pow(2, 3) # printing the answer print("Power is: <>".format(num)) # now apply mode num1 = pow(2, 3, 3) # printing mod print("mod of the given number is: <>".format(num1))
Power is: 8 mod of the given number is: 2

This time we get 2 as a mod because 8 mod 3 is 2. Now note one thing that we cannot apply the mod to floating numbers. If we will try to find a mod of floating numbers, we will get an error. See the following example.

# python pow() function num = pow(2.34, 3) # printing the answer print("Power is: <>".format(num)) # now apply mode num1 = pow(2.34, 3, 3) # printing mod print("mod of the given number is: <>".format(num1))

Python pow() function Explained [Easy Examples]

Notice that we can only provide the third argument if the other arguments as of int data type.

Alternatives of Python power function

Python provides a number of ways to find the power of a number. We can use any of the ways to find the power. In this section, we will discuss some of the alternatives that we can use to find the power of a number, rather than using the built-in python pow() function.

Using a double asterisk to find the power

In python, we can use a double asterisk to find the power of numbers. It is popular and comes under python mathematical operators. The simple syntax looks like this:

where a and be are any numbers. Now let us take a real example and find the power of a number. See the following example.

# Alternative of python pow() function num = 2**3 # printing print(num)

Using this method, we can also find the power of floating numbers as well. See the example below:

# Alternative of python pow() function num = 2.3**3 # printing print(num)

Notice that we get a floating number as an output.

NumPy power method

The NumPy is a popular 3rd party Python library that also provides the pow() method. We can install the NumPy library by using the pip command;

Once the NumPy is installed successfully, we can access the pow() method. The following is the simple syntax of the python NumPy pow() method.

Now let us take a real example and find the power of numbers using the NumPy pow() method. See the example below:

# importing numpy import numpy # Alternative of python pow() function num = numpy.power(2, 3) # printing print("power is: <>".format(num))

we can also find the power of a floating number using this method as well. See the example below:

# importing numpy import numpy # Alternative of python pow() function num = numpy.power(2.3, 3) # printing print("power is: <>".format(num))

Источник

Оцените статью