Help understand code, beginner

Hello, i am new python user and i am trying to practice online my skill but got stucked with one task where i have solution but i dont understand solution at all. i need to sort names in list alphabeticaly without using sort function and solution for that is this code.

jmena = [
‘Michal’, ‘Pepa’, ‘Honza’,
‘Pavel’, ‘Lukas’, ‘Matej’,
‘Iva’, ‘Klara’, ‘Jana’,
‘Honza’, ‘Vasek’,‘Milan’,
‘Michal’
]
kopie_jmen = jmena.copy()

for i in range(len(kopie_jmen)):

for j in range(i + 1, len(kopie_jmen)):

   
    if kopie_jmen[i] > kopie_jmen[j]:
        kopie_jmen[i], kopie_jmen[j] = kopie_jmen[j], kopie_jmen[i]

print(kopie_jmen)

Can you explain me how it can sort names corectly alphabeticaly??

Please format that properly, see the pinned topic.

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.

I’m new to Python but not new to programming. Please correct me if this is wrong.

Each letter has a numeric value. So the names are sorted by the NUMERIC value of the letters. Add in names in lowercase like ‘michal’ and ‘pepa’ and sort the list, the print the list. Capital letters have a value before lowercase letters, so a capital A has a value of 65, but lowercase has has a higher value.

Also please format all your code so we can just copy and paste it and run it ourselves to help you. You will get more help that way.

Also when I copied your code your code uses the wrong quotes in the first line jmena = [ and for all those entries in the list. Never use a word processor to write code even if it saves as plain text as it can mess things up. Use a proper text editor like Notepad++ (Downloads | Notepad++) or PSPad (PSPad download - freeware text editor) or something else. Those two are free.

You will need a text editor which can show hidden characters because Python is real picky about indentation. Indentation must be all spaces or all tabs, not mixed.

Here’s the program I used and the output I got:

jmena = [
'Michal', 'Pepa', 'Honza',
'Pavel', 'Lukas', 'Matej',
'Iva', 'Klara', 'Jana',
'Honza', 'Vasek','Milan',
'Michal', 'michal', 'jana'
]
kopie_jmen = jmena.copy()

for i in range(len(kopie_jmen)):
    for j in range(i + 1, len(kopie_jmen)):
        if kopie_jmen[i] > kopie_jmen[j]:
            kopie_jmen[i], kopie_jmen[j] = kopie_jmen[j], kopie_jmen[i]
        
print(kopie_jmen)

Output:

['Honza', 'Honza', 'Iva', 'Jana', 'Klara', 'Lukas', 'Matej', 'Michal', 'Michal', 'Milan', 'Pavel', 'Pepa', 'Vasek', 'jana', 'michal']

I wouldn’t say that they have numeric values, but that the computer represents each distinct character with a number, a “character code”.

You can see that number with the ord function:

>>> ord('A')
65
>>> ord('B')
66
>>> ord('C')
67
>>> ord('a')
97