[Tutor] While truth

2014-05-20 Thread Ian D
I was reading a tutorial that had these examples in it:


 while False:

  print(False is the new True.)


 while 6:

  print(Which numbers are True?)


while -1:

  print(Which numbers are True?)


while 0:

  print(Which numbers are True?)



Unfortunately the author never explained these statements.


I was wondering if the gist of a while statement could be explained in the 
context of these examples.


e.g. while False:


means while True is False, which is never True because True is of course True 
not False.


but while 6:


means. err while 6 is True? and this is True because... err.


Anyway I am a  bit lost with this.


Can anyone shed any light please?


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


Re: [Tutor] While truth

2014-05-20 Thread Ian D
Or should I have said While False is True, which is never True, because False 
is False not True


 From: dux...@hotmail.com
 To: tutor@python.org
 Date: Tue, 20 May 2014 08:25:48 +
 Subject: [Tutor] While truth

 I was reading a tutorial that had these examples in it:


 while False:

 print(False is the new True.)


 while 6:

 print(Which numbers are True?)


 while -1:

 print(Which numbers are True?)


 while 0:

 print(Which numbers are True?)



 Unfortunately the author never explained these statements.


 I was wondering if the gist of a while statement could be explained in the 
 context of these examples.


 e.g. while False:


 means while True is False, which is never True because True is of course True 
 not False.


 but while 6:


 means. err while 6 is True? and this is True because... err.


 Anyway I am a bit lost with this.


 Can anyone shed any light please?


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


Re: [Tutor] While truth

2014-05-20 Thread David Palao
Hi,
6, -1 or 0 are not bools (True or False):
 6 is True
False

 0 is False
False
 If you had to design a language and want to think about using numbers
in a logical context you could do at least two things:
1) convert the number to bool, ie define a set of rules to assign to
each number a logical value, or
2) don't convert and raise an error.
In python, like in many other languages, option 1) has been chosen.
The rules are roughly: when using a number in a logical context 0 is
casted to False, and the other numbers are considered True.
The while statement expects an expression that returns a logical
value. Put both things together and I think you get your answer, if I
well understood.
Best

2014-05-20 10:25 GMT+02:00 Ian D dux...@hotmail.com:
 I was reading a tutorial that had these examples in it:


 while False:

   print(False is the new True.)


 while 6:

   print(Which numbers are True?)


 while -1:

   print(Which numbers are True?)


 while 0:

   print(Which numbers are True?)



 Unfortunately the author never explained these statements.


 I was wondering if the gist of a while statement could be explained in the 
 context of these examples.


 e.g. while False:


 means while True is False, which is never True because True is of course True 
 not False.


 but while 6:


 means. err while 6 is True? and this is True because... err.


 Anyway I am a  bit lost with this.


 Can anyone shed any light please?


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


Re: [Tutor] While truth

2014-05-20 Thread Cameron Simpson

On 20May2014 08:25, Ian D dux...@hotmail.com wrote:

I was reading a tutorial that had these examples in it:


while False:

 print(False is the new True.)


while 6:

 print(Which numbers are True?)

while -1:
 print(Which numbers are True?)

while 0:
 print(Which numbers are True?)

Unfortunately the author never explained these statements.


That is a pity. Sounds badly written.

I would imagine the intent is that you could try these and see what happens. I 
think that exercise would be more effective with if-statements instead of 
while-statements.


Basicly, the point is likely to show that you do not need to use a bool as 
the while condition; any value considered truthy by Python will do if it 
matches what you are working with.


Broadly, None and 0 and False and empty collections (empty lists, empty sets, 
zero length strings, etc) are false, and most other things are true.


Cheers,
Cameron Simpson c...@zip.com.au

Rugby is a beastly game played by gentlemen; soccer is a gentleman's game
played by beasts; football is a beastly game played by beasts.
- Henry Blaha
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While truth

2014-05-20 Thread Steven D'Aprano
On Tue, May 20, 2014 at 08:25:48AM +, Ian D wrote:

 I was reading a tutorial that had these examples in it:
 
  while False:
 
   print(False is the new True.)

