Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81415 > unrolled thread

Re: "**" in python

Started bySkip Montanaro <skip.montanaro@gmail.com>
First post2014-11-23 19:48 -0600
Last post2014-12-15 22:31 +1100
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: "**" in python Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-23 19:48 -0600
    Re: "**" in python llanitedave <llanitedave@birdandflower.com> - 2014-11-23 18:30 -0800
    Re: "**" in python Steven D'Aprano <steve@pearwood.info> - 2014-11-24 04:47 +0000
      Re: "**" in python Chris Angelico <rosuav@gmail.com> - 2014-11-24 16:18 +1100
    Re: "**" in python albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-12-15 10:33 +0000
      Re: "**" in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-15 22:31 +1100

#81415 — Re: "**" in python

FromSkip Montanaro <skip.montanaro@gmail.com>
Date2014-11-23 19:48 -0600
SubjectRe: "**" in python
Message-ID<mailman.16217.1416793733.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

I want to add one more thing to the other responses. People new to Python
often seem unaware that being an interpreted language, often the best way
to figure something out is to simply try it at the interpreter prompt. The
OP saw "var ** 2" in done code. The most obvious thing to me would have
been to type

var = 42
var ** 2

and see what happened. It's unlikely to trigger a nuclear meltdown, or even
crash the interpreter.

Skip

[toc] | [next] | [standalone]


#81417

Fromllanitedave <llanitedave@birdandflower.com>
Date2014-11-23 18:30 -0800
Message-ID<ddd49362-4e0f-4924-9ce3-e0710b8e6ce5@googlegroups.com>
In reply to#81415
On Sunday, November 23, 2014 5:49:05 PM UTC-8, Skip Montanaro wrote:
> I want to add one more thing to the other responses. People new to Python often seem unaware that being an interpreted language, often the best way to figure something out is to simply try it at the interpreter prompt. The OP saw "var ** 2" in done code. The most obvious thing to me would have been to type
> 
> var = 42
> 
> var ** 2
> 
> and see what happened. It's unlikely to trigger a nuclear meltdown, or even crash the interpreter.
> 
> Skip


I dunno, if it took 7.5 million years to come up with the answer in the first place, imagine how long it might take to square it!

[toc] | [prev] | [next] | [standalone]


#81424

FromSteven D'Aprano <steve@pearwood.info>
Date2014-11-24 04:47 +0000
Message-ID<5472b869$0$2855$c3e8da3$76491128@news.astraweb.com>
In reply to#81415
On Sun, 23 Nov 2014 19:48:50 -0600, Skip Montanaro wrote:

> I want to add one more thing to the other responses. People new to
> Python often seem unaware that being an interpreted language, often the
> best way to figure something out is to simply try it at the interpreter
> prompt. The OP saw "var ** 2" in done code. The most obvious thing to me
> would have been to type
> 
> var = 42
> var ** 2
> 
> and see what happened. It's unlikely to trigger a nuclear meltdown, or
> even crash the interpreter.

Hmmm, it appears that ** is a no-op that always returns its left hand 
argument:


py> 0**2
0
py> 0**7
0
py> 0**99
0

Of course, that could be a coincidence, better try a few other things:

py> 1**57
1
py> 57**1
57
py> -1**12
-1



:-)



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#81430

FromChris Angelico <rosuav@gmail.com>
Date2014-11-24 16:18 +1100
Message-ID<mailman.16226.1416806303.18130.python-list@python.org>
In reply to#81424
On Mon, Nov 24, 2014 at 3:47 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> Hmmm, it appears that ** is a no-op that always returns its left hand
> argument:
>
> py> -1**12
> -1

Ahh but you see, you misunderstand how the whole negative-numbers
thing works. You have your syntax wrong. Python was developed by an
accountant, and you write negative numbers like this:

>>> (1)**12
1

And then, because you're raising it to an even power, you get back
positive one. The hyphen is actually an alignment operator, and it
means "left-align this value".

https://www.python.org/dev/peps/pep-0461/
https://docs.python.org/2/library/stdtypes.html#string-formatting

:)

ChrisA

[toc] | [prev] | [next] | [standalone]


#82557

Fromalbert@spenarnc.xs4all.nl (Albert van der Horst)
Date2014-12-15 10:33 +0000
Message-ID<548eb910$0$6867$e4fe514c@dreader36.news.xs4all.nl>
In reply to#81415
In article <mailman.16217.1416793733.18130.python-list@python.org>,
Skip Montanaro  <skip.montanaro@gmail.com> wrote:
>-=-=-=-=-=-
>
>I want to add one more thing to the other responses. People new to Python
>often seem unaware that being an interpreted language, often the best way
>to figure something out is to simply try it at the interpreter prompt. The
>OP saw "var ** 2" in done code. The most obvious thing to me would have
>been to type
>
>var = 42
>var ** 2
>
>and see what happened. It's unlikely to trigger a nuclear meltdown, or even
>crash the interpreter.
>
>Skip
>
>-=-=-=-=-=-
>[Alternative: text/html]
>-=-=-=-=-=-


With some perseverance, you can ask the interpreter what `` ** ''
does:

help(**)
<syntax error>
maybe so?
help('**')
Indeed, a whole description.

help(var)
help(42)
also work.

Come on, guys and dolls! Your advice to a newbies is soso,
if in this kind of answer, the help facilitiy is not
mentionned.


Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#82560

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-12-15 22:31 +1100
Message-ID<548ec689$0$13005$c3e8da3$5496439d@news.astraweb.com>
In reply to#82557
Albert van der Horst wrote:

> With some perseverance, you can ask the interpreter what `` ** ''
> does:
> 
> help(**)
> <syntax error>
> maybe so?
> help('**')
> Indeed, a whole description.

Yes! Well spotted!



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web