Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Logic in Python Around and Around

Alperen Guman
Alperen Guman
6,552 Points

Why does an undeclared variable name such as X work with "For X in Y" format? I don't get the logic.

I understand what practically is the case, I'm arguing against this syntax because it seems illogical to me, someone please point out why it's logical and why I'm mistaken.

Let's assume Y = [1, 2, 3]

When we say

For X in Y:

 print(X)

there's no logical reason for this to work, because X is undeclared. This literally reads to me as for every undefined in Y print undefined. The computer should go for every what?? but somehow it understands we're talking about items and magically assigns these items to this undeclared variable. Totally illogical.

On the other hand if we said

Y = [1, 2, 3]

X = 1

For X in Y: print(X)

This would make sense and should logically return 1. Or if Y = [1, 2, 3, 1, 2, 3] it should print 1 twice because of the "For" etc.

I get what is aimed to be done but shouldn't we be specifying a data type and then assigning this data type to a new variable instead of putting in a variable where it shouldn't logically go? Here's what would make more sense to me syntax-wise: imagine a data type "item" just like string or float or integer (I don't know if this already exists)

For item in Y:

print(item)

would specifically work but for variables you would go:

X = item(Y)

For X in Y

print(X)

2 Answers

Steven Parker
Steven Parker
231,269 Points

It sounds like your expectations are based partly on experiences with other languages. I think Python is easier to learn for people who are not already familiar with programming. Otherwise, it takes a bit of "re-learning" to be able to think in "pythonesqe" ways..

It also sounds like you may be mixing the concepts of loops ("for x in y") with tests ("if x in y"). These are common programming concepts and not unique to Python.

When you say "So if y was 1, 2, 3, 4, 1, 2, 3, 4 and x was 1" does not apply to "for x in y" because in this case "x" only comes into existence to iterate through the items in y. In the case of such a loop, it would repeat 8 times because that's how many items are in y. On the other hand, when used as a test ("if x in y"), it would happen only once because a test does not repeat.

So, to summarize the difference between the use of "in" in tests and loops:

  • for x in y (loop) — x is used to hold the value of every item in y, one at a time in a loop
  • if x in y (test) — the current value of x is compared to the items in y, the comparison is either true or false

When the difference between tests and loops begins to seem more logical that what you proposed above, you'll know you've learned to "think like programmer".

What is happening is Y, in your case, is an iterable (think list with different items or strings with iterable characters that make up the string). X is being declared in the statement, so it would not have to be declared at an earlier time because this statement is separating the different items/characters from the iterable, Y, and then giving their value to the variable X. Each iteration of the loop uses the next item in Y. So in your example where Y = [1, 2, 3], the first iteration of the loop would give X the value of 1. The next iteration through the for loop would give X the value of 2 and so on. This is helpful when you need to go through several items IN an iterable and you want to assign it to a variable to be used in the FOR loop. Does that help? I could try to explain it differently if additional info would help.

Alperen Guman
Alperen Guman
6,552 Points

Thank you for the explanation. I understand what is being done I'm actually disagreeing with what is being done and the syntax. For X in Y: should not be evaluated as a declerative statement by python. Because if we look at it, it literally just says for every x in y. So if y was 1, 2, 3, 4, 1, 2, 3, 4 and x was 1 For x in y print(yay!) should print yay!yay! because x is in y twice. For assigning the items in Y to X a more logical syntax would be what I proposed in my post I believe. Wouldn't you agree?

I find it easier to think about X being assigned each item in the iterable. I just wouldn't think about it because there's no way to change it.