Iteration index

D-Man dsh8290 at rit.edu
Thu May 31 15:11:42 EDT 2001


On Thu, May 31, 2001 at 08:48:12PM +0200, Marek Augustyn wrote:
| Hello!
| 
| 1. Do I have to use additional variable when I want to know iteration index?
| Example:
| 
| s = ('a', 'b', 'ala', 'a')
| i = 0 # additional variable
| for el in s:
|     print i, el
|     i += 1

Yes, but I think

s = ('a', 'b', 'ala', 'a')
for i in xrange( len( s ) ) :
    print i , s[i]

is easier to read, and you don't have the chance of forgetting to
increment the counter.

| 2. Are there block comments in Python? (like /* */ in C)

Triple-Quoted-Strings technically aren't comments, but as an
expression by themself they behave identically.  The only problem
(with C, C++, and Java comments too) is that you can't nest them.
Python does have 2 string delimiters, however, so you can use """ for
strings and ''' for commented out blocks without trouble (the ''' tqs
can be inside a """ tqs).

-D





More information about the Python-list mailing list