[Tutor] Question on how to do exponents
Steven D'Aprano
steve at pearwood.info
Tue Feb 7 02:07:08 CET 2012
On Mon, Feb 06, 2012 at 04:54:57PM -0800, William Stewart wrote:
> Hello everyone, I am making a calculator and I need to know how to make it do exponents and remainders
> How can I input this info in python?
> Any help would be appreciated
You can do exponents either with the ** operator or the pow()
function:
py> 2**4
16
py> pow(3, 5)
243
The pow() function is especially useful for advanced mathematics where
you want to perform exponents modulo some base:
py> pow(3, 5, 2)
1
That is, it calculates the remainder when divided by 2 of 3**5 *without*
needing to calculate 3**5 first. This is especially useful when
the intermediate number could be huge:
py> pow(1357924680, 2468013579, 1256711)
418453L
To get the remainder, use the % operator or the divmod() function:
py> 17 % 2
1
py> divmod(17, 2)
(8, 1)
Hope this helps.
P.S. please don't hijack threads by replying to an existing message, as
it could lead to some people not seeing your email. It is better to
start a new thread by using "New Message", not with "Reply".
--
Steven
More information about the Tutor
mailing list