Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18720

Re: Explanation about for

References <e1a94d90-57b4-4f6c-be9a-bef08f978639@h13g2000vbn.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-01-09 16:42 -0700
Subject Re: Explanation about for
Newsgroups comp.lang.python
Message-ID <mailman.4559.1326152575.27778.python-list@python.org> (permalink)

Show all headers | View raw


2012/1/9 Νικόλαος Κούρας <nikos.kouras@gmail.com>:
> ================================
> dataset = cursor.fetchall()
>
> for row in dataset:
>    print ( "<tr>" )
>
>    for item in row:
>        print ( "<td><b><font color=yellow> %s </td>" % item )
> ================================
>
> and this:
>
> ================================
> dataset = cursor.fetchall()
>
> for host, hits, agent, date in dataset:
>    print ( "<tr>" )
>
>    for item in host, hits, agent, date:
>        print ( "<td><b><font color=white> %s </td>" % item )
> ================================
>
>
> Can you please explain me how the for structure works here?

You should probably read through the Python tutorial or a Python book
for the basics of Python for loops.

> a) In the 1st example we have 'for row in dataset' what is the value
> of 'row' at that time? What part of 'dataset' is 'row'?

dataset is an iterable object, which means that Python can ask it for
an iterator and then use that iterator to "loop" through the dataset
in some fashion.  In this case, 'dataset' is a database cursor, and
the values returned by the iterator are the rows that were selected by
the query that executed, represented as tuples.  'row' takes on the
value of each of those tuples, one at a time.

> b) In the 2nd example we have for 'host, hits, agent, date in
> dataset'. How does these 4 variables take their values out of dataset?
> How dataset is being splitted?

The second example works the same way as the first, except that
instead of storing each row tuple in a single variable called row, it
unpacks each tuple into four different variables named 'host', 'hits',
'agent', and 'date'.  These represent the values of the selected
columns from the query, for each selected row.

HTH,
Ian

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-09 15:23 -0800
  Re: Explanation about for Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-09 16:42 -0700
  Re: Explanation about for Chris Rebert <clp2@rebertia.com> - 2012-01-09 15:58 -0800
  Re: Explanation about for Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-09 18:11 -0700
    Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-10 01:02 -0800
      Re: Explanation about for Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-01-10 11:57 +0100
        Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-10 03:38 -0800
          Re: Explanation about for Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-01-10 14:52 +0200
        Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-10 03:37 -0800
          Re: Explanation about for "Frank Millman" <frank@chagford.com> - 2012-01-10 15:39 +0200
          Re: Explanation about for Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-01-10 16:22 +0100
          Re: Explanation about for RainyDay <andrei.avk@gmail.com> - 2012-01-11 14:39 -0800
      Re: Explanation about for  Nick Dokos <nicholas.dokos@hp.com> - 2012-01-10 10:24 -0500
        Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-11 14:50 -0800

csiph-web