[Tutor] Problems understanding control flow

Sander Sweers sander.sweers at gmail.com
Fri Jul 31 00:49:39 CEST 2009


2009/7/30 Eduardo Vieira <eduardo.susan at gmail.com>:

With == you are testing if the 2 values are exactly the same. So 'one'
== 'one' will return True but 'one; == 'one two' will return False.

With in you test if the value is part of a another value. So using the
same value as above 'one' in 'one' will return True but 'one' in 'one
two' will also return True as 'one' is part of the second string.

The same logic applies to lists and dicts. 'one' == ['one', 'two']
will return False as the the 2 values are not exactly the same but
'one' in ['one', 'two'] will return True.

> # this does not work:
> for row in sitelist:
>   for line in bvreaderone:

If you are getting unexpected results add in print statements to see
what is the data that you are working with. I would add the below 2
print statements to see what you are now actually comparing. Same
applies to the other working example.
:
    print 'Value row['EMAIL'] is: %s' % row['EMAIL']
    print 'Value of line['BVADDREMAIL'] is: %s' % line['BVADDREMAIL']

>       if row['EMAIL'] == line['BVADDREMAIL']:

Here you are testing if the value of row['EMAIL'] is exactly the same
as line['BVADDREMAIL']: which is not the case so the test returns
False and the below print statement is not executed.

>           print line['NAME'], row['COMPANY']
>
> # but this does work:
> for row in sitelist:
>   for line in bvreaderone:
>       if row['EMAIL'] in line['BVADDREMAIL']:

Here you test if the value row['E_MAIL'] is part of
line['BVADDREMAIL']:which is True.

>           print line['NAME'], row['COMPANY']

Someone did a way better job writing this down some time a go but
can't find the post online. I hope you see now what the difference is
between the 2.

Greets
Sander


More information about the Tutor mailing list