Smallest value of a list "iam a begginer"

# list = [-3,4,5,6]
# sss=0
# for i in list:
#   if list[0]>list[sss]:
#     print ("smallest",i)
#   else :
#     i =list[0]
#   sss+=1
# print(i)

Will this code always output the smallest value regardless of the contents of the list?

Why not create a few test cases and see for yourself?

1 Like

Have a look at python’s min function. It’s bad practice to redefine a python builtin. list is a python builtin.

For the sake of getting a taste of thinking about the algorithm of doing tasks, I think it’s OK to practise writing codes with basic statements to do things like finding the minimum and maximum value in a list of numbers, sorting a list or reversing the items in a list, even though there are built-in functions to do that. Many beginner courses or books have such practices.

1 Like

Will this code always output the smallest value regardless of the contents of the list?

No and Yes, depends on what do you mean by ‘output’. If you mean by the last statement print(i), the answer is No. After the running of the code, i will either be the last value of the list if the last value is the smallest, or always be the first value no matter it is the smallest or not.

To run as the code is written, If the first value is the smallest, the output will be only that value. But if there were values smaller than the first value, they will be all printed out in different lines with ‘smallest’ at the beginning, before the printout of the value of i. You may argue afterall the smallest value is included in the overall output, though it will need human judgement on which line of output is in fact the smallest one…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.