Python presentations

Discussion in 'Python' started by andrea crotti, Sep 13, 2012.

  1. 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?
     
    andrea crotti, Sep 13, 2012
    #1
    1. Advertising

  2. andrea crotti

    John Gordon Guest

    In <> andrea crotti <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Sep 13, 2012
    #2
    1. Advertising

  3. andrea crotti

    mblume Guest

    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
     
    mblume, Sep 13, 2012
    #3
  4. 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
     
    88888 Dihedral, Sep 13, 2012
    #4
  5. 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]
     
    Alexander Blinne, Sep 13, 2012
    #5
  6. On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <> 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
     
    Chris Angelico, Sep 13, 2012
    #6
  7. andrea crotti

    Miki Tebeka Guest

    > 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
     
    Miki Tebeka, Sep 13, 2012
    #7
  8. andrea crotti

    Miki Tebeka Guest

    > 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
     
    Miki Tebeka, Sep 13, 2012
    #8
  9. 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..
     
    Andrea Crotti, Sep 14, 2012
    #9
  10. Chris Angelicoæ–¼ 2012å¹´9月14日星期五UTC+8上åˆ6時39分25秒寫é“:
    > On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <> 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.
     
    88888 Dihedral, Sep 14, 2012
    #10
  11. Chris Angelicoæ–¼ 2012å¹´9月14日星期五UTC+8上åˆ6時39分25秒寫é“:
    > On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <> 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.
     
    88888 Dihedral, Sep 14, 2012
    #11
  12. On 14.09.2012 00:38, Chris Angelico wrote:
    > On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <> 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)
     
    Alexander Blinne, Sep 14, 2012
    #12
  13. On Fri, Sep 14, 2012 at 9:47 PM, Alexander Blinne <> wrote:
    > On 14.09.2012 00:38, Chris Angelico wrote:
    >> On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <> 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
     
    Chris Angelico, Sep 14, 2012
    #13
  14. 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
     
    Alexander Blinne, Sep 16, 2012
    #14
  15. On Mon, Sep 17, 2012 at 2:13 AM, Alexander Blinne <> 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
     
    Chris Angelico, Sep 16, 2012
    #15
  16. 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
     
    Steven D'Aprano, Sep 16, 2012
    #16
  17. 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 :)
     
    Alexander Blinne, Sep 17, 2012
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Python for Presentations

    Chris, Apr 14, 2004, in forum: Python
    Replies:
    3
    Views:
    419
  2. andrea crotti

    Re: Python presentations

    andrea crotti, Sep 13, 2012, in forum: Python
    Replies:
    0
    Views:
    165
    andrea crotti
    Sep 13, 2012
  3. Jean-Michel Pichavant

    Re: Python presentations

    Jean-Michel Pichavant, Sep 13, 2012, in forum: Python
    Replies:
    21
    Views:
    404
    88888 Dihedral
    Sep 19, 2012
  4. Cameron Simpson

    Re: Python presentations

    Cameron Simpson, Sep 14, 2012, in forum: Python
    Replies:
    0
    Views:
    154
    Cameron Simpson
    Sep 14, 2012
  5. Trent Nelson

    Re: Python presentations

    Trent Nelson, Sep 19, 2012, in forum: Python
    Replies:
    0
    Views:
    154
    Trent Nelson
    Sep 19, 2012
Loading...

Share This Page