Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16490 > unrolled thread
| Started by | Mark <markpackt@gmail.com> |
|---|---|
| First post | 2011-12-01 02:53 -0800 |
| Last post | 2011-12-01 18:23 -0600 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | Mark <markpackt@gmail.com> |
|---|---|
| Date | 2011-12-01 02:53 -0800 |
| Subject | Complete beginner, any help appreciated :) - For Loops |
| Message-ID | <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> |
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.
[toc] | [next] | [standalone]
| From | Laurent Claessens <moky.math@gmail.com> |
|---|---|
| Date | 2011-12-01 12:14 +0100 |
| Message-ID | <jb7nhp$6os$1@news.univ-fcomte.fr> |
| In reply to | #16490 |
> for x in y > > However, what does "for" and "in" mean in this context? It means basically the same as in Englsish Does the following links answer the question ? http://www.ibiblio.org/g2swap/byteofpython/read/for-loop.html http://dsnra.jpl.nasa.gov/software/Python/diveintopython.pdf (page 58) Have a nice day Laurent
[toc] | [prev] | [next] | [standalone]
| From | "Pedro Henrique G. Souto" <pedro.h.souto@gmail.com> |
|---|---|
| Date | 2011-12-01 09:32 -0200 |
| Message-ID | <mailman.3195.1322739143.27778.python-list@python.org> |
| In reply to | #16490 |
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" Good Luck! Att; Pedro Henrique G. Souto <pedro.h.souto@gmail.com> ╔═════════════╗ ║ ²²²d○_○b²²² ║ ╚═════════════╝
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2011-12-01 06:56 -0500 |
| Message-ID | <mailman.3197.1322740635.27778.python-list@python.org> |
| In reply to | #16490 |
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
[toc] | [prev] | [next] | [standalone]
| From | Mark <markpackt@gmail.com> |
|---|---|
| Date | 2011-12-01 04:15 -0800 |
| Message-ID | <28925560.347.1322741750955.JavaMail.geo-discussion-forums@prij11> |
| In reply to | #16490 |
Thanks a lot for the answers everyone, I really appreciate you getting back to me so quickly. I think that I understand where I am with this now :)
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-12-02 00:36 +1100 |
| Message-ID | <87aa7c4dne.fsf@benfinney.id.au> |
| In reply to | #16490 |
Mark <markpackt@gmail.com> writes: > 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. Welcome! You should work your way through the Python tutorial, from beginning to end <URL:http://docs.python.org/tutorial/>. Actually do each exercise, experiment if you have questions, and then continue. Repeat until done. -- \ “Ours is a world where people don't know what they want and are | `\ willing to go through hell to get it.” —Donald Robert Perry | _o__) Marquis | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | "Kyle T. Jones" <onexpadREMOVE@EVOMERyahoodotyouknow.com> |
|---|---|
| Date | 2011-12-01 18:23 -0600 |
| Message-ID | <jb95po$bha$2@dont-email.me> |
| In reply to | #16490 |
On 12/1/11 4:53 AM, 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. y is some list ["help","beginner","question"] for x in y: if x=="question": print "FOUND" Clear? Cheers.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web