Do Python sets change order only on start of programme execution?

Viewed 49

I am just starting to learn Python and programming at all, so probably a stupid question, but I can't stop wondering: when do sets actually change the order of their parts.

So I tried to check.

I have decided to try it out and wrote up a set and some print commands. I actually expected that each time it prints out there will be new order. But no. Order seems to change only when I execute code. Tried in replit.com and in PyCharm IDE. I have even included print(type()) command to check that I actually did created a set, not something else (being a newbie and all..).

So the question: when do Python sets actually change the order of their parts?

Here is my code:

mixed_set = {1, "1", "alef", "bet", "gimel", "alpha", "beta", "gamma", "delta", "delta", "gelato", "London", "Paris", '222', 0.48, "-67", 12113, "Berlin", "Volperting", "August", "Seneca", "Tiberivs"}
print('\ncheck the class of mixed_set: ', type(mixed_set))
print('\ncheck the length of mixed_set: ', len(mixed_set), "\n")
print("first print: ", mixed_set) # first print
print("second print: ", mixed_set) # second print
print("third print: ", mixed_set) # third print
print("fourth print: ", mixed_set) # fourth print
print("fifth print:", mixed_set) # fifth print

print('\n Lets run a print over a for cycle\n')
for x in range(5):
    print(mixed_set)

print('\nNow lets run a print over a while cycle\n')
counter = 0
while counter < 6:
    print(f"this is while cycle print number {counter}", mixed_set)
    counter += 1

print('\nNow lets command print by function\n')


def print_a_set_five_times(set_name):
    print("first print from function", set_name)
    print("second print from function", set_name)
    print("third print from function", set_name)
    print("fourth print from function", set_name)
    print("fifth print from function", set_name)


print_a_set_five_times(mixed_set)

print('\nNow lets command print by player input 5 times over for cycle\n')

for i in range (5):
    a = input("Enter y to print the set: ")
    if a == "y":
        print("printed by command", mixed_set)

And here is what I got as results. Order is the same throughout the single run (copypaste from PyCharm):

check the class of mixed_set:  <class 'set'>

check the length of mixed_set:  21 

first print:  {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
second print:  {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
third print:  {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
fourth print:  {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
fifth print: {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}

 Lets run a print over a for cycle

{0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
{0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
{0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
{0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
{0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}

Now lets run a print over a while cycle

this is while cycle print number 0 {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
this is while cycle print number 1 {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
this is while cycle print number 2 {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
this is while cycle print number 3 {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
this is while cycle print number 4 {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
this is while cycle print number 5 {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}

Now lets command print by function

first print from function {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
second print from function {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
third print from function {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
fourth print from function {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
fifth print from function {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}

Now lets command print by player input 5 times over for cycle

Enter y to print the set: y
printed by command {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
Enter y to print the set: y
printed by command {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
Enter y to print the set: y
printed by command {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
Enter y to print the set: y
printed by command {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}
Enter y to print the set: y
printed by command {0.48, 1, '-67', 'London', 'August', '1', 12113, 'delta', 'Paris', 'alef', 'Tiberivs', 'gelato', '222', 'bet', 'Berlin', 'Seneca', 'gimel', 'alpha', 'gamma', 'Volperting', 'beta'}

Process finished with exit code 0
1 Answers

I think set may change order only when you modify content in set - when you create set, add new item to set, etc. (when it has to find place for new element(s)). But rather not when you remove item from set.

print() doesn't change content in set so every print will display the same order.

You create set only once - at start - and later you use different methods to display it.
But this doesn't change content in set.

Because you use the same computer, the same Python, and the same code - so you may always get the same results.


But when I run the same code

mixed_set = {1, "1", "alef", "bet", "gimel", "alpha", "beta", "gamma", "delta", "delta", "gelato", "London", "Paris", '222', 0.48, "-67", 12113, "Berlin", "Volperting", "August", "Seneca", "Tiberivs"}
print(mixed_set)

with different Pythons then I get different orders

Python 3.10:

{0.48, 1, 'Berlin', 'Seneca', 'delta', 12113, 'Paris', 'Tiberivs', '-67', 'alef', 'alpha', '222', 'gamma', 'Volperting',  'gelato', 'London', 'bet', 'beta', 'gimel', '1', 'August'}

Python 3.8

{0.48, 1, '222', 'Seneca', 'beta', 'alpha', '1', 'gamma', 'August', 'Berlin', 'Volperting', 'alef', 'bet', 'gelato', 'Paris', 'London', 12113, 'gimel', 'delta', '-67', 'Tiberivs'}

bpython (on Python 3.10)

{0.48, 1, 'Volperting', 'Berlin', 'Seneca', '222', 'gamma', 'delta', 12113, '-67', 'Tiberivs', 'Paris', 'alpha', 'August', 'gelato', 'bet', '1', 'beta', 'gimel', 'alef', 'London'}

IDE Thonny (with Python 3.10)

{0.48, 1, 'alef', '1', 'gamma', 'Tiberivs', 'London', 'Seneca', 12113, 'delta', 'beta', 'Volperting', 'alpha', 'bet', 'August', 'gelato', '-67', '222', 'Berlin', 'Paris', 'gimel'}

And the same tools on your computer may give different orders.


EDIT:

After more tests I see I get different order even if I run the same code again with the same Python.
I run it on Linux.


And when I create set in different ways then sometimes I get different orders.

a = set({0.5})
a.add(0)
print(a)

b = set({0})
b.add(0.5)
print(b)

gives me

{0.5, 0}
{0, 0.5}

But the same code for values 0, 1 gives me always the same result {0, 1}.

Related