Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bragging about Python

4 views
Skip to first unread message

pinkflo...@gmail.com

unread,
Jun 7, 2007, 8:33:48 AM6/7/07
to
Is there a resource somewhere on the net that can be used to quickly
and effectively show Python's strengths to non-Python programmers?
Small examples that will make them go "Wow, that _is_ neat"?

Steve Howell

unread,
Jun 7, 2007, 8:46:53 AM6/7/07
to pinkflo...@gmail.com, pytho...@python.org

--- "pinkflo...@gmail.com"
<pinkflo...@gmail.com> wrote:

15 small programs here:

http://wiki.python.org/moin/SimplePrograms



____________________________________________________________________________________
Need a vacation? Get great deals
to amazing places on Yahoo! Travel.
http://travel.yahoo.com/

Mathias Panzenboeck

unread,
Jun 7, 2007, 10:21:01 AM6/7/07
to
Steve Howell schrieb:

> --- "pinkflo...@gmail.com"
> <pinkflo...@gmail.com> wrote:
>
>> Is there a resource somewhere on the net that can be
>> used to quickly
>> and effectively show Python's strengths to
>> non-Python programmers?
>> Small examples that will make them go "Wow, that
>> _is_ neat"?
>>
>
> 15 small programs here:
>
> http://wiki.python.org/moin/SimplePrograms
>

IMHO a few python goodies are missing there.
e.g. generator functions:

# generic fibonacci without upper bound:
def fib():
parent_rabbits, baby_rabbits = 1, 1
while True:
yield baby_rabbits
parent_rabbits, baby_rabbits = baby_rabbits, parent_rabbits + baby_rabbits


# only calculate and print the first 100 fibonacci numbers:
from itertools import islice

for baby_rabbits in islice(fib(),100):
print 'This generation has %d rabbits' % baby_rabbits

Cameron Laird

unread,
Jun 7, 2007, 2:19:06 PM6/7/07
to
In article <46681369$0$12126$3b21...@tunews.univie.ac.at>,

Good point.

This example intrigues me. When I saw the first couple of lines,
I expected

def fib():
generation, parent_rabbits, baby_rabbits = 1, 1, 1
while True:
yield generation, baby_rabbits
generation += 1


parent_rabbits, baby_rabbits = \
baby_rabbits, parent_rabbits + baby_rabbits

for pair in fib():
if pair[0] > 100:
break
print "Generation %d has %d (baby) rabbits." % pair

as more appealing to non-Pythoneers. I'm still suspicious about
how they're going to react to itertools.islice(). Now, though,
I've begun to question my own sense of style ...

Georg Brandl

unread,
Jun 7, 2007, 3:22:34 PM6/7/07
to pytho...@python.org
Mathias Panzenboeck schrieb:

> Steve Howell schrieb:
>> --- "pinkflo...@gmail.com"
>> <pinkflo...@gmail.com> wrote:
>>
>>> Is there a resource somewhere on the net that can be
>>> used to quickly
>>> and effectively show Python's strengths to
>>> non-Python programmers?
>>> Small examples that will make them go "Wow, that
>>> _is_ neat"?
>>>
>>
>> 15 small programs here:
>>
>> http://wiki.python.org/moin/SimplePrograms
>>
>
> IMHO a few python goodies are missing there.

"It's a Wiki." ;)

Georg

--
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.

Szabolcs Nagy

unread,
Jun 7, 2007, 6:32:22 PM6/7/07
to

actually i don't like when a tutorial uses over complicated cute names
if the context is obvious (fibonacci) then we don't need to add
'parent_rabbits' and such identifiers
eg i find more readable and clear the following:

def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a + b

for (n, fn) in enumerate(fib()):
if n > 100:
break
print "F[%d] = %d" % (n, fn)

ymmv

Steve Howell

unread,
Jun 7, 2007, 7:02:26 PM6/7/07
to Szabolcs Nagy, pytho...@python.org

--- Szabolcs Nagy <nsza...@gmail.com> wrote:
>
> actually i don't like when a tutorial uses over
> complicated cute names
> if the context is obvious (fibonacci) then we don't
> need to add
> 'parent_rabbits' and such identifiers

I still prefer the use of "rabbits," but I don't mind
if people change that, as I understand the brevity argument.



____________________________________________________________________________________
Pinpoint customers who are looking for what you sell.
http://searchmarketing.yahoo.com/

