For Loops in Python (Output Smallest Input)
<p>Okay so I am practicing for loops in  Python and I was wondering how could I make a user input 10 intergers then it would output the smallest one. I would know how to do this with a while loop for example: </p>

<pre><code>Smallest = 0
count = 0
while count &lt; 10:
    Number = int(input("Enter a number &gt;&gt; "))
    if Number &lt; Smallest:
        Smallest = Number
    count = count + 1

print("{0} is the biggest value you have entered".format(Smallest))
</code></pre>

<p>But how do I do it in a for loop format? Here's what I have so far:</p>

<pre><code>for i in range(10):
    Number = int(input("Enter a Number &gt;&gt; "))
    if Number &lt; Smallest:
        Smallest = Number

print("{0} is the smallest value you have entered.".format(Smallest))
</code></pre>

<p>What you have there, other than the fact you should initialise <code>Smallest</code>, is equivalent to the solution using your <code>while</code> statement.</p>

<p>So, assuming the <code>while</code> variant is considered correct, the <code>for</code> variant you have will suffice.</p>

<p>The reason I qualify it is because initialising <code>Smallest</code> to zero is actually the <em>wrong</em> thing to do. If your ten numbers are all greater than twenty (for example), <code>Smallest</code> will remain at zero, giving you the wrong answer.</p>

<p>One way to fix this (in both variants) is to change your <code>if</code> statement to:</p>

<pre><code>if i == 0 or Number &lt; Smallest:
</code></pre>

<p>which will set <code>Smallest</code> first time through the loop regardless (though it should be <code>count</code> in the <code>while</code> variant since that's using a different loop control variable).</p>

<hr>

<p>Of course, as you learn more and more about the language, you'll come across the concept of things that are more "Pythonic" than others. An example of this is the rather more succinct:</p>

<pre><code>Smallest = min(int(input("Enter a Number &gt;&gt; ")) for i in range(10))
</code></pre>

<p>which removes the need totally for an explicit check-and-replace strategy (that's still done, but under the covers of the <code>min</code> function).</p>

python loops for-loop