[Tutor] Very basic question about lists

spir denis.spir at free.fr
Mon Dec 22 21:36:40 CET 2008


Le lundi 22 décembre 2008 à 11:33 -0700, Eduardo Vieira a écrit :
> 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

Actually, you are right, I guess. There are here two rather subtile
issues. (In addition to the questions about what you really *mean* with
this piece of code):
-1- What python understands reading "if 'arr' or 'bell' in item:"
-2- What logical value python given to several kinds of objects.

About the first point, notice that you write an 'or' in the middle of a
logical expression:

if 'arr' *or* 'bell' in item

Then, the problem for any reader, be it human or mechanical, is to guess
what you intend to relate with 'or'. On left side, no problem, there is
only 'arr'. On right hand, you probably mean to englobe only 'bell', but
how does python know that? To solve this question, any language defines
an operator precedence (priority) order -- which is nearly the same for
all computer languages and inherits from conventional logic. Below the
table for python:

===================================
Operators and their evaluation order 
        Highest
        Operator
        Comment
  
, [...] {...} `...` 
Tuple, list & dict.
creation; string conv. 
s[i] s[i:j] s.attr
f(...) 
indexing & slicing;
attributes, fct calls 
+x, -x, ~x 
Unary operators 
x**y 
Power 
x*y x/y x%y
mult, division, modulo 
x+y x-y 
addition, substraction 
x<<y   x>>y 
Bit shifting 
x&y 
Bitwise and 
x^y 
Bitwise exclusive or 
x|y 
Bitwise or 
x<y  x<=y  x>y  x>=y
x==y x!=y  x<>y
x is y   x is not y
x in s   x not in s 
Comparison, 
identity, 
membership 
not x 
boolean negation 
x and y 
boolean and 
x or y 
boolean or 
        Lowest 
lambda args: expr 
anonymous function
=================================

As you can see, 'or' is very low, which means that it will be aplied
very late in any logical expression. 'in' comes befores. As you can now
guess, this means that your expression is equal to:

if ('arr') or ('bell' in item)

Lookind at the table, you will understand that, for instance:
if a and b or c	<==> if (a and b) or c
if a or b and c <==> if a or (b and c)
Right?

Now, to the second issue -- which is /according to me/ (others will not
agree) a serious default of python: in addition to logical literals
(True and False) and results of logical operators (and, or, in,...),
python gives a boolean logical value to *all* objects. Which means that:

if ( "todu bêm!" and 3 or file("/home/spir/example.txt",'r') )

is perfectly valid in python. Actually, python considers that anything
that is not "nul" (zero, or empty) has a True logical value. So that
'arr' is True for python. Then your expression above is always True
whatever the right side of the 'or' operator. 

for item in list1:
    if 'arr' or (whatever you like):
        print list1

Your loop will test the condition 5 times, because there are 5 items in
the list. The condition will be True 5 times: the list will print 5
times.
Note that you stepped right from the start on a very tricky trap of
python: most of the language items, and examples and trials you may find
or do will work in a coherent way, meaning the way you expect -- if ever
you have a consistent view of the problem. 

Ecco!

Denis

> Thanks,
> 
> Eduardo
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list