Python Turtle

1. Draw a red colour square using Turtle.

import turtle t = turtle.Turtle() t.color("red") for i in range(4): t.forward(100) t.right(90)

2. Draw a triangle using Turtle.

import turtle t = turtle.Turtle() for i in range(3): t.forward(100) t.left(120) turtle.done()

3. Draw a circle using Turtle.

import turtle t = turtle.Turtle() t.color("blue") t.circle(100) turtle.done()

4. Draw a rectangle using Turtle.

import turtle t = turtle.Turtle() for _ in range(2): t.forward(200) t.right(90) t.forward(100) t.right(90) turtle.done()

5. Draw a star using Turtle.

import turtle t = turtle.Turtle() for _ in range(5): t.forward(100) t.right(144) turtle.done()

5. Python program to draw polygons.

import turtle t = turtle.Turtle() # Number of sides and length of each side sides = 3 # Try 3 for triangle, 4 for square, 5 for pentagon, etc. length = 100 # Angle between sides angle = 360 / sides # Draw polygon for _ in range(sides): t.forward(length) t.left(angle) turtle.done()

6. Draw a house using Turtle with different colors.

import turtle t = turtle.Turtle() t.speed(3) t.pensize(2) # Start lower on the screen t.penup() t.goto(-100, -100) t.pendown() # House Base (Blue Rectangle) t.fillcolor("skyblue") t.begin_fill() for length in [200, 150, 200, 150]: t.forward(length) t.left(90) t.end_fill() # Roof (Red Triangle) t.penup() t.goto(-100, 50) t.pendown() t.fillcolor("tomato") t.begin_fill() for _ in range(3): t.forward(200) t.left(120) t.end_fill() # Door (Brown Rectangle) t.penup() t.goto(-25, -100) t.pendown() t.fillcolor("saddlebrown") t.begin_fill() for length in [50, 80, 50, 80]: t.forward(length) t.left(90) t.end_fill() # Window (Yellow Square) t.penup() t.goto(30, -20) t.pendown() t.fillcolor("gold") t.begin_fill() for _ in range(4): t.forward(40) t.left(90) t.end_fill() t.hideturtle() turtle.done()

7. Use penup() and pendown() to draw two separate shapes.

import turtle t = turtle.Turtle() t.pensize(3) # Shape 1: Red Square t.color("red") t.begin_fill() for _ in range(4): t.forward(100) t.left(90) t.end_fill() # Lift pen and move further right to avoid overlap t.penup() t.goto(200, 0) t.pendown() # Shape 2: Blue Circle t.color("blue") t.begin_fill() t.circle(50) t.end_fill() t.hideturtle() turtle.done()

8. Fill a shape with color using fillcolor().

import turtle t = turtle.Turtle() t.pensize(3) # Set line color and fill color t.color("black") t.fillcolor("cyan") # Start filling t.begin_fill() # Draw shape (Triangle) for _ in range(3): t.forward(120) t.left(120) # End filling t.end_fill() t.hideturtle() turtle.done()

9. Draw multiple shapes in a pattern using a loop.

import turtle t = turtle.Turtle() t.speed(0) colors = ["red", "purple", "blue", "green", "orange", "yellow"] # Loop to draw a pattern of overlapping squares for i in range(36): t.color(colors[i % 6]) for _ in range(4): t.forward(100) t.left(90) t.left(10) # Rotate slightly after each square t.hideturtle() turtle.done()

10. Change the turtle’s shape and draw a simple design.

import turtle t = turtle.Turtle() t.shape("circle") # Change shape (arrow, turtle, circle, square, triangle) t.color("red") t.speed(3) for _ in range(6): t.circle(60) t.left(60) turtle.done()
Back