10 Creative and Mind-Blowing Python Turtle Examples to Inspire Your Next Project

Here are ten more Python turtle examples that you can try out:


1. Drawing a Rectangle:


import turtle

t = turtle.Turtle()

for i in range(2):
t.forward(100)
t.left(90)
t.forward(50)
t.left(90)


2. Drawing a Snowflake:

import turtle

t = turtle.Turtle()

def snowflake(length, levels):
if levels == 0:
t.forward(length)
return
length /= 3.0
snowflake(length, levels-1)
t.left(60)
snowflake(length, levels-1)
t.right(120)
snowflake(length, levels-1)
t.left(60)
snowflake(length, levels-1)

for i in range(3):
snowflake(150, 4)
t.right(120)
turtle.exitonclick()



3. Drawing a Heart:

import turtle

t = turtle.Turtle()

t.fillcolor('red')
t.begin_fill()
t.left(45)
t.forward(100)
t.circle(50, 180)
t.right(90)
t.circle(50, 180)
t.forward(100)
t.end_fill()

turtle.exitonclick()



4. Drawing a Spiral Square:
import turtle

t = turtle.Turtle()

def spiral_square(length, angle, multiplier):
if length > 0:
t.forward(length)
t.right(angle)
spiral_square(length*multiplier, angle, multiplier)

spiral_square(10, 90, 1.03)
turtle.exitonclick()



5. Drawing a Koch Snowflake:
import turtle

t = turtle.Turtle()

def koch(length, depth):
if depth == 0:
t.forward(length)
return
length /= 3.0
koch(length, depth-1)
t.left(60)
koch(length, depth-1)
t.right(120)
koch(length, depth-1)
t.left(60)
koch(length, depth-1)

for i in range(3):
koch(200, 4)
t.right(120)
turtle.exitonclick()



6. Drawing a Tree:
import turtle

t = turtle.Turtle()

def tree(branch_len):
if branch_len > 5:
t.forward(branch_len)
t.right(20)
tree(branch_len-15)
t.left(40)
tree(branch_len-15)
t.right(20)
t.backward(branch_len)

t.left(90)
t.penup()
t.backward(100)
t.pendown()
t.color('green')
tree(75)
turtle.exitonclick()


7. Drawing a Spirograph:

import turtle
import math

t = turtle.Turtle()

def spirograph(R, r, l):
k = r / R
for i in range(360):
t.penup()
t.setpos(R * math.cos(i * math.pi / 180), R * math.sin(i * math.pi / 180))
t.pendown()
theta = math.pi * i / 180
x = (R - r) * math.cos(theta) + l * math.cos(k * theta - theta)
y = (R - r) * math.sin(theta) - l * math.sin(k * theta - theta)
t.setpos(x, y)

spirograph(100, 30, 50)
turtle.exitonclick()


8. Drawing a star:
import turtle

my_turtle = turtle.Turtle()

for i in range(5):
my_turtle.forward(100)
my_turtle.right(144)

turtle.done()
turtle.exitonclick()

9. Drawing a hexagon:
    
import turtle

my_turtle = turtle.Turtle()

for i in range(6):
my_turtle.forward(100)
my_turtle.right(60)

turtle.done()

10. Drawing a flower:


import turtle

my_turtle = turtle.Turtle()

for i in range(36):
my_turtle.right(10)
my_turtle.forward(50)
my_turtle.right(40)
my_turtle.forward(50)
my_turtle.right(140)
my_turtle.forward(50)
my_turtle.right(40)
my_turtle.forward(50)
my_turtle.right(110)

turtle.done()

Post a Comment

© T and P Knowledge. All rights reserved. Developed by Jago Desain