Steve Howell

unread,
Jun 7, 2007, 7:15:33 PM6/7/07
to Cameron Laird, pytho...@python.org
Programs like this were posted on this thread:

> def fib():
> generation, parent_rabbits, baby_rabbits = 1,
> 1, 1
> while True:
> yield generation, baby_rabbits
> generation += 1
> parent_rabbits, baby_rabbits = \
> baby_rabbits, parent_rabbits +
> baby_rabbits
>
> for pair in fib():
> if pair[0] > 100:
> break
> print "Generation %d has %d (baby) rabbits."
> % pair
>

One goal behind the SimplePrograms page is to give
people that are new to Python a *gentle* immersion
into Python code. I prefer simple:

parent_rabbits, baby_rabbits = (1, 1)
while baby_rabbits < 100:


print 'This generation has %d rabbits' %
baby_rabbits

parent_rabbits, baby_rabbits = (baby_rabbits,
parent_rabbits + baby_rabbits)

Somebody commented in another reply that they'd prefer
the variable names "a" and "b," but other than that, I
think it's hard to simplify this.

The problem of counting rabbits is not sufficiently
rich to motivate a solution with generator functions,
and "yield" statements are just gonna scare people
away from the Python, unless they've had a chance to
see simpler idioms first.

I do think there's a place on the page for a good
generators example, but it needs to solve a
sufficiently complex problem that the use of
generators actually simplifies the solution.

So I'm throwing down the gauntlet--can somebody write
a short program (maybe 10 to 20 lines) where you solve
a problem more simply than a similar
non-generator-using solution would solve it? Maybe
something like Eight Queens?

-- Steve

P.S. FWIW the page does already include examples of
generator expressions and the itertools module, but it
does not yet show any code that actually implements a
generator. I would greatly welcome the addition of a
good example.


____________________________________________________________________________________
Yahoo! oneSearch: Finally, mobile search
that gives answers, not web links.
http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC

Steve Howell

unread,
Jun 7, 2007, 7:45:55 PM6/7/07
to Georg Brandl, pytho...@python.org
--- Georg Brandl <g.br...@gmx.net> wrote:
> >>
> >> 15 small programs here:
> >>
> >> http://wiki.python.org/moin/SimplePrograms
> >>
> >
> > IMHO a few python goodies are missing there.
>
> "It's a Wiki." ;)
>

Yes indeed. Please feel free to add to the page, or
make your own fork. See link above.



____________________________________________________________________________________
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front

Szabolcs Nagy

unread,
Jun 8, 2007, 2:32:31 AM6/8/07
to

Steve Howell wrote:
> --- Georg Brandl <g.br...@gmx.net> wrote:
> > >>
> > >> 15 small programs here:
> > >>
> > >> http://wiki.python.org/moin/SimplePrograms
> > >>
> > >
> > > IMHO a few python goodies are missing there.
> >
> > "It's a Wiki." ;)
> >
>
> Yes indeed. Please feel free to add to the page, or
> make your own fork. See link above.

the title of a program shouldn't be part of the code (or it should at
least start with #)
sorry i'm too lazy now to fix it, but imho it needs nicer layout to be
used as 'bragging about python' site

btw nice idea
a good example worth a 100 pages of documentation

Steve Howell

unread,
Jun 8, 2007, 4:47:39 AM6/8/07
to Szabolcs Nagy, pytho...@python.org

--- Szabolcs Nagy <nsza...@gmail.com> wrote:
>
> the title of a program shouldn't be part of the code
> (or it should at
> least start with #)
> sorry i'm too lazy now to fix it, but imho it needs
> nicer layout to be
> used as 'bragging about python' site
>

Agreed. I just spruced up the layout a tiny bit, but
it could still use improvement.

http://wiki.python.org/moin/SimplePrograms


> btw nice idea
> a good example worth a 100 pages of documentation
>

Thanks, I agree. :)


____________________________________________________________________________________
Shape Yahoo! in your own image. Join our Network Research Panel today! http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7


Paul McGuire

unread,
Jun 8, 2007, 9:09:15 AM6/8/07
to
> ___________________________________________________________________________­_________

> Yahoo! oneSearch: Finally, mobile search
> that gives answers, not web links.http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC- Hide quoted text -
>
> - Show quoted text -

