[Tutor] the 'or' attribute

Daniel Ehrenberg littledanehren at yahoo.com
Tue Dec 9 21:36:11 EST 2003


Leung Cris wrote:
> Umm....okay , I get it. So, how can i have a
> variable that stores ' this number, or that number'
> ?

That's not really how it works. Something like 'x or
y' will evaluate to one value, not two. Here's an
example:
>>> 1 or 2
2
1 evaluated to True, so 'or' (you can kinda think of
it as a function) returned 2, the second thing. To
test if, for example, x is either 1 or two, you have
to counterintuitively do:
>>> x == 1 or x == 2
becaise otherwise, Python will just interpret x == 1
or 2 as x == 2. If you still want to make one variable
hold more than one thing, there's a way, but it uses
different syntax. Consider the example below:
>>> 1, 2 #makes a tuple, or an immutable list
(1, 2)
>>> x = 1, 2 #sets x to that tuple
>>> x
(1, 2)
>>> 1 == x #doesn't work because 1 is not the same as
the tuple (1, 2)
False
>>> 1 in x #tests to see if 1 is a member of the tuple
x
True
>>> 1 in 1, 2 #wrong syntax, must use parenthesis
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: iterable argument required
>>> 1 in (1, 2)
True
>>> 3 in x #tests if 3 is a member of x
False
>>> 3 in (1, 2)
False

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/



More information about the Tutor mailing list