[Tutor] the 'or' attribute

tpc at csua.berkeley.edu tpc at csua.berkeley.edu
Wed Dec 10 10:26:49 EST 2003


hi Daniel, I was looking at your reply to Cris, and I couldn't figure out
how you got 'or' to return '2' when you typed in '1 or 2'.  The only way
to do that as far as I can see is to type '2 or 1':

<paste>
>>> 1 or 2
1
>>> 1 or 2
1
>>> 2 or 1
2
</paste>

On Tue, 9 Dec 2003, Daniel Ehrenberg wrote:

> 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/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>




More information about the Tutor mailing list