FAQ

2009/11/12 Tarek Ziad? <ziade.tarek at gmail.com>:
On Thu, Nov 12, 2009 at 3:39 PM, Ray Allen wrote:
[..]
# Loop through list
count = 1
print "Python is ... "
for each in python:
??? print count,each
??? count += 1
I'd replace this bloc by:

# Loop through list
print "Python is ... "
for index, each in enumerate(python):
? ?print index + 1, each

(using enumerate is much more pythonic than using a count variable)
I would avoid the counter.

I learned to iterate a list by using a counter as an index into the
list. When I was shown that I didn't need the counter, I felt uneasy
- perhaps even in denial that this was a good thing - seemed like a
short cut. Weeks or months later it hit me: The reason I needed it
in those other languages is because they didn't have this 'feature',
so the counter/index/item was a work around.

A few months ago someone getting started with python was asking me
~"why is the counter/index/item pattern so cumbersome?" A:"Because
you normally don't need the counter." I could see in his eyes the
exact same disbelief I had felt.

I think we should to focus on what makes python different.

--
Carl K

Search Discussions

  • Ray Allen at Nov 12, 2009 at 4:33 pm
    Thanks for the comments. Taking them onboard, we'd end up with the
    following..

    """Example Python Program"""

    import random

    # Create list
    python = ["Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented"]

    # Append list
    python.append("Open source and cross platform")

    # Shuffle list
    random.shuffle(python)

    # Loop list
    print "Python is ... "
    for index, each in enumerate(python):
    print index + 1, each

    print "Discover more at http://www.python.org"

    It's fewer lines than many 'helloworld!' examples in other languages, but it
    demonstrates a few good features without confusing anybody.

    2009/11/12 Carl Karsten <carl at personnelware.com>
    2009/11/12 Tarek Ziad? <ziade.tarek at gmail.com>:
    On Thu, Nov 12, 2009 at 3:39 PM, Ray Allen wrote:
    [..]
    # Loop through list
    count = 1
    print "Python is ... "
    for each in python:
    print count,each
    count += 1
    I'd replace this bloc by:

    # Loop through list
    print "Python is ... "
    for index, each in enumerate(python):
    print index + 1, each

    (using enumerate is much more pythonic than using a count variable)
    I would avoid the counter.

    I learned to iterate a list by using a counter as an index into the
    list. When I was shown that I didn't need the counter, I felt uneasy
    - perhaps even in denial that this was a good thing - seemed like a
    short cut. Weeks or months later it hit me: The reason I needed it
    in those other languages is because they didn't have this 'feature',
    so the counter/index/item was a work around.

    A few months ago someone getting started with python was asking me
    ~"why is the counter/index/item pattern so cumbersome?" A:"Because
    you normally don't need the counter." I could see in his eyes the
    exact same disbelief I had felt.

    I think we should to focus on what makes python different.

    --
    Carl K
    _______________________________________________
    Advocacy mailing list
    Advocacy at python.org
    http://mail.python.org/mailman/listinfo/advocacy
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/advocacy/attachments/20091112/005aee61/attachment.htm>
  • Roy Smith at Nov 12, 2009 at 4:46 pm

    # Loop list
    print "Python is ... "
    for index, each in enumerate(python):
    print index + 1, each
    Idiomatic python doesn't loop, it iterates. That's more than just
    nit-picking words. The iterator concept is fundamental. It's not just
    that we have iterators, but that there's a standard iterator protocol so
    all iterators look and feel the same (as opposed to, say, C++, where every
    time I use an iterator I need to look up how the iterator for this
    particular class works, because they're all different).

    What you want to do is demonstrate how iterators work (i.e. how easy they
    are to use). The extra fluff with enumerate and printing the numbers just
    obscures the main point you're trying to make. I suggest:

    # Loop list
    print "Python is ... "
    for attribute in python:
    print attribute
  • Carl Karsten at Nov 12, 2009 at 5:04 pm

    On Thu, Nov 12, 2009 at 10:33 AM, Ray Allen wrote:
    Thanks for the comments.? Taking them onboard, we'd end up with the
    following..

    """Example Python Program"""

    import random

    # Create? list
    python = ["Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented"]

    # Append list
    python.append("Open source and cross platform")

    # Shuffle list
    random.shuffle(python)

    # Loop list
    print "Python is ... "
    for index, each in enumerate(python):
    each is a keyword in other languages (for each foo in bar) so it may
    add confusion here.

    for feature in python:
    print(feature)


    I also think we should leave out the comments. at least these. I
    doubt anyone puts this level of comment in their code. We want to
    keep the snippet short, and I am pretty sure anyone who understands
    the comment will also understand the code.
    ??? print index + 1, each

    print "Discover more at http://www.python.org"

    It's fewer lines than many 'helloworld!' examples in other languages, but it
    demonstrates a few good features without confusing anybody.

    2009/11/12 Carl Karsten <carl at personnelware.com>
    2009/11/12 Tarek Ziad? <ziade.tarek at gmail.com>:
    On Thu, Nov 12, 2009 at 3:39 PM, Ray Allen <rayallen153 at googlemail.com>
    wrote:
    [..]
    # Loop through list
    count = 1
    print "Python is ... "
    for each in python:
    ??? print count,each
    ??? count += 1
    I'd replace this bloc by:

    # Loop through list
    print "Python is ... "
    for index, each in enumerate(python):
    ? ?print index + 1, each

    (using enumerate is much more pythonic than using a count variable)
    I would avoid the counter.

    I learned to iterate a list by using a counter as an index into the
    list. ?When I was shown that I didn't need the counter, I felt uneasy
    - perhaps even in denial that this was a good thing - seemed like a
    short cut. ? Weeks or months later it hit me: The reason I needed it
    in those other languages is because they didn't have this 'feature',
    so the counter/index/item was a work around.

    A few months ago someone getting started with python was asking me
    ~"why is the counter/index/item pattern so cumbersome?" ?A:"Because
    you normally don't need the counter." ?I could see in his eyes the
    exact same disbelief I had felt.

    I think we should to focus on what makes python different.

    --
    Carl K
    _______________________________________________
    Advocacy mailing list
    Advocacy at python.org
    http://mail.python.org/mailman/listinfo/advocacy


    --
    Carl K
  • Aahz at Nov 12, 2009 at 5:20 pm

    On Thu, Nov 12, 2009, Ray Allen wrote:
    # Create list
    python = ["Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented"]
    AFAIK, almost everyone indents multi-line expressions. There are several
    ways to do it, this is my preferred version:

    python = [
    "Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented",
    ]

    The reason I prefer this is because it makes it easy to edit the list,
    specifically including the trailing comma on the last element. (E.g.
    it's much easier to delete the first list element than when you put it
    next to the opening bracket -- just delete the line.) It's also easy to
    read because the beginning and end of the list are highly visible. I
    prefer to indent only one level rather than matching up the beginning and
    ending brackets because that gives more line length to the list contents.
    --
    Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/

    [on old computer technologies and programmers] "Fancy tail fins on a
    brand new '59 Cadillac didn't mean throwing out a whole generation of
    mechanics who started with model As." --Andrew Dalke
  • Tennessee Leeuwenburg at Nov 12, 2009 at 11:03 pm
    Just another option...

    print "Python is" + ', '.join([index + ', ' + each for each in python])
    On Fri, Nov 13, 2009 at 4:20 AM, Aahz wrote:
    On Thu, Nov 12, 2009, Ray Allen wrote:

    # Create list
    python = ["Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented"]
    AFAIK, almost everyone indents multi-line expressions. There are several
    ways to do it, this is my preferred version:

    python = [
    "Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented",
    ]

    The reason I prefer this is because it makes it easy to edit the list,
    specifically including the trailing comma on the last element. (E.g.
    it's much easier to delete the first list element than when you put it
    next to the opening bracket -- just delete the line.) It's also easy to
    read because the beginning and end of the list are highly visible. I
    prefer to indent only one level rather than matching up the beginning and
    ending brackets because that gives more line length to the list contents.
    --
    Aahz (aahz at pythoncraft.com) <*>
    http://www.pythoncraft.com/

    [on old computer technologies and programmers] "Fancy tail fins on a
    brand new '59 Cadillac didn't mean throwing out a whole generation of
    mechanics who started with model As." --Andrew Dalke
    _______________________________________________
    Advocacy mailing list
    Advocacy at python.org
    http://mail.python.org/mailman/listinfo/advocacy


    --
    --------------------------------------------------
    Tennessee Leeuwenburg
    http://myownhat.blogspot.com/
    "Don't believe everything you think"
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/advocacy/attachments/20091113/58f148c6/attachment.htm>
  • Roy Smith at Nov 13, 2009 at 1:33 am

    On Nov 12, 2009, at 6:03 PM, Tennessee Leeuwenburg wrote:

    Just another option...

    print "Python is" + ', '.join([index + ', ' + each for each in
    python])
    Our job here is that of pitchman, huckster, pimp even. We have very
    little time to capture somebody's interest. What we are talking about
    is a tease. Quick, obvious, to the point. If our audience has to
    stop and think to understand what they're reading, we've already lost.

    In other words, KISS.


    On Fri, Nov 13, 2009 at 4:20 AM, Aahz wrote:
    On Thu, Nov 12, 2009, Ray Allen wrote:

    # Create list
    python = ["Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented"]
    AFAIK, almost everyone indents multi-line expressions. There are
    several
    ways to do it, this is my preferred version:

    python = [
    "Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented",
    ]

    The reason I prefer this is because it makes it easy to edit the list,
    specifically including the trailing comma on the last element. (E.g.
    it's much easier to delete the first list element than when you put it
    next to the opening bracket -- just delete the line.) It's also
    easy to
    read because the beginning and end of the list are highly visible. I
    prefer to indent only one level rather than matching up the
    beginning and
    ending brackets because that gives more line length to the list
    contents.
    --
    Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/

    [on old computer technologies and programmers] "Fancy tail fins on a
    brand new '59 Cadillac didn't mean throwing out a whole generation of
    mechanics who started with model As." --Andrew Dalke
    _______________________________________________
    Advocacy mailing list
    Advocacy at python.org
    http://mail.python.org/mailman/listinfo/advocacy



    --
    --------------------------------------------------
    Tennessee Leeuwenburg
    http://myownhat.blogspot.com/
    "Don't believe everything you think"
    _______________________________________________
    Advocacy mailing list
    Advocacy at python.org
    http://mail.python.org/mailman/listinfo/advocacy
    ----------------
    Roy Smith
    roy at panix.com




    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/advocacy/attachments/20091112/09a31d7f/attachment-0001.htm>
  • Ray Allen at Nov 13, 2009 at 10:59 am

    Our job here is that of pitchman, huckster, pimp even. We have very little
    time to capture somebody's interest. What we are talking about is a tease.
    Quick, obvious, to the point. If our audience has to stop and think to
    understand what they're reading, we've already lost.

    In other words, KISS.
    Agreed. I've taken out the counter and put in a link to SimplePrograms.

    """Example Python Program"""
    import random
    python = [
    "Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented",
    "Open source and cross platform",
    ]
    python.append("Widely adopted")
    random.shuffle(python)
    print "Python is ... "
    for feature in python:
    print feature
    print "See more examples of Python code at \
    http://wiki.python.org/moin/SimplePrograms"
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/advocacy/attachments/20091113/faf2ac31/attachment.htm>
  • Jason Baker at Nov 13, 2009 at 2:55 pm

    On Fri, Nov 13, 2009 at 4:59 AM, Ray Allen wrote:
    """Example Python Program"""
    import random
    python = [
    ??????? "Easy to read",
    ??????? "Fast to code",
    ??????? "Quick to learn",
    ??????? "Modular and object oriented",
    ??????? "Open source and cross platform",
    ??????? ]
    python.append("Widely adopted")
    random.shuffle(python)
    print "Python is ... "
    for feature in python:
    ??? print feature
    print "See more examples of Python code at \
    http://wiki.python.org/moin/SimplePrograms"
    I like this one. However, I think it's still just a little on the
    complex side for a front-page example. What about something more like
    this:

    python = [
    "Easy to read",
    "Fast to code",
    "Quick to learn",
    "Modular and object oriented",
    "Open source and cross platform",
    ]
    print "Python is ... "

    for feature in python:
    print feature

    print "See more examples of Python code at \
    http://wiki.python.org/moin/SimplePrograms"

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupadvocacy @
categoriespython
postedNov 12, '09 at 4:00p
activeNov 13, '09 at 2:55p
posts9
users6
websitepython.org

People

Translate

site design / logo © 2018 Grokbase