PEP 284, Integer for-loops
Mats Wichmann
mats at laplaza.org
Thu Mar 7 15:46:38 EST 2002
More information about the Python-list mailing list
Thu Mar 7 15:46:38 EST 2002
- Previous message (by thread): PEP 284, Integer for-loops
- Next message (by thread): PEP 284, Integer for-loops
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Wed, 06 Mar 2002 14:23:02 -0800, David Eppstein <eppstein at ics.uci.edu> wrote: :In article <slrna8d4bj.utc.grey at teleute.dmiyu.org>, : Steve Lamb <grey at despair.dmiyu.org> wrote: : :> Perceived lack. IE, this is a solution looking for a problem. I read it :> and really have to ask.... why? If people have a problem with it they just :> need to flip open their Python book and take a few seconds to look it up. : :The situation where I came across this perceived lack was attempting to :use Python syntax to explain the algorithms in an algorithms class :(useful exercise btw, I think the students gained from being able to see :code that actually worked instead of pseudocode, and Python allows many :algorithms to be expressed clearly without distracting the students with :lots of unnecessary pointer-banging). : :Some algorithms (especially in dynamic programming) are expressed most :naturally in terms of integer loops, the ranges of the loops are often :closed rather than open or sometimes need to go in backwards rather than :forward order. Python's range() syntax makes this awkward and detracts :from the reason I wanted to use Python: a simple syntax that expresses :algorithms cleanly and understandably, that does not force students to :buy Python books (this is very much not a programming class) or look up :unfamiliar syntax, and that also is able to run those algorithms :directly. You need to separate range() from the loop. After all, range is not specifically related to loops, it's just handy when doing for loops. I show this interactively. """Loops let you apply an operation repeatedly to a sequence of values. Conceptually, For each item in sequence, print the square of item To show this using Python, let's first generate the sequence. The range function is a handy helper: Let's use range to make a list of numbers that counts down from ten to one by ones: """ >>> seq = range(10, 0, -1) >>> print seq [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] """ Now let's see how a loop works: """ >>> for item in seq: print item ** 2 100 81 64 49 36 25 16 9 4 1 If you'e going on to teach Python, you'd tell 'em those can of course be combined. Mats Wichmann
- Previous message (by thread): PEP 284, Integer for-loops
- Next message (by thread): PEP 284, Integer for-loops
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list