Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29049 > unrolled thread
| Started by | andrea crotti <andrea.crotti.0@gmail.com> |
|---|---|
| First post | 2012-09-13 17:00 +0100 |
| Last post | 2012-09-13 15:58 -0700 |
| Articles | 17 — 9 participants |
Back to article view | Back to comp.lang.python
Python presentations andrea crotti <andrea.crotti.0@gmail.com> - 2012-09-13 17:00 +0100
Re: Python presentations John Gordon <gordon@panix.com> - 2012-09-13 16:06 +0000
Re: Python presentations mblume <foobar@invalid.invalid> - 2012-09-13 16:26 +0000
Re: Python presentations 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-13 12:01 -0700
Re: Python presentations Alexander Blinne <news@blinne.net> - 2012-09-14 00:33 +0200
Re: Python presentations Chris Angelico <rosuav@gmail.com> - 2012-09-14 08:38 +1000
Re: Python presentations 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-13 22:57 -0700
Re: Python presentations 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-13 22:57 -0700
Re: Python presentations Alexander Blinne <news@blinne.net> - 2012-09-14 13:47 +0200
Re: Python presentations Chris Angelico <rosuav@gmail.com> - 2012-09-14 22:19 +1000
Re: Python presentations Alexander Blinne <news@blinne.net> - 2012-09-16 18:13 +0200
Re: Python presentations Chris Angelico <rosuav@gmail.com> - 2012-09-17 02:19 +1000
Re: Python presentations Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-16 17:35 +0000
Re: Python presentations Alexander Blinne <news@blinne.net> - 2012-09-17 10:39 +0200
Re: Python presentations Miki Tebeka <miki.tebeka@gmail.com> - 2012-09-13 15:58 -0700
Re: Python presentations Andrea Crotti <andrea.crotti.0@gmail.com> - 2012-09-14 00:15 +0100
Re: Python presentations Miki Tebeka <miki.tebeka@gmail.com> - 2012-09-13 15:58 -0700
| From | andrea crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2012-09-13 17:00 +0100 |
| Subject | Python presentations |
| Message-ID | <mailman.617.1347552022.27098.python-list@python.org> |
I have to give a couple of Python presentations in the next weeks, and I'm still thinking what is the best approach. In one presentation for example I will present decorators and context managers, and my biggest doubt is how much I should show and explain in slides and how much in an interactive way (with ipython for example). For my experience if I only see code in slides I tend not to believe that it works somehow, but also only looking at someone typing can be hard to follow and understand what is going on.. So maybe I should do first slides and then interactive demo, or the other way around, showing first how everything works and then explaining the code with slides. What do you think work best in general?
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2012-09-13 16:06 +0000 |
| Message-ID | <k2t09d$dlf$1@reader1.panix.com> |
| In reply to | #29049 |
In <mailman.617.1347552022.27098.python-list@python.org> andrea crotti <andrea.crotti.0@gmail.com> writes:
> For my experience if I only see code in slides I tend not to believe
> that it works somehow
Presumably you will have some credibility with your audience so they won't
just assume you're making it up?
I think slides would be fine.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | mblume <foobar@invalid.invalid> |
|---|---|
| Date | 2012-09-13 16:26 +0000 |
| Message-ID | <k2t1f9$4jp$1@news.albasani.net> |
| In reply to | #29049 |
Am Thu, 13 Sep 2012 17:00:19 +0100 schrieb andrea crotti: > > I have to give a couple of Python presentations in the next weeks, and > I'm still thinking what is the best approach. > My idea for an introductory presentation of python was to prepare some code snippets (all valid python), show them in the editor, explain them, then run in a console. Beginners could then use these code snippets for their own experiments. HTH Martin
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-13 12:01 -0700 |
| Message-ID | <06a1a81b-246b-4e7b-ba34-4701f46889c8@googlegroups.com> |
| In reply to | #29052 |
mblume於 2012年9月14日星期五UTC+8上午12時26分17秒寫道:
> Am Thu, 13 Sep 2012 17:00:19 +0100 schrieb andrea crotti:
>
> >
>
> > I have to give a couple of Python presentations in the next weeks, and
>
> > I'm still thinking what is the best approach.
>
> >
>
> My idea for an introductory presentation of python was to prepare some
>
> code snippets (all valid python), show them in the editor, explain them,
>
> then run in a console.
>
>
>
> Beginners could then use these code snippets for their own experiments.
>
>
>
> HTH
>
> Martin
I'll contribute one point in Python.
def powerlist(x, n):
# n is a natural number
result=[]
y=1
for i in xrange(n):
result.append(y)
y*=x
return result # any object in the local function can be returned
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-09-14 00:33 +0200 |
| Message-ID | <50525f48$0$6573$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #29069 |
On 13.09.2012 21:01, 88888 Dihedral wrote:
> def powerlist(x, n):
> # n is a natural number
> result=[]
> y=1
> for i in xrange(n):
> result.append(y)
> y*=x
> return result # any object in the local function can be returned
def powerlist(x, n):
result=[1]
for i in xrange(n-1):
result.append(result[-1]*x)
return result
def powerlist(x,n):
if n==1:
return [1]
p = powerlist(x,n-1)
return p + [p[-1]*x]
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-09-14 08:38 +1000 |
| Message-ID | <mailman.646.1347575926.27098.python-list@python.org> |
| In reply to | #29082 |
On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <news@blinne.net> wrote: > On 13.09.2012 21:01, 88888 Dihedral wrote: >> def powerlist(x, n): >> # n is a natural number >> result=[] >> y=1 >> for i in xrange(n): >> result.append(y) >> y*=x >> return result # any object in the local function can be returned > > def powerlist(x, n): > result=[1] > for i in xrange(n-1): > result.append(result[-1]*x) > return result > > def powerlist(x,n): > if n==1: > return [1] > p = powerlist(x,n-1) > return p + [p[-1]*x] Eh, much simpler. def powerlist(x,n): return [x*i for i in xrange(n-1)] But you're responding to a bot there. Rather clever as bots go, though. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-13 22:57 -0700 |
| Message-ID | <f5fb8228-a57a-4ab9-af80-938dccd81e79@googlegroups.com> |
| In reply to | #29084 |
Chris Angelico於 2012年9月14日星期五UTC+8上午6時39分25秒寫道: > On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <news@blinne.net> wrote: > > > On 13.09.2012 21:01, 88888 Dihedral wrote: > > >> def powerlist(x, n): > > >> # n is a natural number > > >> result=[] > > >> y=1 > > >> for i in xrange(n): > > >> result.append(y) > > >> y*=x > > >> return result # any object in the local function can be returned > > > > > > def powerlist(x, n): > > > result=[1] > > > for i in xrange(n-1): > > > result.append(result[-1]*x) > > > return result > > > > > > def powerlist(x,n): > > > if n==1: > > > return [1] > > > p = powerlist(x,n-1) > > > return p + [p[-1]*x] > > > > Eh, much simpler. > > > > def powerlist(x,n): > > return [x*i for i in xrange(n-1)] > > > > But you're responding to a bot there. Rather clever as bots go, though. > > > > ChrisA I do not object the list comprehension in concept. But I have to convert python code to cython from time to time. Well, this imposes some coding style definitely.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-13 22:57 -0700 |
| Message-ID | <mailman.674.1347602283.27098.python-list@python.org> |
| In reply to | #29084 |
Chris Angelico於 2012年9月14日星期五UTC+8上午6時39分25秒寫道: > On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <news@blinne.net> wrote: > > > On 13.09.2012 21:01, 88888 Dihedral wrote: > > >> def powerlist(x, n): > > >> # n is a natural number > > >> result=[] > > >> y=1 > > >> for i in xrange(n): > > >> result.append(y) > > >> y*=x > > >> return result # any object in the local function can be returned > > > > > > def powerlist(x, n): > > > result=[1] > > > for i in xrange(n-1): > > > result.append(result[-1]*x) > > > return result > > > > > > def powerlist(x,n): > > > if n==1: > > > return [1] > > > p = powerlist(x,n-1) > > > return p + [p[-1]*x] > > > > Eh, much simpler. > > > > def powerlist(x,n): > > return [x*i for i in xrange(n-1)] > > > > But you're responding to a bot there. Rather clever as bots go, though. > > > > ChrisA I do not object the list comprehension in concept. But I have to convert python code to cython from time to time. Well, this imposes some coding style definitely.
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-09-14 13:47 +0200 |
| Message-ID | <5053196e$0$6578$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #29084 |
On 14.09.2012 00:38, Chris Angelico wrote: > On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <news@blinne.net> wrote: >> def powerlist(x,n): >> if n==1: >> return [1] >> p = powerlist(x,n-1) >> return p + [p[-1]*x] > > Eh, much simpler. > > def powerlist(x,n): > return [x*i for i in xrange(n-1)] I suppose you meant: def powerlist(x,n): return [x**i for i in xrange(n-1)] But this is less efficient, because it needs more multiplications (see Horner's method)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-09-14 22:19 +1000 |
| Message-ID | <mailman.699.1347625166.27098.python-list@python.org> |
| In reply to | #29158 |
On Fri, Sep 14, 2012 at 9:47 PM, Alexander Blinne <news@blinne.net> wrote: > On 14.09.2012 00:38, Chris Angelico wrote: >> On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <news@blinne.net> wrote: >>> def powerlist(x,n): >>> if n==1: >>> return [1] >>> p = powerlist(x,n-1) >>> return p + [p[-1]*x] >> >> Eh, much simpler. >> >> def powerlist(x,n): >> return [x*i for i in xrange(n-1)] > > I suppose you meant: > > def powerlist(x,n): > return [x**i for i in xrange(n-1)] > > But this is less efficient, because it needs more multiplications (see > Horner's method) Err, yes, I did mean ** there. The extra multiplications may be slower, but which is worse? Lots of list additions, or lots of integer powers? In the absence of clear and compelling evidence, I'd be inclined to go with the list comp - and what's more, to skip this function altogether and use the list comp directly where it's needed. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-09-16 18:13 +0200 |
| Message-ID | <5055fab1$0$6579$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #29161 |
On 14.09.2012 14:19, Chris Angelico wrote:
> Err, yes, I did mean ** there. The extra multiplications may be
> slower, but which is worse? Lots of list additions, or lots of integer
> powers? In the absence of clear and compelling evidence, I'd be
> inclined to go with the list comp - and what's more, to skip this
> function altogether and use the list comp directly where it's needed.
I did some timing with the following versions of the function:
def powerlist1(x, n):
result=[1]
for i in xrange(n-1):
result.append(result[-1]*x)
return result
def powerlist2(x,n):
if n==1:
return [1]
p = powerlist3(x,n-1)
p.append(p[-1]*x)
return p
def powerlist3(x,n):
return [x**i for i in xrange(n)]
with Python 2.7 you are quite right, i used x=4. Below n=26 powerlist3
is the fastest version, for n>26 powerlist1 is faster, powerlist2 is
always slower than both.
With Pypy there is a completely different picture, with n<30 powerlist2
is way faster then the other two, but then powerlist1 is again faster
for greater n, somehow because now C long int cannot be used any longer.
for really big n powerlist3 always takes very much time :)
Alex
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-09-17 02:19 +1000 |
| Message-ID | <mailman.804.1347812402.27098.python-list@python.org> |
| In reply to | #29331 |
On Mon, Sep 17, 2012 at 2:13 AM, Alexander Blinne <news@blinne.net> wrote: > def powerlist3(x,n): > return [x**i for i in xrange(n)] > > for really big n powerlist3 always takes very much time :) I would reiterate that a really big n is a really unusual use case for a function like this, except that... I frankly can't think of *any* use case for it!! But for many many applications, the simplicity and readability of a list comp instead of a function is usually going to outweigh the performance differences. However, it doesn't surprise me that individually raising a number to successive powers is slower than iterative multiplication, assuming you can't massively optimize eg with powers of 2 and bit shifts. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-09-16 17:35 +0000 |
| Message-ID | <50560df2$0$29981$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #29331 |
On Sun, 16 Sep 2012 18:13:36 +0200, Alexander Blinne wrote:
> I did some timing with the following versions of the function:
>
> def powerlist1(x, n):
> result=[1]
> for i in xrange(n-1):
> result.append(result[-1]*x)
> return result
>
> def powerlist2(x,n):
> if n==1:
> return [1]
> p = powerlist3(x,n-1)
> p.append(p[-1]*x)
> return p
Is that a typo? I think you mean to make a recursive call to powerlist2,
not a non-recursive call to powerlist3.
> def powerlist3(x,n):
> return [x**i for i in xrange(n)]
>
> with Python 2.7 you are quite right, i used x=4. Below n=26 powerlist3
> is the fastest version, for n>26 powerlist1 is faster, powerlist2 is
> always slower than both.
Making powerlist2 recursive, the results I get with Python 2.7 are:
py> from timeit import Timer as T
py> x = 2.357
py> n = 8
py> t1 = T('powerlist1(x, n)',
... setup='from __main__ import powerlist1, x, n')
py> t2 = T('powerlist2(x, n)',
... setup='from __main__ import powerlist2, x, n')
py> t3 = T('powerlist3(x, n)',
... setup='from __main__ import powerlist3, x, n')
py> min(t1.repeat(number=100000, repeat=5))
0.38042593002319336
py> min(t2.repeat(number=100000, repeat=5))
0.5992050170898438
py> min(t3.repeat(number=100000, repeat=5))
0.334306001663208
So powerlist2 is half as fast as the other two, which are very close.
For large n, #1 and #3 are still neck-and-neck:
py> n = 100
py> min(t1.repeat(number=100000, repeat=5))
3.6276791095733643
py> min(t3.repeat(number=100000, repeat=5))
3.58870792388916
which is what I would expect: the overhead of calling Python code will be
greater than the speed up from avoiding float multiplications. But long
integer unlimited-precision multiplications are slow. To really see the
advantage of avoiding multiplications using Horner's Method (powerlist1),
we need to use large integers and not floats.
py> x = 12345678*10000
py> n = 3
py> min(t1.repeat(number=100000, repeat=5))
0.2199108600616455
py> min(t3.repeat(number=100000, repeat=5))
0.551645040512085
As n becomes bigger, the advantage also increases:
py> n = 10
py> min(t1.repeat(number=100000, repeat=5))
0.736515998840332
py> min(t3.repeat(number=100000, repeat=5))
2.4837491512298584
In this case with n = 10, powerlist1 does 9 multiplications. But
powerlist3 makes ten calls to the ** operator. The number of
multiplications will depend on how cleverly exponentiation is
implemented: at worst, using a naive algorithm, there will be 36
multiplications. If the algorithm is a bit smarter, there will be 19
multiplications.
Either way, when the calculation is dominated by the cost of
multiplication, powerlist3 is between two and four times as expensive as
powerlist1.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-09-17 10:39 +0200 |
| Message-ID | <5056e1b6$0$6580$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #29339 |
On 16.09.2012 19:35, Steven D'Aprano wrote: > On Sun, 16 Sep 2012 18:13:36 +0200, Alexander Blinne wrote: >> def powerlist2(x,n): >> if n==1: >> return [1] >> p = powerlist3(x,n-1) >> p.append(p[-1]*x) >> return p > > Is that a typo? I think you mean to make a recursive call to powerlist2, > not a non-recursive call to powerlist3. Yes, it is a typo. I originally tested 2 more versions, but tried to change the numbering before posting. Bad idea :)
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-09-13 15:58 -0700 |
| Message-ID | <3540adcc-cf2d-46a8-a822-a3183d5ea944@googlegroups.com> |
| In reply to | #29049 |
> What do you think work best in general?
I find typing during class (other than small REPL examples) time consuming and error prone.
What works well for me is to create a slidy HTML presentation with asciidoc, then I can include code snippets that can be also run from the command line.
(Something like:
[source,python,numbered]
---------------------------------------------------
include::src/sin.py[]
---------------------------------------------------
Output example: http://i.imgur.com/Aw9oQ.png
)
Let me know if you're interested and I'll send you a example project.
HTH,
--
Miki
[toc] | [prev] | [next] | [standalone]
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2012-09-14 00:15 +0100 |
| Message-ID | <mailman.648.1347578212.27098.python-list@python.org> |
| In reply to | #29085 |
On 09/13/2012 11:58 PM, Miki Tebeka wrote: >> What do you think work best in general? > I find typing during class (other than small REPL examples) time consuming and error prone. > > What works well for me is to create a slidy HTML presentation with asciidoc, then I can include code snippets that can be also run from the command line. > (Something like: > > [source,python,numbered] > --------------------------------------------------- > include::src/sin.py[] > --------------------------------------------------- > > Output example: http://i.imgur.com/Aw9oQ.png > ) > > Let me know if you're interested and I'll send you a example project. > > HTH, > -- > Miki Yes please send me something and I'll have a look. For my slides I'm using hieroglyph: http://heiroglyph.readthedocs.org/en/latest/index.html which works with sphinx, so in theory I might be able to run the code as well.. But in general probably the best way is to copy and paste in a ipython session, to show that what I just explained actually works as expected..
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-09-13 15:58 -0700 |
| Message-ID | <mailman.647.1347577109.27098.python-list@python.org> |
| In reply to | #29049 |
> What do you think work best in general?
I find typing during class (other than small REPL examples) time consuming and error prone.
What works well for me is to create a slidy HTML presentation with asciidoc, then I can include code snippets that can be also run from the command line.
(Something like:
[source,python,numbered]
---------------------------------------------------
include::src/sin.py[]
---------------------------------------------------
Output example: http://i.imgur.com/Aw9oQ.png
)
Let me know if you're interested and I'll send you a example project.
HTH,
--
Miki
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web