Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!news-transit.tcx.org.uk!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'beginner': 0.04; 'function,': 0.07; 'python': 0.08; 'subject:beginner': 0.09; 'syntax.': 0.09; 'am,': 0.12; '"**"': 0.16; '"for"': 0.16; '"in"': 0.16; '"r")': 0.16; '#this': 0.16; '(1,': 0.16; '(then': 0.16; '2007)': 0.16; 'caveat': 0.16; 'differently,': 0.16; 'general.': 0.16; 'iterable,': 0.16; 'means:': 0.16; 'received:192.168.1.104': 0.16; 'sequence.': 0.16; 'val': 0.16; 'cc:addr:python-list': 0.16; 'syntax': 0.16; 'examples': 0.16; 'wrote:': 0.18; 'string,': 0.18; 'this?': 0.19; 'memory': 0.21; 'subject:help': 0.21; '(or': 0.22; "doesn't": 0.22; 'assume': 0.22; 'header:In-Reply-To:1': 0.22; 'loop,': 0.23; 'produces': 0.23; 'hopefully': 0.24; 'code': 0.25; '(in': 0.26; "i'm": 0.26; 'cc:addr:gmail.com': 0.28; 'coding': 0.28; 'bit': 0.28; 'elements': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'css,': 0.30; 'lot.': 0.30; '(the': 0.30; 'thanks': 0.31; 'anyone': 0.31; "i've": 0.31; 'does': 0.32; 'values': 0.32; 'wondering': 0.32; 'list': 0.32; 'header:User- Agent:1': 0.33; 'actually': 0.33; 'named': 0.33; 'it.': 0.34; 'character': 0.34; 'question,': 0.34; 'things': 0.34; 'list.': 0.35; 'something': 0.35; 'however,': 0.36; 'equal': 0.36; 'cc:2**1': 0.36; 'example,': 0.37; 'element': 0.37; 'but': 0.37; 'run': 0.37; 'list,': 0.37; 'received:192': 0.37; 'think': 0.37; 'spent': 0.38; 'some': 0.38; 'aside': 0.39; 'characters': 0.39; 'define': 0.39; 'mark': 0.39; 'help': 0.39; "it's": 0.40; 'received:192.168': 0.40; 'hours': 0.60; 'range': 0.61; 'your': 0.61; 'free': 0.64; 'subject:For': 0.67; '"for': 0.67; 'header :Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'print.': 0.84; 'subject:any': 0.84 Date: Thu, 01 Dec 2011 06:56:44 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15 MIME-Version: 1.0 To: "Pedro Henrique G. Souto" Subject: Re: Complete beginner, any help appreciated :) - For Loops References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> <4ED765BB.7050305@gmail.com> In-Reply-To: <4ED765BB.7050305@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:245ruPUgdmZXONEH0NaeQ4i3f05d0T0fUmzDiNH3aIg 6UTdhPZiOSm0UG9MlQHF4iSWNDt2QGQzU49oqS/0OxhN64qTj+ apm/IRvDeB0KYJ3I2mu3b90V1BHSFmPFacogoI3KReFPbKDaQL fV8FCswGbcQ5Ddw117Z7/Q6TUsXWCDyH833Q+Yn9mHK9Zxnc6Q 4Ox1B7w2KaG9Lw2YyJaat6pJpLDEcH3CorTCT2CIP71NSHjrFx KBuOpPr+og1ywFplnKnkNT8CYBYu6KY2iKmAn4AzwcCvEpKyaY jJsi1Yuf7wSAaD++9Clhq0iUBDo2MbFpw0UVq+O2+r6JoklFQ= = Cc: python-list@python.org, markpackt@gmail.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1322740635 news.xs4all.nl 6925 [2001:888:2000:d::a6]:32798 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16494 On 12/01/2011 06:32 AM, Pedro Henrique G. Souto wrote: > > On 01/12/2011 08:53, Mark wrote: >> Hi there, >> >> I'm a complete beginner to Python and, aside from HTML and CSS, to >> coding in general. I've spent a few hours on it and think I >> understand most of the syntax. >> >> However, I'm wondering a bit about For Loops. I know that the basic >> syntax for them is to define a list, and then to use something like: >> >> for x in y >> >> However, what does "for" and "in" mean in this context? Can anyone >> help me to understand this? I know it's a really basic question, but >> hopefully it will see me on my way to coding properly :) >> >> Thanks a lot. > > That means (in a free translation) > > "For each one of 'x' in 'y', do this" > > 'y' is a list, for example, then it means: "For each one of the > elements of the list 'y' (the element on the current iteration is > named 'x'), do this" > > And if y is a string, it means for each character in y, do this x = the character (then do the indented part) And if y is an arbitrary iterable, it means for each thing the iterable produces x = the thing (then do the indented part) Each time you go through the loop, x will be equal to the next item in the sequence. So you can systematically process the items in the list (or characters in the string, or values from an iterable), one at a time. The only real caveat is to not do something that would change the list (etc.) while you're looping through it. What are some other examples of iterables? infile = open("myfile.txt", "r") #infile is an iterable for line in infile: print "**", line, "**" for val in (1, 4, 9, 2007) for x in xrange(50000000): #this does the same as range(), but doesn't actually build the list. #this way, we don't run out of memory You can also code your own generator function, which is an iterable, and may be used like the above. Some things in this message assume Python 2.x. in Python 3, range works differently, as does print. -- DaveA