[... snip examples ...]


 I was wondering if the gist of a while statement could be explained in 
 the context of these examples.
 
 e.g. while False:
 
 means while True is False, which is never True because True is of 
 course True not False.
 
 but while 6:
 
 means. err while 6 is True? and this is True because... err.


Not quite 6 is True, except figuratively speaking.

It might be easier to consider if...else rather than while, although the 
rules are exactly the same. An if...else block looks at a value, and 
then decides which branch to take: the if part or the else part. So 
we might have something like:

if condition:
do_this()
else:
do_that()


While-loops are similar, except that they repeat so long as the 
condition is a true value, rather than just once.

while condition:
do_this()


Now, why did I say the condition is a true value instead of the 
condition is True? To explain, let me contrast Python with some other 
programming languages.

In some programming languages, the condition in an if or while statement 
is restricted to one of exactly two values, usually called True and 
False. These values are called Booleans (named after the mathematician 
who first worked on them, George Boole) or just bools. Any other 
value, like 6, or None, or Hello World, is an error. In those 
languages, if condition will take one branch if condition is True, and 
the other branch if False, and there are no other possibilities:

if condition:
print(condition is True)
else:
print(condition is False)


*Python is not like this.*

In languages like Python, the value being tested can have many different 
values. We can describe this in various ways:

# This is what Python does.
if condition:
print(condition is a truthy value)
print(condition is true-ish)
print(in a boolean context, condition is true)
print(condition is something)
else:
print(condition is a falsey value)
print(condition is false-ish)
print(in a boolean context, condition is false)
print(condition is nothing)


Think of this as duck-typing for bools. Most of the time, we don't 
care if condition is *actually* True or False, only whether it is 
true-like or false-like. Many other languages do something similar to 
this, e.g. Javascript, PHP, Ruby, and so on.

Notice that I use (big T) True and (big F) False to refer to the actual 
Python constants True and False, and (small t) true and (small f) false 
to refer to things which are truthy or falsey.

Back to our if...else statement, or while statement: the condition 
doesn't have to be an actual bool True or False, it can be any value at 
all. Try running this bit of code and see what it prints:


for condition in (Hello World, 23, [], , None, 1.5, 0.0, [1, 2, 3]):
if condition:
print(%r is a truthy value % condition)
else:
print(%r is a falsey value % condition)


Can you see the pattern? I suggest you run the above before reading on. 
Don't worry, I'll wait... 

W
E
 
A
R
E
 
W
A
I
T
I
N
G

Can you see the pattern?

In Python, values which represent something are considered truthy. 
Those which represent nothing or empty are considered falsey. So we 
have:

Falsey values:
  None
  0
  0.0
  empty string 
  empty list []
  empty tuple ()
  empty dict {}
  and of course False

Truthy values:
any non-zero integer, like 1, 2, -3, ...
any non-zero float, like 2.5, 17.8, -100.1, ...
any non-empty string, like spam, eggs, ...
any non-empty list, like [1, 2, 3]
any non-empty tuple, like (0, 1)
any non-empty dict, like {23: twenty-three}
and of course True


99% of the time, you shouldn't care whether something is actually True 
or False. Well, perhaps 90% of the time. But, if you do care, you can 
convert any object you like to True or False by calling bool() on it:

bool(None)
= returns False, because None is falsey

bool(101)
= returns True, because 101 is truthy


What do you think bool(False) will return?

Since it's a string, and it is not the empty string , it will return 
True. If you need to convert the string False to False, you need to 
test for it yourself.

A few words of advice. Never write something like this:

while bool(value):
...

since the call to bool() is redundant. Python already checks to see 
whether value is truthy, calling bool() just does it twice.

But even worse is this:

while bool(value) is True:
...


That just displays unfamiliarity with boolean logic and makes you look 
ignorant. bool(value) will return True or False, so comparing it to True 
is redundant. If you don't trust that, where do you stop?

while bool(value): ...
while bool(value) is True: ...
while (bool(value) is True) is True: ...
while ((bool(value) is True) is True) is True: ...
while (((bool(value) is True) is True) is True) 

