Creating sublayers with python

I am still trying to get a full understanding of string formatting and the use of {}…
one thing I cannot figure out is why the curly brackets change the order of numbers…?

a=5
b=4
c=2
d=6
(a**b)
625
{a,b,c,d}
{2, 4, 5, 6}
[a,b,c,d]
[5, 4, 2, 6]
(a,b,c,d)
(5, 4, 2, 6)

It’s only string formatting if the curly braces are between a pair of double or single quotes, meaning part of a string.

pi = 3.14
message = "The constant pi is equal to {}".format(pi)

print message  # The constant pi is equal to 3.14

Here the pair of curly braces is a placeholder that indicates, where the variable pi will be inserted into the message variable by the string method .format().

a=5
b=4
c=2
d=6
print a**b  # 625

Here symbols a to d are integer variables. a**b is the same as doing a^b = 5^4 = 625.

foo = {a,b,c,d}  
print foo  # set([2, 4, 5, 6])

This should create a set of unique, sorted values, not to be confused with a dictionary, which also uses curly braces, but consists of key-value-pairs, instead of individual values.

foo = [a,b,c,d]
print foo  # [a,b,c,d]

Here you create an ordinary list of values.

foo = (a,b,c,d)
print foo  # (5, 4, 2, 6)

This is called a tuple.

To understand how these complex types (list, dict, set, etc.) can be output as strings and thus printed, you first need to understand what custom types are and how you can override their __repr__() method, which is in charge of the representation of the type and thus how it’s output as a string, when you print it.

Let’s say you want to have a custom Point type with parameters x, y, and z.

class Point:
    def __init__(self, x, y, z):
        self.x, self.y, self.z = x, y, z
        

pt = Point(0, 1, -1)  # creates a new Point object, an instance of the Point class
print pt  # <__main__.Point instance at 0x000000000000003F>

If you want a nicer print out, you can override the __repr__ method.

class Point:
    def __init__(self, x, y, z):
        self.x, self.y, self.z = x, y, z
    
    def __repr__(self):
        return "({}, {}, {})".format(self.x, self.y, self.z)  # custom output


pt = Point(0, 1, -1)
print pt  # (0, 1, -1)

As a beginner, this can be hard to understand, because you first need to get a grip on object oriented programming and classes.

thank you for the explanations…
I was surprised that simply writing the following in the console would give me the results without my asking to print and that the curly brackets would reorder the numbers…

foo = {2,5,3,8,1,9,4}
(foo)
{1, 2, 3, 4, 5, 8, 9}

most of what I am doing is with numbers in Rhino and GH so I am interested to know if there is a way to tell what is inside {} to be ordered in different ways not just from min to max ?or even simply from max to min.

foo = {a,b,c,d}  
print foo  # set([2, 4, 5, 6])

As already mentioned above, this syntax creates a set of sorted, unique values (cf. docs).

If you don’t need set functionality, simply use a list instead.

foo = [a,b,c,d]
print foo  # [5,4,2,6]

The items are listed in the order that they were inserted.

You can sort lists however you like.
If you want ascending order, simply do this:

foo.sort()
print foo  # [2,4,5,6]

Or this for descending order:

foo.sort(reverse=True)
print foo  # [6,5,4,2]