<br><br><div class="gmail_quote">On Wed, Mar 3, 2010 at 6:45 PM, Wells <span dir="ltr"><<a href="mailto:thewellsoliver@gmail.com">thewellsoliver@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
This seems sort of odd to me:<br>
<br>
>>> a = 1<br>
>>> a += 1.202<br>
>>> a<br>
2.202<br>
<br>
Indicates that 'a' was an int that was implicitly casted to a float.<br>
But:<br>
<br>
>>> a = 1<br>
>>> b = 3<br>
>>> a / b<br>
0<br>
<br>
This does not implicitly do the casting, it treats 'a' and 'b' as<br>
integers, and the result as well. Changing 'b' to 3.0 will yield a<br>
float as a result (0.33333333333333331)<br>
<br>
Is there some way to explain the consistency here? Does python<br>
implicitly change the casting when you add variables of a different<br>
numeric type?<br>
<br>
Anyway, just  curiosity more than anything else. Thanks!<br>
<font color="#888888">--<br></font></blockquote><div><br>Python doesn't cast anything. Variable names in python do not have types. It's the objects bound to the names that have types.<br><br>a += 1.202 does not change a in place. It is exactly the same thing as writing<br>
a = a + 1.202<br><br>In Python (like many other languages), int + float returns a float. That float is then assigned to the name "a". <br><br>In Python 2, all operations with two int operands return an int, including division. This was changed in Python 3 so that 1 / 3 will return a float and 1 // 3 will do integer division.<br>
 </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><font color="#888888">
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>