Python: the power function
The tutor focuses on Python’s pow(x,y) command.
Back in my post on May 3, 2014 I pointed out that
In the left expression above, c is the root while b is the exponent. Some might also call b the “power”. The variable a you might call the argument.
On this website I use the familiar notation
Note that it follows that
√(49)=49^(1/2)=7
since the exponent on 49 in the left expression, not being written, is understood to be 1, while the root, not being stated, is assumed to be 2.
Another example:
3√(125)=(125)^(1/3)=5
Python’s pow function seems to favour the a^(b/c) interpretation. Before using pow, you need the line
import math
to make pow available. Then, pow can be used as follows:
math.pow(argument, rational exponent)
Therefore, the little program
import math
print(str(math.pow(3,2)))
print(str(math.pow(8,1/3)))
print(str(math.pow(32,2/5)))
should yield the output
9.0
2.0
4.0
I’ll be talking more about Python math functions in future posts:)
Source:
Donaldson, Toby. Python, 3rd Ed. Peachpit Press, 2014
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.