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>This is easy to test. By default, if you perform arithmetic on two integers, Python will store the result as an integer. You can see that here.</p>

<pre><code>&gt;&gt;&gt; x = 1
&gt;&gt;&gt; y = 2
&gt;&gt;&gt; z = x + y
&gt;&gt;&gt; type(z)
&lt;class 'int'&gt;
</code></pre>

<p>The type function will tell you what the 'type' of an object is in Python. Here we can see that the type of the z variable is int, which stands for integer.</p>

<p>If we were to perform arithmetic on two different types, say integer and float, and the result was a decimal, then the result will be stored as a float.</p>

<pre><code>&gt;&gt;&gt; x = 2.55
&gt;&gt;&gt; y = 3
&gt;&gt;&gt; z = x / 3
&gt;&gt;&gt; type(z)
&lt;class 'float'&gt;
</code></pre>

<p>Now, if you were to perform arithmetic on an integer and a float and the result were to come out as a non-decimal, Python will STILL store this as a float. You can see that here.</p>

<pre><code>&gt;&gt;&gt; x = 2.5
&gt;&gt;&gt; y = 1.25
&gt;&gt;&gt; z = x / y
&gt;&gt;&gt; type(z)
&lt;class 'float'&gt;
&gt;&gt;&gt; print(z)
2.0
</code></pre>

<p>In conclusion, when you do arithmetic, Python will store the result as an integer only if the two numbers involved were also integer. If one was float, then the new variable will also be of float type (or class).</p>

<p>Hope this helps :)</p>

python python-3.x types