[Python-Dev] questionnaire about zip and for loops

Peter Schneider-Kamp peter@schneider-kamp.de
Thu, 20 Jul 2000 08:53:11 +0000


This is a slightly reduced version of a questionnaire run by Greg Wilson on some CS
grad students.

I'd like to use the readers of comp.lang.python as guinea pigs this time.

PLEASE SEND THE RESULTS TO mailto:peter@schneider-kamp.de - DON'T SEND THEM TO THE LIST

Thanks for your time,
Peter

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

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 example for-loops to an output. Only make one cross per line.
Please consider each example on its own.
(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]]:      o       o       o       o       o       o
    print x+y

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

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

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

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

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

(G)
for x in [10, 20, 30]; y in [1, 2]:             o       o       o       o       o       o
    print x+y


--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter@schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de