Re: [Tutor] While truth

2014-05-20 Thread Danny Yoo
On Tue, May 20, 2014 at 1:25 AM, Ian D dux...@hotmail.com wrote:
 I was reading a tutorial that had these examples in it:


 while False:
   print(False is the new True.)


 while 6:
   print(Which numbers are True?)


 while -1:
   print(Which numbers are True?)


 while 0:
   print(Which numbers are True?)

 Unfortunately the author never explained these statements.


The statements above are trying to talk about what Python considers to
be true.  In some languages, there is a single distinguished true
value.  Python chooses a broader definition that allows everything to
be considered true, with the exception of the following values:

False
None
Numeric zero
Empty collection
Empty string

Reference: https://docs.python.org/3/reference/expressions.html#booleans

We care about what values are true, because they are the switch that
controls which way we're flowing through a conditional statement like
if or while.

As people are pointing out, the examples above are a bit
disappointing.  They are demonstrating this with infinite while loops,
and that's probably not a great idea because the output will be so
overwhelming to be actively distracting.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While truth

2014-05-20 Thread C Smith
You can test out a condition like this in IDLE like so:
while 6:
print yes its true
break


while 0:
print yes its true
break


while -1:
print yes its true
break


emptyList = []
while emtpyList:
print yes its true
break

This way you don't have to deal with an infinite loop.
It will print yes its true once, if it IS true and then break will
break you out of the loop.

On Tue, May 20, 2014 at 2:00 PM, Danny Yoo d...@hashcollision.org wrote:
 On Tue, May 20, 2014 at 1:25 AM, Ian D dux...@hotmail.com wrote:
 I was reading a tutorial that had these examples in it:


 while False:
   print(False is the new True.)


 while 6:
   print(Which numbers are True?)


 while -1:
   print(Which numbers are True?)


 while 0:
   print(Which numbers are True?)

 Unfortunately the author never explained these statements.


 The statements above are trying to talk about what Python considers to
 be true.  In some languages, there is a single distinguished true
 value.  Python chooses a broader definition that allows everything to
 be considered true, with the exception of the following values:

 False
 None
 Numeric zero
 Empty collection
 Empty string

 Reference: https://docs.python.org/3/reference/expressions.html#booleans

 We care about what values are true, because they are the switch that
 controls which way we're flowing through a conditional statement like
 if or while.

 As people are pointing out, the examples above are a bit
 disappointing.  They are demonstrating this with infinite while loops,
 and that's probably not a great idea because the output will be so
 overwhelming to be actively distracting.
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While truth

2014-05-20 Thread Danny Yoo
On Tue, May 20, 2014 at 11:44 AM, C Smith illusiontechniq...@gmail.com wrote:
 You can test out a condition like this in IDLE like so:
 while 6:
 print yes its true
 break


 while 0:
 print yes its true
 break


 while -1:
 print yes its true
 break


 emptyList = []
 while emtpyList:
 print yes its true
 break

 This way you don't have to deal with an infinite loop.
 It will print yes its true once, if it IS true and then break will
 break you out of the loop.


That being said, if we're going to go through these contortions, we
might as well use if, right?  :P


The infinite loops in the examples above are a complete distraction
from the main point, I think.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While truth

2014-05-20 Thread C Smith
Of course that isn't very useful code. I thought it might be a useful
quick test for someone learning how while loops treat different
values.

On Tue, May 20, 2014 at 2:46 PM, Danny Yoo d...@hashcollision.org wrote:
 On Tue, May 20, 2014 at 11:44 AM, C Smith illusiontechniq...@gmail.com 
 wrote:
 You can test out a condition like this in IDLE like so:
 while 6:
 print yes its true
 break


 while 0:
 print yes its true
 break


 while -1:
 print yes its true
 break


 emptyList = []
 while emtpyList:
 print yes its true
 break

 This way you don't have to deal with an infinite loop.
 It will print yes its true once, if it IS true and then break will
 break you out of the loop.


 That being said, if we're going to go through these contortions, we
 might as well use if, right?  :P


 The infinite loops in the examples above are a complete distraction
 from the main point, I think.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor