Re: Data Types

2016-09-22 Thread Chris Angelico
On Thu, Sep 22, 2016 at 10:33 PM, BartC  wrote:
>>>   print (10<20)=>  True
>>>   print (type(10<20))  =>  
>>
>>
>> 10<20 shouldn't be thought of as some alternative value which is a bool,
>> any
>> more than we should think of 1+1 as being a different value to 2.
>
>
> They're a little different:
>
>  1+1 yielding 2 is  int+int => int
>  10<20 yielding True is int bool

That's a couple of example expressions and the way they're handled.

> My post was really about bool values lurking everywhere not just where you
> explicitly write or assign True or False.

Sure. Thing is, those expressions are actually just syntactic sugar
for functions. Let's replace the integers with these puppies:

class Spam:
def __init__(self, value):
self.value = value
def __repr__(self):
return "Spam(%r)" % self.value
def __add__(self, other):
return Spam(self.value + other.value)
def __lt__(self, other):
if self.value < other.value:
return "Yes, %r < %r" % (self.value, other.value)
return ""

>>> Spam(1) + Spam(1)
Spam(2)
>>> Spam(10) < Spam(20)
'Yes, 10 < 20'
>>> Spam(10) > Spam(20)
''

This is perfectly legal code, and it doesn't use True or False for its
comparisons. If you want it to, you have to actually return one of
those constants from __lt__, either by explicitly typing its name, or
by passing it up the chain (eg "return self.value < other.value"),
which just moves the problem around.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-22 Thread BartC

On 22/09/2016 02:40, Steve D'Aprano wrote:

On Wed, 21 Sep 2016 10:25 pm, BartC wrote:


On 21/09/2016 05:03, Cai Gengyang wrote:


Are there any other data types that will give you type(A) or type(B) =
 besides True and False?


No types but any variable or expression containing True or False will be
a bool type (or class bool):


"Containing" True or False? Certainly not:


Well, I wrote 'containing' before I added 'or expression', when 
'evaluating to' might have been better.



  print (10<20)=>  True
  print (type(10<20))  =>  


10<20 shouldn't be thought of as some alternative value which is a bool, any
more than we should think of 1+1 as being a different value to 2.


They're a little different:

 1+1 yielding 2 is  int+int => int
 10<20 yielding True is int bool

My post was really about bool values lurking everywhere not just where 
you explicitly write or assign True or False.



--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-22 Thread Steve D'Aprano
On Thu, 22 Sep 2016 01:46 pm, Sayth Renshaw wrote:

> What about 0 or 1 they are true and false like no other numbers? what
> category do they fall in with regards to booleans?

0 and 1 are ints:

py> type(0)

py> type(1)



just like 2, 3, 4, -1, -999, 1098765432 etc.

But just like 1 == 1.0 (a float), and 0 == 0.0 (a float), and in fact 1 also
equals Fraction(1, 1) and Decimal(1) and 1+0j (complex number), so 1 also
equals the bool True and 0 equals the bool False.

The reason for this is that in many programming languages, there are no
boolean values. People use 0 for false and 1 (or sometimes -1) for true. In
the early days, Python was the same.

See https://www.python.org/dev/peps/pep-0285/ for more details.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-22 Thread Lawrence D’Oliveiro
On Thursday, September 22, 2016 at 3:47:27 PM UTC+12, Sayth Renshaw wrote:
> What about 0 or 1 they are true and false like no other numbers?

>>> isinstance(1, bool)
False
>>> isinstance(True, int)
True
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-22 Thread Larry Hudson via Python-list

On 09/20/2016 09:03 PM, Cai Gengyang wrote:
[snip...]

So for example for "bool" , it only applies to True/False and nothing else? (2 
data types), i.e. :


Not exactly...  bool is the data type (or class), True and False are the only two _values_ (not 
types).





type(True)



type(False)




[snip...]

Besides the bool class, with its two values, there is also the Nonetype class, which has only 
one value:  None.


But to expand on/rephrase what has already been said:  while the bool class/type has only the 
two values True/False, in expressions using logical operators (and, or, not...), ANY data type 
can be interpreted as a boolean.  These are often referred to as truthy and falsey values to 
distinguish them from actual boolean True/False values.


I'm sure you've already seen the list, if not you should quickly learn it...
Any numeric (int, float, complex) that is zero is falsey.
Any empty collection (list, string, dictionary...) is falsey.
EVERYTHING else is truthy.

--
 -=- Larry -=-
--
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-21 Thread Sayth Renshaw

> > 
> >> Are there any other data types that will give you type(A) or type(B) =
> >>  besides True and False?
> > 
> > No types but any variable or expression containing True or False will be
> > a bool type (or class bool):
> 
> "Containing" True or False? Certainly not:
> 
> py> type( [1, 2, True] )
> 
> py> type( False or 99 )
> 
> 
> Based on your examples, I think you mean any expression *evaluating to* True
> or False will be a bool. Um, yeah, of course it will. Because it evaluates
> to one of the only two bool values, True or False.
> 
> >   A = 10<20
> >   print (type(A))  =>  
> 
> That's because the value of A is True.
> 
> >   print (10<20)=>  True
> >   print (type(10<20))  =>  
> 
> 10<20 shouldn't be thought of as some alternative value which is a bool, any
> more than we should think of 1+1 as being a different value to 2.
> 
> 

What about 0 or 1 they are true and false like no other numbers? what category 
do they fall in with regards to booleans?

In [6]: 0 == False
Out[6]: True

In [7]: 1 == True
Out[7]: True

In [8]: 2 == True
Out[8]: False

In [9]: 3 == True
Out[9]: False

In [10]: 3 == False
Out[10]: False

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-21 Thread Steve D'Aprano
On Wed, 21 Sep 2016 10:25 pm, BartC wrote:

