Help understand code, beginner

Welcome @kralsteny . When you post code, please follow this advice, so it is more readable. As you know, indenting is vital in Python, but the default formatting for text takes it out.

I think the best explanation would be to watch it working. Copy that print statement into the inner loop. You could put it inside the if body so it only prints when there’s a change. Print anything else that helps you follow the logic. Make the list shorter too, temporarily, so you can watch to the end.

The short explanation is:

  • j is always greater than (i.e. after) i .
  • kopie_jmen[i] > kopie_jmen[j] compares strings alphabetically.
  • So if kopie_jmen[i] belongs after kopie_jmen[j] it will swap them in the list. (That assignment of two things at once is a cool Python feature.)
  • By the end of the inner loop (on j) you are guaranteed that what is now in kopie_jmen[i] belongs before everything that is now after it in the list.
  • And when you’ve done that for every i, you’re sorted.

But there’s really no substitute for just watching it work.