Well, I misread your "gauntlet throwing", and implemented a brief 8-
queens using recursion and exceptions. Perhaps this could serve as
the "before" to an "after" using generators. (I note that neither
recursion nor exceptions are covered in your examples.) Also, in
googling about for other 8-queens solutions in Python, most are quite
complicated - this one uses only 24 lines, and works for boards of any
size. I also think this line for printing out the board:

print "\n".join( "."*q+"Q"+"."*(BOARD_SIZE-q-1) for q in queens )

illustrates a couple of interesting idioms of Python (joining a list,
generator expression, "."*repetition to give "....").

Worthy of your page?

-- Paul


BOARD_SIZE = 8
def validate(queens):
left = right = col = queens[-1]
for r in reversed(queens[:-1]):
left,right = left-1,right+1
if r in (left, col, right):
raise Exception

def add_queen(queens):
for i in range(BOARD_SIZE):
testQueens = queens+[i]
try:
validate(testQueens)
if len(testQueens)==BOARD_SIZE:
return testQueens
else:
return add_queen(testQueens)
except:
pass
raise Exception

queens = add_queen([])
print queens
print "\n".join( "."*q+"Q"+"."*(BOARD_SIZE-q-1) for q in queens )


Steve Howell

unread,
Jun 8, 2007, 5:54:17 PM6/8/07
to Paul McGuire, pytho...@python.org

--- Paul McGuire <pt...@austin.rr.com> wrote:
> >
> > So I'm throwing down the gauntlet--can somebody
> write
> > a short program (maybe 10 to 20 lines) where you
> solve
> > a problem more simply than a similar
> > non-generator-using solution would solve it?
> Maybe
> > something like Eight Queens?
> >
>
> Well, I misread your "gauntlet throwing", and
> implemented a brief 8-
> queens using recursion and exceptions. Perhaps this
> could serve as
> the "before" to an "after" using generators. (I note
> that neither
> recursion nor exceptions are covered in your
> examples.)

Actually, there was a minor example with exceptions,
but agreed re:recursion, and this is just such a
classic problem, it belongs on the page.

> Also, in
> googling about for other 8-queens solutions in
> Python, most are quite
> complicated - this one uses only 24 lines, and works
> for boards of any
> size. I also think this line for printing out the
> board:
>
> print "\n".join( "."*q+"Q"+"."*(BOARD_SIZE-q-1) for
> q in queens )
>
> illustrates a couple of interesting idioms of Python
> (joining a list,
> generator expression, "."*repetition to give
> "....").

Agreed.

>
> Worthy of your page?
>

Definitely worthy. Go ahead and add it.

FWIW I have been labelling programs by the number of
lines of code. So this is program #24, which now
creates a gap between #16 and #23.

Also, I humbly suggest test_queens instead of
testQueens. I don't want to reopen the PEP 8 debate,
just a recommendation. I think PEP 8 would also
suggest some white space around '+' and '=='.

>
> BOARD_SIZE = 8
> def validate(queens):
> left = right = col = queens[-1]
> for r in reversed(queens[:-1]):
> left,right = left-1,right+1
> if r in (left, col, right):
> raise Exception
>
> def add_queen(queens):
> for i in range(BOARD_SIZE):
> testQueens = queens+[i]
> try:
> validate(testQueens)
> if len(testQueens)==BOARD_SIZE:
> return testQueens
> else:
> return add_queen(testQueens)
> except:
> pass
> raise Exception
>
> queens = add_queen([])
> print queens
> print "\n".join( "."*q+"Q"+"."*(BOARD_SIZE-q-1) for
> q in queens )
>

Very Pythonic solution IMHO. Nicely done.



____________________________________________________________________________________
Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
http://farechase.yahoo.com/

Richard Jones

unread,
Jun 8, 2007, 7:50:07 PM6/8/07
to
Steve Howell wrote:
> --- Szabolcs Nagy <nsza...@gmail.com> wrote:
>>
>> actually i don't like when a tutorial uses over
>> complicated cute names
>> if the context is obvious (fibonacci) then we don't
>> need to add
>> 'parent_rabbits' and such identifiers
>
> I still prefer the use of "rabbits," but I don't mind
> if people change that, as I understand the brevity argument.

The rabbit naming scheme makes the code far more accessible to people who
aren't as familiar with fibonacci.

Really, this is about making Python accessible to more people, not the same
people it's currently accessible to :)


Richard

0 new messages