Broadcasting in NumPy


 #Broadcasting
import numpy as np
# We will add,multiply, subtract  the vector v to each row of the matrix x,
x = np.array([[1,2,3], [4,5,6], [7,8,9]])
v = np.array([1, 0, 1])
print(x)
print(v)                 
y = x + v
y1= x * v
y2= x - v
print("Addition\n",y,"\n","Multiplication \n",y1,"\n","Subtraction \n",y2) 

Drawing a H-Tree Fractal using turtle in python program


import turtle
SPEED = 8
BG_COLOR = "red"
PEN_COLOR = "lightgreen"
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
DRAWING_WIDTH = 500
DRAWING_HEIGHT = 500
PEN_WIDTH = 5
TITLE = "H-Tree Fractal"
FRACTAL_DEPTH = 3
def draw_line(tur, pos1, pos2):
    tur.penup()
    tur.goto(pos1[0], pos1[1])
    tur.pendown()
    tur.goto(pos2[0], pos2[1])

def recursive_draw(tur, x, y, width, height, count):
    draw_line(
        tur,
        [x + width * 0.25, height // 2 + y],
        [x + width * 0.75, height // 2 + y],
    )
    draw_line(
        tur,
        [x + width * 0.25, (height * 0.5) // 2 + y],
        [x + width * 0.25, (height * 1.5) // 2 + y],
    )
    draw_line(
        tur,
        [x + width * 0.75, (height * 0.5) // 2 + y],
        [x + width * 0.75, (height * 1.5) // 2 + y],
    )
    if count <= 0: 
        return
    else: 
        count -= 1
    
        recursive_draw(tur, x, y, width // 2, height // 2, count)
        recursive_draw(tur, x + width // 2, y, width // 2, height // 2, count)
       recursive_draw(tur, x, y + width // 2, width // 2, height // 2, count)
       recursive_draw(tur, x + width // 2, y + width // 2, width // 2, height // 2, count)

if __name__ == "__main__":
    # Screen setup
    screen = turtle.Screen()
    screen.setup(SCREEN_WIDTH, SCREEN_HEIGHT)
    screen.title(TITLE)
    screen.bgcolor(BG_COLOR)
    # Turtle artist (pen) setup
    artist = turtle.Turtle()
    artist.hideturtle()
    artist.pensize(PEN_WIDTH)
    artist.color(PEN_COLOR)
    artist.speed(SPEED)
   
    recursive_draw(artist, - DRAWING_WIDTH / 2, - DRAWING_HEIGHT / 2, DRAWING_WIDTH, DRAWING_HEIGHT, FRACTAL_DEPTH)
   
    turtle.done()

Drawing a fractal tree using turtle in python program


 import turtle
MINIMUM_BRANCH_LENGTH = 15
def build_tree(t, branch_length, shorten_by, angle):
  if branch_length > MINIMUM_BRANCH_LENGTH:
    t.forward(branch_length)
    new_length = branch_length - shorten_by
    t.left(angle)
    build_tree(t, new_length, shorten_by, angle)
    t.right(angle * 2)
    build_tree(t, new_length, shorten_by, angle)
    t.left(angle)
    t.backward(branch_length)
tree = turtle.Turtle()
tree.hideturtle()
tree.setheading(90)
tree.color('red')
build_tree(tree, 50, 5, 30)
turtle.mainloop()