Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'fashion.': 0.07; 'python': 0.08; 'host,': 0.09; 'query,': 0.09; 'rows': 0.09; 'storing': 0.09; 'tuple': 0.09; 'columns': 0.16; 'executed,': 0.16; 'iterator': 0.16; 'row': 0.16; 'row.': 0.16; 'tuples,': 0.16; '\xa0for': 0.16; '\xa0print': 0.16; 'cc:addr:python-list': 0.16; 'received:74.125.82.44': 0.16; 'received:mail-ww0-f44.google.com': 0.16; 'this:': 0.16; 'cc:no real name:2**0': 0.20; 'charset:iso-8859-7': 0.21; 'header:In-Reply-To:1': 0.22; 'here?': 0.23; 'cc:2**0': 0.24; 'object,': 0.24; 'structure': 0.26; 'variable': 0.28; 'message-id:@mail.gmail.com': 0.28; 'date:': 0.29; 'cc:addr:python.org': 0.29; 'second': 0.29; 'example': 0.29; 'does': 0.32; 'values': 0.32; 'instead': 0.33; 'named': 0.33; 'probably': 0.34; 'received:74.125.82': 0.35; 'explain': 0.36; 'basics': 0.37; 'query': 0.37; 'variables': 0.37; 'except': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; '2nd': 0.38; 'some': 0.38; 'returned': 0.39; 'represent': 0.39; 'should': 0.39; 'being': 0.39; 'called': 0.40; 'selected': 0.68; 'skip:= 30': 0.68; '1st': 0.70; 'time?': 0.73; 'row,': 0.84; 'agent,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=ykiCMvbKI0A8QYbm+Ru7sf9TFqRmduWPOLLblqoCedk=; b=az/4nkEfiSN2Bo05stBs9e+t8uizKo8B7XxbC0HMySsHSu0Vp1ngBOIMIoST5nUpW5 SiC07ob0l34BSoojQ/JNmUWphzh7BZV/wWsYoKrepEewVTJt5PxB28smpyQBNWiB7c2b E1Kik45gMJq/eABB2/AQKzsNrnTZrOtaFMnXo= MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Mon, 9 Jan 2012 16:42:22 -0700 Subject: Re: Explanation about for To: =?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?= Content-Type: text/plain; charset=ISO-8859-7 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1326152575 news.xs4all.nl 6849 [2001:888:2000:d::a6]:47129 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18720 2012/1/9 =CD=E9=EA=FC=EB=E1=EF=F2 =CA=EF=FD=F1=E1=F2 : > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > dataset =3D cursor.fetchall() > > for row in dataset: > =A0 =A0print ( "" ) > > =A0 =A0for item in row: > =A0 =A0 =A0 =A0print ( " %s " % item ) > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > > and this: > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > dataset =3D cursor.fetchall() > > for host, hits, agent, date in dataset: > =A0 =A0print ( "" ) > > =A0 =A0for item in host, hits, agent, date: > =A0 =A0 =A0 =A0print ( " %s " % item ) > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > > > 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