Python presentations

A

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.

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?
 
J

John Gordon

In said:
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.
 
M

mblume

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
 
8

88888 Dihedral

mblumeæ–¼ 2012å¹´9月14日星期五UTC+8上åˆ12時26分17秒寫é“:
Am Thu, 13 Sep 2012 17:00:19 +0100 schrieb andrea crotti:





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
 
A

Alexander Blinne

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]
 
C

Chris Angelico

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
 
M

Miki Tebeka

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,
 
M

Miki Tebeka

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,
 
A

Andrea Crotti

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,

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..
 
8

88888 Dihedral

Chris Angelicoæ–¼ 2012å¹´9月14日星期五UTC+8上åˆ6時39分25秒寫é“:
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):
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.
 
8

88888 Dihedral

Chris Angelicoæ–¼ 2012å¹´9月14日星期五UTC+8上åˆ6時39分25秒寫é“:
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):
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.
 
A

Alexander Blinne

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)
 
C

Chris Angelico

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
 
A

Alexander Blinne

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
 
C

Chris Angelico

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
 
S

Steven D'Aprano

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.
 
A

Alexander Blinne

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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,260
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top