[Python-Dev] multi-loop quiz results

Greg Wilson gvwilson@nevex.com
Thu, 13 Jul 2000 15:17:41 -0400 (EDT)


I gave 20+ grad students in computer engineering and computer science
(plus a couple of profs) the questionnaire below.  A summary of
results is:

* Everyone understood the existing syntax (A).

* Most assumed that that the 'zip()' function version would yield
  cross-product iteration (B).  This surprised me a lot, particularly
  as I think we all agree that 'zip()' is the most suggestive possible
  name for this function.

* Versions (D), (F), and (H) show that semi-colon separated targets
  imply cross-product iteration, but 'and' and 'while' imply
  simultaneous iteration.

* Version (G) shows that using 'and' suggests that the sequences being
  iterated over must have the same length.

The questionnaire and a table of responses are included below (100
characters wide).

Greg

----------------------------------------------------------------------

The single and double loop below print the output shown:

for x in [10, 20, 30]:           for x in [10, 20, 30]:
    print x                          for y in [1, 2, 3]:
                                         print x+y
10                              11
20                              12
30                              13
                                21
                                22
                                23
                                31
                                32
                                33

Match each of the following to its output.  (Note that several
examples may print the same thing.)


                                                (1)     (2)     (3)     (4)     (5)     (6)

                                                11      11      11      11      error   ??
                                                22      12      22      12
                                                33      13              21
                                                        21              22
                                                        22              31
                                                        23              32
                                                        31
                                                        32
                                                        33

(A)
for (x, y) in [[10, 1], [20, 2], [30, 3]]:      11      0       0       0       0       0
    print x+y

(B)
for [x, y] in zip([10, 20, 30], [1, 2, 3]):     2       6       0       0       0       3
    print x+y

(C)
for [x, y] in zip([10, 20, 30], [1, 2]):        0       0       1       4       4       2
    print x+y

(D)
for x in [10, 20, 30]; y in [1, 2, 3]:          3       7       0       0       1       0
    print x+y

(E)
for x in [10, 20, 30]; y in [1, 2]:             1       0       2       6       2       0
    print x+y

(F)
for (x in [10, 20, 30]) and (y in [1, 2, 3]):   7       4       0       0       0       0
    print x+y

(G)
for (x in [10, 20, 30]) and (y in [1, 2]):      0       0       1       2       6       2
    print x+y

(H)
for x in [10, 20, 30] while y in [1, 2, 3]:     2       7       0       0       1       1
    print x+y

(I)
for x in [10, 20, 30] while y in [1, 2]:        0       1       0       7       2       1
    print x+y

(J)
for x; y in [10, 20, 30]; [1, 2, 3]:            2       3       0       1       3       2
    print x+y

(K)
for x; y in [10, 20, 30]; [1, 2]:               0       1       0       3       5       2
    print x+y