#! /usr/bin/python

list1 = ['arr', 'bre', 'grau', 'lower', 'tudo']
for item in list1:
         if item == 'arr' or item == 'grau':
            print list1

Hopefully, my rewording of one of your tests will make it a bit easier to see what is happening.
A for statement such as 'for item in list1' walks the whole list. Therefore, every iteration is tested.
Notice you are doing two tests in one if statement so that each time a true condition happens during the iterations that list will print. You have two  iterations that get a true result so list1 prints twice.

That should clear up the confusion. If not please tel us what is still not clear.

Robert Berman

Eduardo Vieira wrote:
Hello, I'm trying to teach my self programming with python and there
are some basic things that stumps me:
Given this code:
###
list1 = ['arr', 'bre', 'grau', 'lower', 'tudo']
for item in list1:
    if 'arr' in item:
        print list1
###
The output is (as expected):
['arr', 'bre', 'grau', 'lower', 'tudo']

If I change my condition to:
if 'arr' or 'bell' in item:
or to this:
if 'arr' or 'grau' in item:

I get this result:
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']

Why this? I guess I'm not grasping the use of AND and OR

Thanks,

Eduardo
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

  
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to