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