I see I have to do a loop inside a loop and that this the right expression
if word == 'ar' or word == 'ko':
but this is not:
if word == 'ar' or 'ko':
In the last example: as the 'or' operator has the least priority, it
will be applied last. Which means that all other operations in the
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',
#! /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
eduardo,
welcome to programming, and even better, welcome to Python! you've
done your research and found a list of great people who can help you
out.
with regards to your question, my comment are below...
list1 = ['arr', 'bre', 'grau', 'lower', 'tudo']
for item in list1:
if 'arr' in
On Mon, Dec 22, 2008 at 1:33 PM, Eduardo Vieira eduardo.su...@gmail.com wrote:
if 'arr' or 'bell' in item:
The interpreter sees this as
if ('arr') or ('bell' in item):
'arr' always evaluates to True so the condition is always true. The
correct way to express this condition is
if 'arr' in
if 'arr' or 'bell' in item:
The interpreter sees this as
if ('arr') or ('bell' in item):
'arr' always evaluates to True so the condition is always true. The
correct way to express this condition is
if 'arr' in item or 'bell' in item:
arrgh. yes, i saw this too but forgot to mention it
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:
list1 = ['ar', 'fir', 'wo']
list2 = ['ber', 'gar', 'gt']
list3 = ['hu', 'mo', 'ko', 'tr']
list4 = ['q', 'wer', 'duh']
whole = [list1, list2, list3, list4]
for item in whole:
if 'ar' or 'ko' in item:
print item
So, the unexpected result was that I got all lists printed, when I
if 'ar' or 'ko' in item:
This is incorrect. What you meant to say was:
if 'ar' in item or 'ko' in item:
or something equivalent to that.
if 'ar' or 'ko' in item means if ('ar') is True or ('ko' in item) is
True. Since 'ar' is True anyway, you'll get a match every time.