@Sudarshan iyengar, sir.. I'm not new to programming, but new to python! I find your classes interesting and simple to understand. But the best part is that my son (9 yrs old), is more interested in your course than I am :) He is finding it extremely simple and easy to understand and is playing around with Python using your course contents.. Thanks a lot for making the course so simple and easy for everyone to understand
if we dont plan to change the value of the elements in that for loop, then for-each (for i in l) is the better way to go. But if we want to change elements, i = .... wont really do anything in a for-each loop, there we need a C-style for loop and do l[i] = ... trying executing this code: l = [1,2,3] for i in l: print(i, end=' ') i = i + 10 print(i) print(l) due to the prints inside the forloop it seems that the value changed, but it only changes the value of 'i' and doesn't touch the list itself. This can be seen in the last print (outside the loop) that still prints [1,2,3]. So if we are finding the minimum value in a list we can use for i in l. (or better can just use min(l)) If we are sorting the list using its indexes (not like the obvious sort in this lecture) then we need for i in range(len(l)) bottomline, use foreach if you dont wish to change the list in that forloop If you are asking about time complexity (efficiency of the program) then they are almost the same, but the for i in range(len(l)) is slightttttly faster.
@@sayanghosh6996 Whoa, that's really interesting. I didn't know 'for i in range(len(l))' has a better time complexity. I just ran a few iterations of both variations and took down the time taken to execute. From what I got, it appears that they take _almost_ the same time, but 'for i in l' seems to be _slightly_ faster. But again, time complexity is not the same as time taken to execute. So, I might need to use larger samples to get a more accurate result. Hmm, now I'm tempted to look up the source code of range().
@@shreyapal6111 yeah they have the same time complexity i guess, but on my (not so rigourous) experiment i found range (len(l)) to run a few milliseconds faster.
I actually mention this very point in one of the videos coming ahead. Use your discretion and use the one that is easy for you (based on the context). In the current context both are equivalent.
@Sudarshan iyengar, sir.. I'm not new to programming, but new to python! I find your classes interesting and simple to understand. But the best part is that my son (9 yrs old), is more interested in your course than I am :) He is finding it extremely simple and easy to understand and is playing around with Python using your course contents.. Thanks a lot for making the course so simple and easy for everyone to understand
Sir i am new in programming Thank you so much for making such easy to understand content.😊😊
Sir aap hi maths2 bhi padha do 🙏🏻
haha.......wish this could happen
Lol 😂
ascending order: 4:50
min element in list: 6:05 6:37
obvious sort: 7:08
moral of the story: 9:30 10:05
The only thing i'm wondering about is the "mimimum' you wrote in line 1.
u r great sir, thanks
how many time sudarshan sir will return l. hahaaa
that is like saying thank you... for the lack of a better phrase :) :) :)
@@sudarshaniyengar8549 yes sir
We can just write the code in this way also
def sort_list(l):
x=[]
l.sort()
x=l.copy()
l.clear()
return x
it also works fine😅
I noticed that you usually use 'for i in range(len(l))' instead of 'for i in l'.
Is there any benefit of using the former over the latter?
if we dont plan to change the value of the elements in that for loop, then for-each (for i in l) is the better way to go. But if we want to change elements, i = .... wont really do anything in a for-each loop, there we need a C-style for loop and do l[i] = ...
trying executing this code:
l = [1,2,3]
for i in l:
print(i, end=' ')
i = i + 10
print(i)
print(l)
due to the prints inside the forloop it seems that the value changed, but it only changes the value of 'i' and doesn't touch the list itself. This can be seen in the last print (outside the loop) that still prints [1,2,3].
So if we are finding the minimum value in a list we can use for i in l. (or better can just use min(l))
If we are sorting the list using its indexes (not like the obvious sort in this lecture) then we need for i in range(len(l))
bottomline, use foreach if you dont wish to change the list in that forloop
If you are asking about time complexity (efficiency of the program) then they are almost the same, but the for i in range(len(l)) is slightttttly faster.
@@sayanghosh6996 Whoa, that's really interesting. I didn't know 'for i in range(len(l))' has a better time complexity.
I just ran a few iterations of both variations and took down the time taken to execute. From what I got, it appears that they take _almost_ the same time, but 'for i in l' seems to be _slightly_ faster.
But again, time complexity is not the same as time taken to execute. So, I might need to use larger samples to get a more accurate result.
Hmm, now I'm tempted to look up the source code of range().
@@shreyapal6111 yeah they have the same time complexity i guess, but on my (not so rigourous) experiment i found range (len(l)) to run a few milliseconds faster.
I actually mention this very point in one of the videos coming ahead. Use your discretion and use the one that is easy for you (based on the context). In the current context both are equivalent.
@@shreyapal6111 I am not sure if there is any time complexity difference.