Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

5 ways to do Exponents (Square Roots) in Python

|| 0

5 ways to do Exponents (Square Roots) in Python
Stechies

1.‘**’ Operator

Exponent is to raise a number to the given power, in python ‘**’ operator is used to calculate exponent power.

Example ‘**’

print "exponent of 2 raised to power 4:", 2**4.
output will be :- is exponent of 2 raised to power 4: 16.

2. pow() Function

Alternatively, we can use the pow() function too. In pow() function there is two arguments one is base and the second one is the power value to raised value.

Syntax:

pow() along with an argument

Example :pow

import math
print "exponent of 2 raised to power 4:", math.pow(2,4)
output will be:- exponent of 2 raised to power 4: 16.0

3. exp() method

Another method is exp(), where it returns the exponent of e, the numeric argument passed in this function. Before calling this method we have to import the math module.

Syntax: exp() along with an argument.

Example:- exp()

import math
print "the exponent is ", math.exp(2.1)

Output:

the exponent is 8.16616991257

4. Logrithms() function

One another way to find out exponent using the logarithms. The log10 and log will give the exponent base 10 or base e, it is the default value.

Syntax: log () along with an argument

Example:

import math
print "the exponent is ", math.log(100)

Output:

$python main.py
the exponent is 4.60517018599

5. Using loop

We can also find out the exponent by using loop (here we use for loop) and pow function with example.

# for loop Python Program to find Exponent of a Number
num = int(input(" Please Enter Postive Integer : "))
epont = int(input(" Enter Exponent Value to the power raise to : "))
power = 1
for loo in range(1, epont + 1):
power = power * num
print("The Result of {0} Power {1} = {2}".format(num, epont, power))

Output:

Please Enter Postive Integer : 5
Enter Exponent Value to the power raise to : 2
The Result of 5 Power 2 = 25

 


Advertisement
Advertisement
Advertisement