How can I find the smallest number in a list using a for or while loop in Python?
Matthew
2012-01-26 10:44:29 UTC
I have a list of numbers entered by the user (there is no limit to how small or big the numbers can be) and I need to find the smallest one in the list by using a for or while loop. Any suggestions? Thanks.
Three answers:
anonymous
2012-01-29 16:17:41 UTC
In python the easiest way to find a minimum in a list is to just use the built in min() function.
Ex:
>>>foo = [44,23,2,11,19,5]
>>>min(foo)
2
However if you do want to use a for loop, you could iterate through the list by index and compare values to find the smallest one.
>>>minimum = foo[0]
>>>for i in foo[1:):
>>> if i >>>print minimum
I have 0 characters to work with
2012-01-26 10:52:43 UTC
I don't work with python, but this is the general solution for all languages to find a minimum in an unsorted list:
minimum = the first number in the list
for (the second number in the list through to the last number)
{
> if (the number you are iterated to < minimum)
> minimum = the number you are iterated to
}
hopefully this pseudocode makes sense if that doesn't maybe this will (though its not correct syntax)
minimum = list[0];
for (i=0; i > if(list[i]
glauser
2016-12-08 16:36:30 UTC
ok, i will attempt that can assist you you on the 2d: First you may desire to create an array(record of things) then use the technique that converts uppercase to lowercase, use counter with the technique of discover to discover each and every characters. use if assertion to print basically the quantity of each and every letter discovered and yet another print for the characters that are no longer letters making use of the technique to discover char. etc.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.