Hi all,

Last week went so fast learning Python the hard way. Did many exercises on looping and conditional statements in Python. Here I am explaining how I did solve a problem in Python.
The program was to find the biggest two numbers from a list of numbers. As I had already done the function to find largest number from a list, I used it to assign largest number from the list to a variable big. Then I assigned the first element of the list to another variable big2, and compared it to all the other elements of the list with a loop. If the compared element was bigger than current big2 value and it is less than the big value, then the current element is assigned to big2. This way, by the end of the loop we will have the second largest value.

``````def biggest2(x):
big2 = x
big = biggest(x)
for i in x:
if( i > big2):
if(i < big):
big2 = i
return [big,big2]``````

The first time I tried, the function was working fine. Then after checking another list in which largest value is the first element, the function returned  first element for both big and big2 instead of two different values. That is when I realized that line 4 if condition is never True with first element being the largest number. So, I redefined the function as follows.

``````def biggest2(x):
big2 = x
big = biggest(x)
if(big2 == big):
big2 = x
for i in x:
if( i > big2):
if(i < big):
big2 = i

return [big,big2]
``````

After that the program did work fine. After two days I happened to check the program, I totally forgot why these extra two lines outside the loop were there and had to spent a lot of time to figure it out. And realized commenting is very important even in the simplest of programs.
That's all for this week. Thank you.