What is the default value that Python chooses when adding two variables?
<p>Suppose we have the following context: </p>

<pre><code>x = 3  
y = 4  
z = x + y
</code></pre>

<p>Will <code>z</code> be an <code>int</code> or <code>float</code>, etc? I am well aware that floating numbers do end with <code>.something</code>, however its unclear to me whether Python will favour the floating point type over the integer type, given the circumstances that it is unpredictable whether the user is going to change this variable to another type or not.</p>

<p>When <em>mixing</em> types, you'll always get the 'wider' type, where a complex number is wider than float, which in turn is wider than an integer. When both values are the same type (so <em>not</em> mixing types), you will just get that one type as the result.</p>

<p>From the <a href="https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex" rel="nofollow"><em>Numeric Types</em> documentation</a>:</p>

<blockquote>
  <p>Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the ânarrowerâ type is widened to that of the other, where plain integer is narrower than long integer is narrower than floating point is narrower than complex. Comparisons between numbers of mixed type use the same rule.</p>
</blockquote>

<p>So when you are summing numbers of different types, Python will widen the narrower type to be the same as the wider type. For an operation with an integer and a floating point number, you'll get a float, as float is wider.</p>

<p>However, there is no point in changing the type if both operands are the same type. Changing the type in that case would be very surprising.</p>

python python-3.x types