[BangPypers] strange behavior

Sidharth Kuruvila sidharth.kuruvila at gmail.com
Thu Jul 10 11:03:11 CEST 2008


Any number with a decimal point is treated as an object of type float and
the numbers without a decimal point of are treated as ints. You can use the
type function to find the type of an object.

>>> type(1)
<type 'int'>
>>> type(1.0)
<type 'float'>

>>> 1+1.0
2.0
Arithmetic operations over two types give the result in the more flexible
type, in this case that is a float.

You can use the int and float functions for type conversion

>>> int(1.0)
1
>>> float(1)
1.0


On Thu, Jul 10, 2008 at 10:30 AM, Pradeep Gowda <pradeep at btbytes.com> wrote:

>
> On Jul 10, 2008, at 12:54 AM, Gopal Ghosh wrote:
>
>>
>>
>> *********************
>> >>> 7/3
>> 2
>> >>> 7/-3
>> -3
>> >>> 3/7
>> 0
>> >>> # again reapting the questions with one more decimal place
>> >>> 7.0/3
>> 2.3333333333333335
>> >>> 7.0/-3
>> -2.3333333333333335
>> >>> 3.0/7
>> 0.42857142857142855
>> >>> # why it is not showing the exact values in the previous cases
>> >>>
>>
>
> Dividing Integer by an integer will give you Integer
> Dividing Integer by a float or a float by an integer will give a float.
> See this: http://docs.python.org/ref/binary.html
>
> However this behaviour will change to a less "surprising"   3/4 returning
> 0.75 as one would expect in python 3.0
> See: http://www.comp.leeds.ac.uk/nde/papers/
> teachpy3.html#unsurprising-arithmetic
>
> -PG
> http://pradeepgowda.com
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20080710/91304273/attachment.htm>


More information about the BangPypers mailing list