[Tutor] tuples and mysqldb tables

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Sep 19 01:32:38 CEST 2005



Hi Ed,


Let's look at one of your questions:

> How do I use a loops to iterate through the nested tuple

Let's separate this apart from the SQL stuff for the moment.  Are you
familiar with Python's for loop, and how it can work on sequences?  For
example:

######
>>> authors = ['fowler', 'lau', 'gamma', 'helm', 'johnson', 'vlissides']
>>> for a in authors:
...     print "hello", a
...
hello fowler
hello lau
hello gamma
hello helm
hello johnson
hello vlissides
######


Looping across a sequence works regardless of what the elements are, so
that means we can also do this iteration if the elements themselves are
lists:

######
>>> random_numbers = [[3, 1, 4], [1, 5, 9], [2, 6]]
>>> for row in random_numbers:
...     print row
...
[3, 1, 4]
[1, 5, 9]
[2, 6]
######


Does this help answer your question?


I'm getting a feeling that some of the questions you have actually don't
have anything to do with MySQLdb really; instead, it feels like your
questions are really about basic concepts.

Have you already gone through a tutorial like the ones linked from the
Beginner's Guide here?

    http://wiki.python.org/moin/BeginnersGuide

If not, you may want to go through one of them: it'll should help you in
the long run.  Feel free to ask questions here.


Good luck to you!



More information about the Tutor mailing list