> On 21/09/2016 05:03, Cai Gengyang wrote:
> 
>> Are there any other data types that will give you type(A) or type(B) =
>>  besides True and False?
> 
> No types but any variable or expression containing True or False will be
> a bool type (or class bool):

"Containing" True or False? Certainly not:

py> type( [1, 2, True] )

py> type( False or 99 )


Based on your examples, I think you mean any expression *evaluating to* True
or False will be a bool. Um, yeah, of course it will. Because it evaluates
to one of the only two bool values, True or False.

>   A = 10<20
>   print (type(A))  =>  

That's because the value of A is True.

>   print (10<20)=>  True
>   print (type(10<20))  =>  

10<20 shouldn't be thought of as some alternative value which is a bool, any
more than we should think of 1+1 as being a different value to 2.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-21 Thread BartC

On 21/09/2016 05:03, Cai Gengyang wrote:


Are there any other data types that will give you type(A) or type(B) =  besides True and False?


No types but any variable or expression containing True or False will be 
a bool type (or class bool):


 A = 10<20
 print (type(A))  =>  

 print (10<20)=>  True

 print (type(10<20))  =>  

 --
Bartc
--
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-20 Thread Steven D'Aprano
On Wednesday 21 September 2016 14:03, Cai Gengyang wrote:

> So for example for "bool" , it only applies to True/False and nothing else?

Correct. True and False are the only instances of the type 'bool'.

> (2 data types), i.e. :
> 
 type(True)
> 
 type(False)
> 
> 
> Are there any other data types that will give you type(A) or type(B) =  'bool'> besides True and False?

No.





-- 
Steven
git gets easier once you get the basic idea that branches are homeomorphic 
endofunctors mapping submanifolds of a Hilbert space.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-20 Thread Cai Gengyang
On Wednesday, September 21, 2016 at 11:22:42 AM UTC+8, Steve D'Aprano wrote:
> On Wed, 21 Sep 2016 01:00 pm, Cai Gengyang wrote:
> 
> > So I am going through chapter 7.1 (Data Types) of
> > http://programarcadegames.com/index.php… What do these terms (input and
> > output) mean --- Can someone explain in plain English and layman's terms ?
> 
> http://www.dictionary.com/browse/input
> 
> http://www.dictionary.com/browse/output
> 
> 
> Does that answer your question about "input and output"?
> 
> 
> > Thanks a lot ...
> > 
>  type(3)
> > 
>  type(3.145)
> > 
>  type("Hi there")
> > 
>  type(True)
> > 
> 
> I don't understand why you are showing this. What part don't you understand?
> 
> The number 3 is an int (short for integer); that is the type of value it is.
> 5 is also an int, and 0, and 9253, and -73. They are all integers.
> 
> 3.145 is a float (short for "floating point number"). "Hi there" is a str
> (short for string). True is a bool (short for Boolean value, which is a
> technical term for special True/False values).
> 
> Perhaps if you can ask a more clear question, I can give a more clear
> answer.
> 
> 
> 
> 
> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.

Right, 

So for example for "bool" , it only applies to True/False and nothing else? (2 
data types), i.e. :

>>> type(True)

>>> type(False)


Are there any other data types that will give you type(A) or type(B) =  besides True and False?

Regards,

GY

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Data Types

2016-09-20 Thread Steve D'Aprano
On Wed, 21 Sep 2016 01:00 pm, Cai Gengyang wrote:

> So I am going through chapter 7.1 (Data Types) of
> http://programarcadegames.com/index.php… What do these terms (input and
> output) mean --- Can someone explain in plain English and layman's terms ?

http://www.dictionary.com/browse/input

http://www.dictionary.com/browse/output


Does that answer your question about "input and output"?


> Thanks a lot ...
> 
 type(3)
> 
 type(3.145)
> 
 type("Hi there")
> 
 type(True)
> 

I don't understand why you are showing this. What part don't you understand?

The number 3 is an int (short for integer); that is the type of value it is.
5 is also an int, and 0, and 9253, and -73. They are all integers.

3.145 is a float (short for "floating point number"). "Hi there" is a str
(short for string). True is a bool (short for Boolean value, which is a
technical term for special True/False values).

Perhaps if you can ask a more clear question, I can give a more clear
answer.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Data types

2005-03-24 Thread Lonnie Princehouse
Lists, tuples, and dictionaries are built-in types.
Classes are the mechanism for user-defined types in Python.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data types

2005-03-24 Thread vahmad70
I am new to python and learning it. Can you please give me a simple
example of user defined type through class mechanism.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data types

2005-03-24 Thread Bill Mill
On 24 Mar 2005 10:29:40 -0800, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 I am new to python and learning it. Can you please give me a simple
 example of user defined type through class mechanism.

GIYF:
http://www.python.org/2.2/descrintro.html#subclassing

Peace
Bill Mill
bill.mill at gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data types

2005-03-24 Thread Tim Jarman
[EMAIL PROTECTED] wrote:

 I am new to python and learning it. Can you please give me a simple
 example of user defined type through class mechanism.

Python 2.4 (#1, Dec 31 2004, 17:21:43)
[GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2
Type help, copyright, credits or license for more information.
 class Gumby(object):
... def __init__(self, body_part):
... self.body_part = body_part
... def speak(self):
... print My %s hurts! % self.body_part
...
 g = Gumby(brain)
 g
__main__.Gumby object at 0x402c570c
 g.speak()
My brain hurts!


Despite what Mr Gumby just said, defining your own classes is pretty
painless in Python. Check out the Tutorial, especially section 9:
http://www.python.org/doc/2.4/tut/node11.html

Enjoy!

-- 
Website: www DOT jarmania FULLSTOP com
-- 
http://mail.python.org/mailman/listinfo/python-list