Complete beginner, any help appreciated :) - For Loops

M

Mark

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.
 
P

Pedro Henrique G. Souto

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
<[email protected]>
â•”â•â•â•â•â•â•â•â•â•â•â•â•â•â•—
║ ²²²d○_○b²²² ║
â•šâ•â•â•â•â•â•â•â•â•â•â•â•â•â•
 
D

Dave Angel

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.
 
M

Mark

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 :)
 
K

Kyle T. Jones

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top