Re: [Tutor] Question on how to do exponents

2012-02-07 Thread Alan Gauld

On 07/02/12 01:01, Nate Lastname wrote:

Exponents ... are **(or ^)


Not quite the ^ operator is a bitwise XOR...

 2^2
0
 2^1
3

pow() is the other way to do exponents.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on how to do exponents

2012-02-07 Thread Sarma Tangirala
On 7 February 2012 13:49, Alan Gauld alan.ga...@btinternet.com wrote:

 On 07/02/12 01:01, Nate Lastname wrote:

 Exponents ... are **(or ^)


 Not quite the ^ operator is a bitwise XOR...

  2^2
 0
  2^1
 3

 pow() is the other way to do exponents.


Is is better to use pow() against **?


 --
 Alan G
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/

 __**_
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor




-- 
Sarma Tangirala,
Class of 2012,
Department of Information Science and Technology,
College of Engineering Guindy - Anna University
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on how to do exponents

2012-02-07 Thread Alan Gauld

On 07/02/12 16:54, Sarma Tangirala wrote:


Is is better to use pow() against **?


I suspect ** will be faster since it doesn't have the function
call overhead.

But I haven't tried timing it. Feel free to do some tests and find out.
Let us know how you get on!


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on how to do exponents

2012-02-07 Thread Steven D'Aprano

Sarma Tangirala wrote:


Is is better to use pow() against **?



Advantages of **

- it is shorter to type x**y vs pow(x, y)
- being an operator, it is slightly faster than calling a function
- you can't monkey-patch it

Disadvantages of **

- being an operator, you can't directly use it as a function-object
- it can't take three arguments
- hard to google for **
- you can't monkey-patch it

Advantages of pow()

- it is a function, so you can pass it around as an object
- three argument form
- easy to call help(pow) to see documentation
- easy to google for pow
- can be monkey-patched

Disadvantages of pow()

- a tiny bit slower due to the function call
- slightly longer to type
- can be monkey-patched


Weigh up the advantages and disadvantages of each, and make the call which is 
better for you.


(My preference is to use the ** operator.)



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on how to do exponents

2012-02-07 Thread Sarma Tangirala
On 8 February 2012 00:01, Steven D'Aprano st...@pearwood.info wrote:

 Sarma Tangirala wrote:

  Is is better to use pow() against **?



 Advantages of **

 - it is shorter to type x**y vs pow(x, y)
 - being an operator, it is slightly faster than calling a function
 - you can't monkey-patch it

 Disadvantages of **

 - being an operator, you can't directly use it as a function-object
 - it can't take three arguments
 - hard to google for **
 - you can't monkey-patch it

 Advantages of pow()

 - it is a function, so you can pass it around as an object
 - three argument form
 - easy to call help(pow) to see documentation
 - easy to google for pow
 - can be monkey-patched

 Disadvantages of pow()

 - a tiny bit slower due to the function call
 - slightly longer to type
 - can be monkey-patched


 Weigh up the advantages and disadvantages of each, and make the call which
 is better for you.

 (My preference is to use the ** operator.)


A simple function call argument would have done! :D Thanks for the survey!

Anyway, I was wondering about this, if internally pow() actually uses **. :P




 --
 Steven

 __**_
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor




-- 
Sarma Tangirala,
Class of 2012,
Department of Information Science and Technology,
College of Engineering Guindy - Anna University
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on how to do exponents

2012-02-07 Thread Dave Angel

On 02/07/2012 01:57 PM, Sarma Tangirala wrote:

On 8 February 2012 00:01, Steven D'Apranost...@pearwood.info  wrote:


Sarma Tangirala wrote:

  Is is better to use pow() against **?

Advantages of **

- it is shorter to type x**y vs pow(x, y)
- being an operator, it is slightly faster than calling a function
- you can't monkey-patch it

Disadvantages of **

- being an operator, you can't directly use it as a function-object
- it can't take three arguments
- hard to google for **
- you can't monkey-patch it

Advantages of pow()

- it is a function, so you can pass it around as an object
- three argument form
- easy to call help(pow) to see documentation
- easy to google for pow
- can be monkey-patched

Disadvantages of pow()

- a tiny bit slower due to the function call
- slightly longer to type
- can be monkey-patched


Weigh up the advantages and disadvantages of each, and make the call which
is better for you.

(My preference is to use the ** operator.)



A simple function call argument would have done! :D Thanks for the survey!

Anyway, I was wondering about this, if internally pow() actually uses **. :P


I have no idea, but I'd assume so, unless there's a 3rd argument.   At 
that point, the algorithm must change drastically.


--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on how to do exponents

2012-02-07 Thread bob gailer

On 2/7/2012 1:57 PM, Sarma Tangirala wrote:


Anyway, I was wondering about this, if internally pow() actually uses 
**. :P



 from dis  import dis
 dis(lambda a,b:a**b)
  1   0 LOAD_FAST0 (a)
  3 LOAD_FAST1 (b)
  6 BINARY_POWER
  7 RETURN_VALUE
 dis(lambda a,b:pow(a,b))
  1   0 LOAD_GLOBAL  0 (pow)
  3 LOAD_FAST0 (a)
  6 LOAD_FAST1 (b)
  9 CALL_FUNCTION2
 12 RETURN_VALUE

Now you know, and you know how to find out!

To delve any deeper you'd have to inspect the c source for pow.
I'd assume it uses the c exponent operator

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on how to do exponents

2012-02-06 Thread Nate Lastname
Exponents and remainder (modulus) are **(or ^) and % respectively.  I.E.;
d = a ** b (exponent)
c = a % b (modulus)

There you are!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on how to do exponents

2012-02-06 Thread Steven D'Aprano
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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor