Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-12 Thread Alan Gauld


"Steven D'Aprano"  wrote

Spiced tea with milk. Well, technically, it just means "tea with 
milk", but in English chai is used exclusively for spiced tea


Never heard of it I confess.

I've heard the,  presumably related, term char, meaning a cup of black
tea (as in tea without milk, not black leaves!). And when I've been
in India I've heard tea called chai, but again it wasn't spiced,
just plain old tea without milk. But I've never heard of chai being
used in the UK, certainly not in Scotland!.

"Latte" is short for the Italian "caffè latte", or literally "coffee 
with milk". The latte part means "with milk", not coffee.


And I'm familiar with coffee latte, but like your waiter I'd never
heard of chai and latte being used together. So I too might have
brought you coffee and tea mixed! :-)

PS.
I tasted the Nestle's chai when I was in Australia and your
description accords with my findings! :-(



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-12 Thread Steven D'Aprano

Alan Gauld wrote:


"Steven D'Aprano"  wrote

ordered a chai latte at a cafe. The waiter had no idea what that was, 
but must have known that "chai" means tea, and so mixed tea and coffee


So now I've got to ask, what is a chai latte?
I could Google it but I'm feeling lazy :-)


Spiced tea with milk. Well, technically, it just means "tea with milk", 
but in English chai is used exclusively for spiced tea ("masala chai" in 
Indian) rather than black or green tea.


Oh, except for Nestles, who sell something here in Australia which they 
call chai but is actually flavoured coffee. I think it's flavoured with 
rat droppings and pimple-squeezings, no matter what the packet says, 
because it truly is disgusting.


"Latte" is short for the Italian "caffè latte", or literally "coffee 
with milk". The latte part means "with milk", not coffee.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-12 Thread Alan Gauld


"Steven D'Aprano"  wrote

ordered a chai latte at a cafe. The waiter had no idea what that 
was, but must have known that "chai" means tea, and so mixed tea and 
coffee


So now I've got to ask, what is a chai latte?
I could Google it but I'm feeling lazy :-)

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-11 Thread Steven D'Aprano

Lie Ryan wrote:


The question "Would you like Italian or Chinese for dinner" is actually
a contraction of "Would you like Italian for dinner or would you like
Chinese for dinner". If we ask these two questions separately to the
wife, we get either "Yes or Yes", "Yes or No", "No or Yes", or "No or
No", which evaluates to either "Yes", "Yes", "Yes", and "No" (use "True"
or "False", if you prefer). Or syntactically:


In natural language (at least in English, other languages may have other 
conventions), "or" generally has a meaning closer to exclusive-or (xor) 
than to the logical disjunction (boolean "or"):


"We can go out, or we can stay home."
"Take the money, or the box."
"You must find the defendant guilty or not guilty."
"The cat is either inside the box, or outside the box."
"Your money, or your life."

You can't do both at the same time.

Even when the two alternatives aren't strictly contradictory, it's often 
assumed that only one will hold:


"Would you like tea or coffee?"

It would be surprising if somebody wanted both.

(Particularly if they were served in the same cup -- my wife once 
ordered a chai latte at a cafe. The waiter had no idea what that was, 
but must have known that "chai" means tea, and so mixed tea and coffee 
in the same cup and served it with milk. And yes, the result was as 
horrible as it sounds.)


We often make inclusivity an explicit choice:

"Dinner, or a movie, or both?"


Quoting from Websters Dictionary [1913]:

   A particle that marks an alternative; as, you may read or may
   write, -- that is, you may do one of the things at your
   pleasure, but not both. It corresponds to either. You may
   ride either to London or to Windsor. It often connects a
   series of words or propositions, presenting a choice of
   either; as, he may study law, or medicine, or divinity, or he
   may enter into trade.


Having said that, "or" in natural language is not precisely logical-xor 
either. I can't think of any natural question "would you like A or B?" 
where the answer "No" is appropriate if you would like both. Natural 
language is also far more flexible, and frequently allows choices that 
aren't explicitly enumerated:


Waiter: "Tea or coffee?"
Person A: "Nothing for me."
Person B: "Hot chocolate please."
Steve Martin: "I'll have a tall fair-trade organic half double-decaf 
half caf low-fat soy latte with a twist of lemon."

Logician: "Yes."


The reality is, there's no one-to-one correspondence between natural 
language constructs and boolean algebra.




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-11 Thread Lie Ryan
On 12/11/10 04:12, Alan Gauld wrote:
> "Steven D'Aprano"  wrote
>> As an experiment, offer to buy your wife dinner, and ask if she'd
>> prefer to go to an Italian or Chinese restaurant.
> 
> :-)
> She would either answer "Yes" (she would like to go to one of
> them, and if I'm lucky she might give me a clue which!) or "No"
> (she would prefer neither, Indian perhaps...)

The question "Would you like Italian or Chinese for dinner" is actually
a contraction of "Would you like Italian for dinner or would you like
Chinese for dinner". If we ask these two questions separately to the
wife, we get either "Yes or Yes", "Yes or No", "No or Yes", or "No or
No", which evaluates to either "Yes", "Yes", "Yes", and "No" (use "True"
or "False", if you prefer). Or syntactically:

===
I(i) = Italian (an object)
I(c) = Chinese (an object)
I(W) = Indication of Want (a unary relation)
---
W(i) or W(c)
===

In short circuiting language, that question is translated to: "If you
want Italian for dinner then answer Italian, else answer Chinese". Or
syntactically:

===
I(i) = Italian (an object)
I(c) = Chinese (an object)
I(W) = Indication of Want (a unary relation)
I(E) = Eat at (a unary relation)
---
if W(i) then i else c
--- or ---
if W(i) then Ei
if not W(i) then Ec
===

Neither of the two previous translations corresponds to the intuition we
had in natural language.

Instead, in natural language, the best translation is probably "If you
prefer Italian over Chinese for dinner then answer Italian, else if you
prefer Chinese over Italian for dinner then answer Chinese (assume she
cannot answer Neither or Both). Or syntactically:

===
I(i) = Italian (an object)
I(c) = Chinese (an object)
I(>) = Wife's Order of Preference (a strict weak order binary relation)
I(E) = Eat at (a unary relation)
---
if i > c then Ei
if c > i then Ec
===

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-10 Thread Walter Prins
Hi Steven

On 10 December 2010 03:50, Steven D'Aprano  wrote:

> Some languages (Pascal comes to mind) doesn't have short-circuit behaviour
> at all.
>

Don't mean to nit pick, but in my experience it really depends on the
compiler implementation and which version of Pascal you're talking about.
Certainly, Borland's Turbo Pascal had and later Object Pascal (today called
Delphi) has to this day short-circuit evaluation as default behaviour,
although you can turn this off via a compiler switch if you want.(And as
an aside, according to wikipedia ISO Pascal actually also allows but does
not require support of short-circuit boolean evaluation.)  It really depends
on what your program does -- if your program contains functions with
side-effects (a bad idea, but if it does) then short-circuit evaluation will
probably break your code.  On the other hand, not having short-circuit
boolean expression evaluation can in most programming contexts be needlessly
inefficient.

Anyway, your general point is of course quite correct, so feel free to
ignore my ramblings...

Best,

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-10 Thread Alan Gauld

"Steven D'Aprano"  wrote

Python knows that if val is true then it doesn't need to evaluate 
the second term that causes it to return val rather than 1.


That's what makes it short circuiting, but that's not why it returns 
the first argument. `or` in standard Pascal doesn't short-circuit.


But Pascal returns a boolean result, it doesn't return the original
value of either term, it returns the true Boolean value of the
expression.


Take this example `or.p` file:


I don't see what point you are trying to make?
Other than that Pascal doesn't do short-circuit evaluation.
But that doesn't relate to the fact that its a combination
of Python doing short circuit evaluation plus returning the
original value that enables the "val or 1" idiom to work.

It's a boolean expression you would reasonably expect a true 
boolean result.


That brought a smile to my face! That's very amusing, the idea that 
*boolean algebra* is intuitively expected!


It is if you are used to computer programming languages :-)
So for example if I do

print len("foo")
I don't expect "bar" - even though bar does have
the same length as "foo".

The way most languages nowadays treat logic is inconsistent
with how they treat other operations but done for the many
conveniences that such an approach has over the more purist.
eg
if myString

rather than

if myString == ""

etc.

As an experiment, offer to buy your wife dinner, and ask if she'd 
prefer to go to an Italian or Chinese restaurant.


:-)
She would either answer "Yes" (she would like to go to one of
them, and if I'm lucky she might give me a clue which!) or "No"
(she would prefer neither, Indian perhaps...)

If she says that she doesn't understand the question, because 
restaurants aren't True/False boolean values, then you might have a 
point :)


Yes, I agree that only a truue mathematician would reply like that.

Alan G.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-10 Thread Steven D'Aprano

ALAN GAULD wrote:



Doesn't  short-circuit evaluation refer specifically to the behavior
where arguments  are only evaluated if they need to be? It's a very
useful feature, but not  technically required for the "val = val or 1"
behavior to work.


Its essential.
If Python always evaluated all parts of a boolean expression 
the return value would always be the last item.


You can simulate non-short-circuit behaviour with a function:

def or_(a, b):
if a: return a
return b

x = 0
or_(True, 1/x)

Unlike the short-circuiting `or` operator, this version evaluates the 
1/x even though it doesn't end up being used. This was one of the 
reasons why the ternary `if` operator had to be handled by syntax, 
rather than having people write a function:


ifte(condition, a, b):
if condition: return a
else: return b

ifte(len(x) > 0, x[0], "nothing there")

This will fail if x is an empty list, unlike this:

x[0] if len(x) > 0 else "nothing there"

which works because the Python interpreter knows not to evaluate x[0] 
unless needed.



 It's the fact that
Python knows that if val is true then it doesn't need to evaluate 
the second term that causes it to return val rather than 1.


That's what makes it short circuiting, but that's not why it returns the 
first argument. `or` in standard Pascal doesn't short-circuit. Take this 
example `or.p` file:


[st...@sylar pascal]$ cat or.p
program main(input, output);

function f1(a:integer):boolean;
  begin
writeln('calling f1');
f1 := True;
  end;

function f2(a:integer):boolean;
  begin
writeln('calling f2');
f2 := True;
  end;

var
  n: integer;
  f: boolean;

begin
  n := 0;
  f := f1(n) or f2(n);
end.

[st...@sylar pascal]$
[st...@sylar pascal]$ gpc --no-short-circuit or.p
[st...@sylar pascal]$ ./a.out
calling f1
calling f2


(gpc defaults to the sensible but non-standard short-circuit behavior, 
and you have to pass a compiler option to get the standard behaviour.)




Also,  returning on of its operands rather than a boolean is hardly a
quirk, since  basically all dynamic languages do it ever since perl
made "val = val or 1"  an idiom (at least, I think it was perl).


Its a quirk in that it is not the intuitively expected behaviour.
It's a boolean expression you would reasonably expect a 
true boolean result.


That brought a smile to my face! That's very amusing, the idea that 
*boolean algebra* is intuitively expected!


As an experiment, offer to buy your wife (or girlfriend, boyfriend, 
significant other, or if all else fails, mum) dinner, and ask if she'd 
prefer to go to an Italian or Chinese restaurant. If she says that she 
doesn't understand the question, because restaurants aren't True/False 
boolean values, then you might have a point :)




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-10 Thread ALAN GAULD


> Doesn't  short-circuit evaluation refer specifically to the behavior
> where arguments  are only evaluated if they need to be? It's a very
> useful feature, but not  technically required for the "val = val or 1"
> behavior to work.

Its essential.
If Python always evaluated all parts of a boolean expression 
the return value would always be the last item. It's the fact that 
Python knows that if val is true then it doesn't need to evaluate 
the second term that causes it to return val rather than 1.

> Also,  returning on of its operands rather than a boolean is hardly a
> quirk, since  basically all dynamic languages do it ever since perl
> made "val = val or 1"  an idiom (at least, I think it was perl).

Its a quirk in that it is not the intuitively expected behaviour.
It's a boolean expression you would reasonably expect a 
true boolean result.

I think you are right that Perl was the first popular language to 
do this, but Perl is a relatively recent arrival (abouit the same 
time as Python - 1988-90?) and it has become a feature of 
many recent dynamic languages. But most static languages 
still return true boolean values.

Alan G.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-09 Thread Steven D'Aprano

Hugo Arts wrote:


Doesn't short-circuit evaluation refer specifically to the behavior
where arguments are only evaluated if they need to be? It's a very
useful feature, but not technically required for the "val = val or 1"
behavior to work.


Yes, exactly.

Some languages (Pascal comes to mind) doesn't have short-circuit 
behaviour at all. If I've understood correctly, some languages (Algol, I 
think, but don't quote me) have short-circuiting function parameters, so 
you could do this:


x = 0
function(flag, x/0)

and the argument x/0 would only be evaluated if the function actually 
tried to use it.


Python has short-circuit behaviour for:

x or y
x and y
all(iterable)
any(iterable)
true_value if condition else false_value



Also, returning on of its operands rather than a boolean is hardly a
quirk, since basically all dynamic languages do it ever since perl
made "val = val or 1" an idiom (at least, I think it was perl).


Yes, it's certainly useful and not deprecated. At worst, it's less 
common since the introduction of the ternary if operator, but it's still 
useful. You can do things like this:


extras = []  # Global list of extra names to use.

def func(x, names=None):
# Do something with x and an optional list of names.
names = names or extras or ['my', 'internal', 'list', 'of', 'names']
do_stuff_with(x, names)


This means that the names actually used will be the first of:

- the function argument
- the global extras
- the built-in internal list

which is not empty. So you can override the internal list globally by 
setting extras, and you can override the global list by passing a list 
of names to the function.




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-09 Thread Hugo Arts
On Fri, Dec 10, 2010 at 2:07 AM, Alan Gauld  wrote:
>
> "Alex Hall"  wrote
>>
>> val=val or 1
>
>> I am guessing that val is an int. If val==0, the 'or' kicks in and
>> val=1, else the or is not needed and val=val. Am I close?
>
> Yes this is a combination of what is known as short circuit evaluation of
> boolean expressions and a quirk of Python that returns the actual value of
> something that is being treated as a boolean.
>

Doesn't short-circuit evaluation refer specifically to the behavior
where arguments are only evaluated if they need to be? It's a very
useful feature, but not technically required for the "val = val or 1"
behavior to work.

Also, returning on of its operands rather than a boolean is hardly a
quirk, since basically all dynamic languages do it ever since perl
made "val = val or 1" an idiom (at least, I think it was perl).

There is more to the innocent little or. See
http://docs.python.org/library/stdtypes.html#truth-value-testing
It works on any type, not just numbers, and operates by truth value
testing, which is more complicated than it might first seem.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-09 Thread Alex Hall
Thanks to all for the quick responses. Python always surprises me with
its shortcuts...

On 12/9/10, Alan Gauld  wrote:
>
> "Alex Hall"  wrote
>
>> val=val or 1
>
>> I am guessing that val is an int. If val==0, the 'or' kicks in and
>> val=1, else the or is not needed and val=val. Am I close?
>
> Yes this is a combination of what is known as short circuit
> evaluation of boolean expressions and a quirk of Python that
> returns the actual value of something that is being treated as
> a boolean.
>
> There is a section on this in the Functional Programming
> topic in my tutor which explains and illustrates in much
> more detail.
>
> This particular trick is now deprecated in favour of the new
> conditional expressiion, so your code would now be written as:
>
> val = val if val else 1
>
>> Can other words or symbols be used in contexts where one
>> would not normally think of them?
>
> See my tutor, it shows how and can be used in similar ways...
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-09 Thread Alan Gauld


"Alex Hall"  wrote 


val=val or 1



I am guessing that val is an int. If val==0, the 'or' kicks in and
val=1, else the or is not needed and val=val. Am I close? 


Yes this is a combination of what is known as short circuit 
evaluation of boolean expressions and a quirk of Python that 
returns the actual value of something that is being treated as 
a boolean.


There is a section on this in the Functional Programming 
topic in my tutor which explains and illustrates in much 
more detail.


This particular trick is now deprecated in favour of the new 
conditional expressiion, so your code would now be written as:


val = val if val else 1

Can other words or symbols be used in contexts where one 
would not normally think of them?


See my tutor, it shows how and can be used in similar ways...

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'or' in assignment (not if statement)?

2010-12-09 Thread Adam Bark

On 10/12/10 00:51, Alex Hall wrote:

Hi all,
I am reading the source of a project I hope to help with
(http://www.qwitter-client.net). I sometimes see something like:
val=val or 1
I am guessing that val is an int. If val==0, the 'or' kicks in and
val=1, else the or is not needed and val=val. Am I close? Can other
words or symbols be used in contexts where one would not normally
think of them?
Thanks.

   

Hi Alex,

This is one of those times the interactive interpreter comes in handy eg:

In [1]: val=5

In [2]: val=val or 1

In [3]: val
Out[3]: 5

In [4]: val=0

In [5]: val=val or 1

In [6]: val
Out[6]: 1

You are right by the way and I know you can't test every possibility 
but, as you already suspected the outcome, this just reinforces it I think.


HTH,
Adam.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] 'or' in assignment (not if statement)?

2010-12-09 Thread Alex Hall
Hi all,
I am reading the source of a project I hope to help with
(http://www.qwitter-client.net). I sometimes see something like:
val=val or 1
I am guessing that val is an int. If val==0, the 'or' kicks in and
val=1, else the or is not needed and val=val. Am I close? Can other
words or symbols be used in contexts where one would not normally
think of them?
Thanks.

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] or synxtax in if statement

2007-08-31 Thread Brian Wisti
On 8/31/07, David Bear <[EMAIL PROTECTED]> wrote:
>
> I think I want to be lazy and express this
>
> if a == b | a = c
> (if a equal b or a equals c)
> using
>
> if a == b | c
>
> it seems to work.. but I'm not sure if it is correct -- and I haven't seen
> any documentation on using this type of syntax.



You could put b and c in a tuple or array and check for membership

>>> a, b, c = 1, 0, 1
>>> a in (b, c)
True

Is that lazy enough?

Kind Regards,

Brian Wisti
http://coolnamehere.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] or synxtax in if statement

2007-08-31 Thread Eric Brunson
David Bear wrote:
> I think I want to be lazy and express this
>
> if a == b | a = c
> (if a equal b or a equals c)
> using
>
> if a == b | c
>
> it seems to work.. but I'm not sure if it is correct -- and I haven't seen
> any documentation on using this type of syntax.
>
>
>   
The pipe is the "bitwise or" operator.  I think you're looking for "or".

And... "if a==b or c" won't work.

e.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] or synxtax in if statement

2007-08-31 Thread Smith, Jeff
That definitely won't work.  How could the language possibly determine
if you meant 

a == b | a == c

as opposed to the literal

a == b | c

What this becomes is

a == (b | c)

Also be aware that | is a "bitwise or" and not a logical "or" which may
not be what you want.  So your original expression may not be what you
want since it will get evaluated as

a == (b | a) == c

Consider the following result:

>>> a=0
>>> b=0
>>> c=1
>>> print a == b | a == c
False
>>> print a == (b | a) == c
False
>>> print (a == b) | (a == c)
True
>>> print a == b | c
False

Although what I suspect you really want is

>>> print a == b or a == c
True
>>> print (a == b) or (a == c)
True

But this means that your shortcut becomes

(a == b) or c

So consider

>>> a=0
>>> b=1
>>> c=0
>>> print a == b or c
0

Which is the same as false.

Jeff 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of David Bear
Sent: Friday, August 31, 2007 3:40 PM
To: tutor@python.org
Subject: [Tutor] or synxtax in if statement

I think I want to be lazy and express this

if a == b | a = c
(if a equal b or a equals c)
using

if a == b | c

it seems to work.. but I'm not sure if it is correct -- and I haven't
seen any documentation on using this type of syntax.


--
--
David Bear
College of Public Programs at Arizona State University

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] or synxtax in if statement

2007-08-31 Thread David Bear
I think I want to be lazy and express this

if a == b | a = c
(if a equal b or a equals c)
using

if a == b | c

it seems to work.. but I'm not sure if it is correct -- and I haven't seen
any documentation on using this type of syntax.


-- 
--
David Bear
College of Public Programs at Arizona State University

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] or

2007-03-29 Thread John Fouhy
On 30/03/07, Christopher Spears <[EMAIL PROTECTED]> wrote:
> What I can't remember is what is 'or' in python.  For
> example, what if I want the loop to skip apples and
> pears?  I tried this:
>
> >>> for f in fruit:
> ... if f != "apples" or "pears":
> ... print f
> ... print "This is not an apple or pear"
> ...
> apples
> This is not an apple or pear
> pears
> This is not an apple or pear
> oranges
> This is not an apple or pear
> >>>
>
> Actually maybe my problem is not asking the right
> question?  Should I be looking for 'and' instead of
> 'or' ?

Hi Christopher,

"or" is used to combine two boolean expressions.  What you wrote is
equivalent to:

for f in fruit:
  if (f != "apples") or ("pears"):
 # etc

So python first checks the truth of (f != "apples").  If this is true,
it continues to the body of the if statement.  If this is false,
python checks the truth of ("pears").  Because "pears" is a nonempty
string, it is always true, and so python continues to the body of the
if statement anyway.

You have a few options to correct your code.  Consider the following:

for f in fruit:
  if f == "apples" or f == "pairs":
print f, "is an apple or a pair."

This will check if f is either "apples" or "pairs".  You can then invert that:

for f in fruit:
  if not (f == "apples" or f == "pairs"):
print f, "is not an apple or pair."

You could then use DeMorgan's law to convert this to:

for f in fruit:
  if f != "apples" and f != "pairs":
print f, "is not an apple or pair."

Finally, you have one more option, using the "in" operator:

for f in fruit:
  if f not in ["apples", "pairs"]:
print f, "is not an apple or pair."

HTH!

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] or

2007-03-29 Thread Christopher Spears
I was doodling at the interpreter:

>>> fruit = ["apples","pears","oranges"]
>>> for f in fruit:
... if f != "apples":
... print f
... print "This is not an apple."
...
pears
This is not an apple.
oranges
This is not an apple.

What I can't remember is what is 'or' in python.  For
example, what if I want the loop to skip apples and
pears?  I tried this:

>>> for f in fruit:
... if f != "apples" or "pears":
... print f
... print "This is not an apple or pear"
...
apples
This is not an apple or pear
pears
This is not an apple or pear
oranges
This is not an apple or pear
>>>

Actually maybe my problem is not asking the right
question?  Should I be looking for 'and' instead of
'or' ?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OR operator?

2006-04-06 Thread Alan Gauld
> I'm trying with this operator, could it possible?
> 
> fruit1 = 'apple'
> fruit2 = 'orange'
> 
> fruits = fruit1 or fruit2
> 
> is 'orange' = fruits ?

Why not just try it at the python prompt?!

And you would find that no, fruits = 'apple'
(The order is because fruits is a name which references a 
value. orange is a value so cannot reference anything else, so 
'orange' = fruits is actually an impossible concept!).

The reason for the result is the way Python evaluates 'or' expressions.
Python considers non-empty strings (like 'apple') to be true.
Python also evaluates an 'or' by evaluating the first element 
and, if it is true then it doesn't bother evaluating the second element
since the 'or' must be true if the first part is true. This is known 
as "short-circuit evaluation".

If you did 

fruit3 = ""
fruits = fruit3 or fruit2

this time fruits would equal 'orange' because the first item 
was an empty string which Python considers to be false 
so it had to evaluate the second item.

Finally, it could be argued that the 'or' should return 'true' or 'false 
but because Python considers values to be either true or false it 
just returns the value. I suspect that if boolean values had been 
in Python at the beginning the result would be different but 
they weren't and it isn't! :-)

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OR operator?

2006-04-05 Thread Charles Bartley
>
> I'm trying with this operator, could it possible?
>
> fruit1 = 'apple'
> fruit2 = 'orange'
>
> fruits = fruit1 or fruit2
>
> is 'orange' = fruits ?

Maybe this:

###

fruit1 = 'apple'
fruit2 = 'orange'

fruits = [fruit1, fruit2]

print 'orange' in fruits# prints the boolean result of 'orange in 
fruits'

###

Regards,
Charles B. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OR operator?

2006-04-05 Thread ទិត្យវិរៈ
Hi,

I'm trying with this operator, could it possible?

fruit1 = 'apple'
fruit2 = 'orange'

fruits = fruit1 or fruit2

is 'orange' = fruits ?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor