Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • Code Is Cheap. Engineering Judgement Is Now the Scarce Resource
    • Build a digital twin agent (with guardrails)
    • Robotiq Launches IQ to Make Palletizing Automation Faster and More Predictable
    • Leica Cine Compact 1: Premium 4K smart projector
    • Coach vs mentor – Who can help you level up your career?
    • Flush With Cash From OpenAI, Opal Is Making an AI-Powered Audio Gadget
    • Dozens of Red Hat packages backdoored through its official NPM channel
    • Microsoft Build 2026 Kicks Off Today: Live Updates on Copilot AI and Dev Tools
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Tuesday, June 2
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»Drawing Shapes with the Python Turtle Module
    Artificial Intelligence

    Drawing Shapes with the Python Turtle Module

    Editor Times FeaturedBy Editor Times FeaturedDecember 11, 2025No Comments9 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    , our primary understanding of the Python language is understanding that we are able to program capabilities, introduce iterations with the assistance of Python loops, and determine which program to execute with the assistance of conditional statements like if, elif, and else. However Python is much extra highly effective than that; it could possibly entry and course of information, create attention-grabbing video games, simulate scientific ideas, course of huge knowledge, generate machine studying fashions, and much more!

    On this article, we are going to use Python to create graphical outputs through the use of the Python module Turtle. It is a beginner-friendly tutorial that teaches how to attract shapes and program drawings utilizing Python. To proceed additional, it’s important to have a radical understanding of Python fundamentals: primary statements, loops, lessons and objects, and accessing modules.

    Python’s Turtle Module

    The turtle module in Python is a module that enables graphical outputs by way of code. The module offers a pointer, which could be customised as a turtle (thus the title), and the motion of the turtle leaves a path behind, drawing shapes and creating visible patterns on the display. The module is put in with the Python normal library, which signifies that we wouldn’t have to put in the module ourselves. Its lessons and capabilities can simply be accessed by way of the next import assertion:

    from turtle import *

    Now, allow us to dive deep into this module by way of the Python official documentation: https://docs.python.org/3/library/turtle.html

    Turtle Primary Instructions

    As could be seen from the documentation linked above, the fundamental instructions that we can provide to our turtle are the next:

    • ahead() This operate tells the turtle to maneuver ahead by a particular distance. It takes an integer worth as an argument.
    • backward()This operate tells the turtle to maneuver backward by a particular distance. It additionally takes an integer worth as an argument.
    • left()This tells the turtle to show to the left by a sure angle that has been handed to the operate as an argument.
    • proper()This tells the turtle to show to the proper by a sure angle that has been handed to the operate as an argument.
    • shade() This can change the colour of the turtle based on the string that has been handed to this operate as an argument (eg, “pink”). Coloration names could be accessed from here.
    • width() This can change the width of the pointer by the integer worth
    • exitonclick() This can enable us to shut the display that has been generated as an output to our code, simply by clicking on the display.

    Run the next code, and alter it based on your necessities to grasp what the turtle does when every of the above capabilities is named.

    from turtle import *
    shade("pink")
    width(5)
    ahead(100)
    left(45)
    
    shade("blue")
    width(4)
    ahead(100)
    left(45)
    ahead(20)
    
    shade("purple")
    width(4)
    ahead(60)
    left(45)
    backward(50)
    
    shade("yellow")
    width()
    ahead(100)
    proper(45)
    
    exitonclick()
    Turtle Primary Capabilities (Picture by Creator)

    Utilizing OOP Turtle Graphics

    Now that we’ve got seen how the fundamental capabilities within the module work and the way the output is generated, we are going to use the idea of Object Oriented Programming that’s defined within the documentation to additional our understanding. The idea of lessons and objects, created by way of the constructor, brings ease when programming big advanced applications with variables having comparable parameters however totally different values. That is the place OOP turns out to be useful, and its incorporation within the module will enable us to make use of a couple of turtle at a time.

    To be able to proceed forward, it is very important have a primary stage understanding of lessons and objects, particularly how objects could be created with their attrbitutes and strategies.

    Allow us to create our turtle and display objects:

    from turtle import Turtle, Display
    
    my_turtle = Turtle()
    print(my_turtle)
    
    Turtle Object created (Picture by Creator)

    As could be seen from the screenshot above, the turtle object has been created, and its location is outlined. Now we are going to use the documentation to customise our turtle.

    We’ll outline the form, shade, and width of our turtle utilizing the next code:

    my_turtle.form("turtle")
    my_turtle.shade("coral1")
    my_turtle.width(4)

    Allow us to additionally outline the display object and customise it.

    display = Display()
    display.title('Drawing Shapes with Turtle Module')
    display.bgcolor("white")
    display.exitonclick()
    Turtle Object and Display Objects Customised (Picture by Creator)

    The display object has been outlined above, and its title and background colours have been set accordingly. Now, allow us to transfer in direction of the primary purpose of this tutorial!

    Drawing Shapes

    We’ll now learn to draw totally different shapes with our customised turtle!

    Triangle

    The primary form that we are going to draw is a triangle. This may be drawn utilizing the fundamental capabilities that we mentioned above: ahead and proper. We all know {that a} triangle consists of three sides, and for an equilateral triangle, the angle between either side is 60. We are able to draw this triangle utilizing the next strains of code:

    my_turtle.ahead(200)
    my_turtle.proper(120)
    my_turtle.ahead(200)
    my_turtle.proper(120)
    my_turtle.ahead(200)
    my_turtle.proper(120)
    Triangle (Picture by Creator)

    As could be seen, we’ve got efficiently created a triangle with the assistance of our turtle. Discover that we’ve got set the angle by which the turtle strikes to the proper to 120, so that might imply the remaining angle that might turn into the interior angle of the triangle might be 180 – 120 = 60. This had been our purpose. We’ll work equally for the following form.

    Sq.

    Now we are going to use our turtle to attract a sq.. Since a sq. has 4 sides, we are going to use the ahead motion technique 4 occasions, with the angle set as 360/4 = 90º every time we’re utilizing the proper technique. Allow us to additionally change the colour of the turtle to darkish turquoise (Turtle Colors)

    Right here is our code to attract a sq.:

    my_turtle.shade("darkish turquoise")
    my_turtle.ahead(200)
    my_turtle.proper(90)
    my_turtle.ahead(200)
    my_turtle.proper(90)
    my_turtle.ahead(200)
    my_turtle.proper(90)
    my_turtle.ahead(200)
    my_turtle.proper(90)
    Sq. (Picture by Creator)

    Pentagon

    Subsequent, we are going to create a pentagon, which is a 5-sided form with the angle between either side equal to 108. Because of this the outside angle might be equal to 72. We’ll code the above into our code, this time utilizing 5 strains of code for the 5 sides. We may also change the colour of our turtle.

    my_turtle.shade("spring inexperienced")
    my_turtle.ahead(150)
    my_turtle.proper(72)
    my_turtle.ahead(150)
    my_turtle.proper(72)
    my_turtle.ahead(150)
    my_turtle.proper(72)
    my_turtle.ahead(150)
    my_turtle.proper(72)
    my_turtle.ahead(150)
    my_turtle.proper(72)
    Pentagon (Picture by Creator)

    As you possibly can see within the code block above, we’ve got decreased the ahead motion from 200 to 150 in order that the pentagon could be drawn throughout the display.

    Constructing the Algorithm

    We now have used the turtle module to attract a triangle, a sq., and a pentagon. As we are able to see from the above, we are able to simply detect a sample. The turtle strikes ahead and proper as many sides as there are. For drawing a triangle, thus a three-sided form, the turtle strikes ahead, then proper, then ahead, then proper, then once more ahead and once more proper, a complete of three units of ahead and proper. For a sq., the identical set is executed 4 occasions, that’s, as many sides as the form has. And equally for a pentagon. Thus, we are able to set up a sample of recurring ahead and proper capabilities. We are able to set a hard and fast worth of the space lined every time the turtle strikes ahead. As for the angle given to the proper technique, identical to the variety of occasions the statements are repeated is dependent upon the variety of sides within the form, equally, the angle can also be decided by the variety of sides. This exterior angle can simply be calculated by the next components:

    Exterior Angle = 360 / Variety of Sides

    The outside angle for a triangle might be 360/3 = 120. The outside angle for a sq. might be 360/4 = 90, and so forth and so forth. This might be used to feed the proper technique.

    Defining the Operate

    Now we are going to outline a generic operate that takes the variety of sides of a form to attract the form. If we give 3 as an argument, it can create a triangle. If we give 8 as an argument, it can create an octagon, and so forth.

    def draw_shapes(num_sides):
        angle = 360 / num_sides
        for i in vary(num_sides):
            my_turtle.ahead(50)
            my_turtle.proper(angle)

    The operate takes within the variety of sides, calculates the outside angle, which is fed to the proper technique, and executes the ahead and proper technique the variety of occasions as there are sides. So, suppose we need to draw an octagon, we are going to name the operate and provides the quantity 8 as an argument to the operate. We may additionally outline the colour of the form:

    my_turtle.shade("pale violet purple")
    draw_shapes(8)
    Pale Violet Crimson Octagon (Picture by Creator)

    Drawing Shapes inside a Vary

    Now we are going to use the above operate we’ve got outlined, and use the for loop to loop by way of a spread of numbers, every equivalent to a facet. We’ll begin with 3 for a triangle, and our turtle will draw as many shapes as we wish. So, suppose we wish to draw a triangle, a sq., a pentagon, and so forth as much as a decagon, we are going to loop by way of the numbers 3 to 11, as 11 is excluded within the vary of a for loop.

    Allow us to additionally add the availability of drawing every form with a special shade. For that, we are going to create an inventory of colours, and the for loop may also loop by way of the colours within the listing.

    my_colors = ["dark gray", "hot pink", "midnight blue", "orange", "indigo", "dark sea green", "tan", "pale violet red", "sky blue", "spring green"]
    

    As soon as we’ve got created our listing of colours, we are going to modify our operate to incorporate the color-changing characteristic, after which loop by way of the vary to attract shapes.

    def draw_shapes(num_sides):
        angle = 360 / num_sides
        my_turtle.shade(my_colors[3-num_sides])
        for i in vary(num_sides):
            my_turtle.ahead(75)
            my_turtle.proper(angle)
    
    for shape_size_n in vary(3,11):
        draw_shapes(shape_size_n)
    Drawing Colourful Shapes with Turtle

    Conclusion

    On this tutorial, we’ve got explored the turtle module, understood its primary capabilities and OOP idea, and used it to create shapes. This was an intermediate-level Python tutorial that required a primary stage understanding of lessons and objects, defining and calling capabilities, in addition to customizing colours with the assistance of Trinket. It is a basic-level instance of what may very well be achieved with the Turtle module. We are able to discover the module additional and be taught numerous coding by way of it!



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    Code Is Cheap. Engineering Judgement Is Now the Scarce Resource

    June 2, 2026

    From Regex to Vision Models: Which RAG Technique Fits Which Problem

    June 2, 2026

    Escaping the Valley of Choice in BI

    June 2, 2026

    Ensuring Data Integrity with Cryptographic Hashing and the Ethereum Blockchain

    June 1, 2026

    RAG Is Not Machine Learning, and the ML Toolkit Solves the Wrong Problem

    June 1, 2026

    How to Combine Claude Code and Codex for Maximum Coding Power

    June 1, 2026

    Comments are closed.

    Editors Picks

    Code Is Cheap. Engineering Judgement Is Now the Scarce Resource

    June 2, 2026

    Build a digital twin agent (with guardrails)

    June 2, 2026

    Robotiq Launches IQ to Make Palletizing Automation Faster and More Predictable

    June 2, 2026

    Leica Cine Compact 1: Premium 4K smart projector

    June 2, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    ClearScore strengthens mortgage strategy with acquisition of fellow London-based Acre Platforms

    January 23, 2026

    ‘Mario Kart World’ Races Are More Chaotic—and Better—Than Ever

    June 3, 2025

    Pornhub to restrict access for UK users from February

    January 27, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

    Type above and press Enter to search. Press Esc to cancel.