Learning python

Here’s some example code for Python Mode:

from random import shuffle
def setup():
    size(640, 640)
    noLoop()
    background(239)
    rectMode(CENTER)
    fill(255, 255, 255, 127)
    strokeWeight(2)
    stroke(0, 0, 127)
    
def draw():
    translate(width / 2, height / 2)
    CompositeSquare(500, 500, 4).render()
    
class CompositeSquare:
    def __init__(self, w, h, level):
        self.w = w
        self.h = h
        self.level = level
    def render(self):
        push()
        rotate(random(PI / 4) - PI / 8)
        rect(0, 0, self.w, self.h)
        if self.level > 1:
            coeffs = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
            shuffle(coeffs)
            for i in range(4):
                push()
                translate(coeffs[i][0] * self.w / 4, coeffs[i][1] * self.h / 4)
                CompositeSquare(self.w / 2, self.h / 2, self.level - 1).render()
                pop()
        pop()

In Python, functions begin with def. Block headers end with a colon. Code contents are defined by their indentation. So, when the indentation decreases, we’ve left the code block.

For an example of a boolean expression, here’s an if block header:

        if self.level > 1: