evaluation of >

G

Gary Wessle

Hi

what does the i > a in this code mean. because the code below is
giving False for all the iteration. isn't suppose to evaluate each
value of i to the whole list? thanks

a = range(8)
i = 0
while i < 11:
print i > a
i = i + 1

False
False
False
False
False
False
False
False
False
False
False


thanks
 
R

Roy Smith

Gary Wessle said:
Hi

what does the i > a in this code mean. because the code below is
giving False for all the iteration. isn't suppose to evaluate each
value of i to the whole list? thanks

a = range(8)
i = 0
while i < 11:
print i > a
i = i + 1

False
False
False
False
False
False
False
False
False
False
False


thanks

I'm not sure what you're expecting to happen, or what you're trying to do,
but comparing an integer to a list is (almost) meaningless.

See http://docs.python.org/ref/comparisons.html, where it says, "objects of
different types always compare unequal, and are ordered consistently but
arbitrarily".
 
J

John Machin

what does the i > a in this code mean. because the code below is
giving False for all the iteration. isn't suppose to evaluate each
value of i to the whole list? thanks

But that's EXACTLY what it's doing; each integer value named i is
notionally being compared to the whole list value named a. However as
the types differ (int vs list), it doesn't even look at the actual
values. Each (rather meaningless) comparison evaluates to False.

Did you read section 5.9 (Comparisons) of the Reference Manual? Deep in
the fine print, it says "objects of different types always compare
unequal, and are ordered consistently but arbitrarily". The answers
might have all been True.
a = range(8)
i = 0
while i < 11:
print i > a

The print statement is your friend. Use it more effectively.
print i, a, i > a
i = i + 1

False
False
[snip]
Perhaps if you tell us what you thought the code should do, and/or what
you are investigating, or trying to achieve ....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148
Top