Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16494
| 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 | <d@davea.name> |
| 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 <d@davea.name> |
| 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" <pedro.h.souto@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3197.1322740635.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Complete beginner, any help appreciated :) - For Loops Mark <markpackt@gmail.com> - 2011-12-01 02:53 -0800 Re: Complete beginner, any help appreciated :) - For Loops Laurent Claessens <moky.math@gmail.com> - 2011-12-01 12:14 +0100 Re: Complete beginner, any help appreciated :) - For Loops "Pedro Henrique G. Souto" <pedro.h.souto@gmail.com> - 2011-12-01 09:32 -0200 Re: Complete beginner, any help appreciated :) - For Loops Dave Angel <d@davea.name> - 2011-12-01 06:56 -0500 Re: Complete beginner, any help appreciated :) - For Loops Mark <markpackt@gmail.com> - 2011-12-01 04:15 -0800 Re: Complete beginner, any help appreciated :) - For Loops Ben Finney <ben+python@benfinney.id.au> - 2011-12-02 00:36 +1100 Re: Complete beginner, any help appreciated :) - For Loops "Kyle T. Jones" <onexpadREMOVE@EVOMERyahoodotyouknow.com> - 2011-12-01 18:23 -0600
csiph-web