Let exp = [1,2,3,4,5]
If I then execute x in exp
, it will give me False
. But if I execute :
for x in exp:
if x==3:
print('True')
Then execute x in exp
, it returns True
. What's happening here? I didn't assign anything to x. Did I? I am really confused.
**EDIT:**Sorry if I didn't say this before: x
is not defined before.
Answer
Thank you everyone. I understand it now. the elements of exp
is assigned to x
as exp
is iterated over. And x in exp
equals True
in the last line of code because the last element has been assigned to x
.
When you run your first loop, a variable
x
is assigned to each value of the list as you iterate over it.Let's say you start with
x=0
.0
is not in[1,2,3,4,5]
, thereforex in exp = False
.However, when you run the loop, you now have a new
x
. When the loop is complete,x
is initialized to the final element in the list, meaningx=5
.5
is in[1,2,3,4,5]
, thereforex in exp = True
.The syntax is similar, but they mean two different things.
x in exp
is a conditional expression; it searches to see if the value ofx
appears in the listexp
. If it's there, the expression evaluates toTrue
, otherwise,False
. You can also usenot in
.for x in exp
introduces a loop wherex
iterates over the elements ofexp
. The loop body will be called once for each element, and within the body,x
will be set to each element successively.That's how looping works in Python. '
For var in sequence
' will assign tovar
as you iterate through the sequence.If you were to simply print
x
for example:You would see the following:
Perhaps what you were looking for was just the index of the value? If so, there are a couple of things you could do.
To iterate explicitly with the index, use
enumerate(sequence)
as such:Where the first value (
index
in this case) will always be the index of the current iteration.You can also iterate over the length of the object using
range(len(sequence))
which is sometimes useful if you're potentially modifying the list as you iterate. Here you're actually creating a separate list usingrange
, with length equal to thelen
of the list. This will not assign from the list you're targeting -- rather it will assign from the list created byrange()
-- so you'd have to access it using yourfor x in...
as the index of the list.As for your second line and point of confusion - 'x in exp' - you will indeed see that
x
remains assigned after the fact, and so usingx in exp
will returnTrue
because the last assigned value tox
is, in fact, inexp
.for x in ...
inherently assigns values tox
, becausex
is the loop variable in afor
loop.Each iteration of the loop assigns a value into
x
; Python doesn't create a new variable scope for loops.is the equivalent of...
Seems like you stumbled about
in
being somewhat overloaded in Python.x in exp
, you are asking "Isx
inexp
?"for x in exp: ...
, you tell Python "For each element inexp
, call itx
and do ..."The latter will assign each of the values in
exp
tox
, one after the other, and execute the body of the loop with that value, so in the first iterationx
is assigned1
, in the second2
, and in the last5
. Also,x
keeps this value after the loop!Thus, before the loop, assuming that the variable
x
is defined but has some other value,x in exp
will returnFalse
, and after the loop, it returnsTrue
, becausex
is still assigned the last value fromexp
.