Close Menu
    Trending
    • Enabling small language models to solve complex reasoning tasks | MIT News
    • New method enables small language models to solve complex reasoning tasks | MIT News
    • New MIT program to train military leaders for the AI age | MIT News
    • The Machine Learning “Advent Calendar” Day 12: Logistic Regression in Excel
    • Decentralized Computation: The Hidden Principle Behind Deep Learning
    • AI Blamed for Job Cuts and There’s Bigger Disruption Ahead
    • New Research Reveals Parents Feel Unprepared to Help Kids with AI
    • Pope Warns of AI’s Impact on Society and Human Dignity
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » Drawing Shapes with the Python Turtle Module
    Artificial Intelligence

    Drawing Shapes with the Python Turtle Module

    ProfitlyAIBy ProfitlyAIDecember 11, 2025No Comments10 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    , our primary understanding of the Python language is understanding that we will program features, 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 way extra highly effective than that; it may entry and course of recordsdata, create fascinating video games, simulate scientific ideas, course of large knowledge, generate machine studying fashions, and much more!

    On this article, we are going to use Python to create graphical outputs by utilizing 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 permits graphical outputs by means of code. The module supplies a pointer, which could be customised as a turtle (thus the identify), 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 customary library, which signifies that we shouldn’t have to put in the module ourselves. Its lessons and features can simply be accessed by means of the next import assertion:

    from turtle import *

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

    Turtle Fundamental Instructions

    As could be seen from the documentation linked above, the fundamental instructions that we may give 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() It will change the colour of the turtle in keeping with the string that has been handed to this operate as an argument (eg, “pink”). Colour names could be accessed from here.
    • width() It will change the width of the pointer by the integer worth
    • exitonclick() It will 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 in keeping with your necessities to grasp what the turtle does when every of the above features is known as.

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

    Utilizing OOP Turtle Graphics

    Now that now we have seen how the fundamental features 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 means of the constructor, brings ease when programming enormous complicated applications with variables having related parameters however completely 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.

    In an effort to proceed forward, it is very important have a primary degree 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 screen
    
    my_turtle = Turtle()
    print(my_turtle)
    
    Turtle Object created (Picture by Writer)

    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 are going to 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 screen()
    display.title('Drawing Shapes with Turtle Module')
    display.bgcolor("white")
    display.exitonclick()
    Turtle Object and Display screen Objects Customised (Picture by Writer)

    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 aim of this tutorial!

    Drawing Shapes

    We are going to now discover ways to draw completely different shapes with our customised turtle!

    Triangle

    The primary form that we’ll draw is a triangle. This may be drawn utilizing the fundamental features 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 all sides is 60. We are able to draw this triangle utilizing the next traces 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 Writer)

    As could be seen, now we have efficiently created a triangle with the assistance of our turtle. Discover that now we have set the angle by which the turtle strikes to the proper to 120, so that may imply the remaining angle that may turn out to be the inner angle of the triangle will probably be 180 – 120 = 60. This had been our aim. We are going to work equally for the subsequent 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 instances, with the angle set as 360/4 = 90º each 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 Writer)

    Pentagon

    Subsequent, we are going to create a pentagon, which is a 5-sided form with the angle between all sides equal to 108. Because of this the outside angle will probably be equal to 72. We are going to code the above into our code, this time utilizing 5 traces of code for the 5 sides. We can even 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 Writer)

    As you may see within the code block above, now we have decreased the ahead motion from 200 to 150 in order that the pentagon could be drawn inside the display.

    Constructing the Algorithm

    We have now used the turtle module to attract a triangle, a sq., and a pentagon. As we will see from the above, we will 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 instances, that’s, as many sides as the form has. And equally for a pentagon. Thus, we will set up a sample of recurring ahead and proper features. We are able to set a set worth of the space coated each time the turtle strikes ahead. As for the angle given to the proper technique, similar to the variety of instances the statements are repeated relies on 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 method:

    Exterior Angle = 360 / Variety of Sides

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

    Defining the Perform

    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’s going to create a triangle. If we give 8 as an argument, it’s going to create an octagon, and many others.

    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 instances as there are sides. So, suppose we wish to draw an octagon, we are going to name the operate and provides the quantity 8 as an argument to the operate. We might also outline the colour of the form:

    my_turtle.shade("pale violet crimson")
    draw_shapes(8)
    Pale Violet Purple Octagon (Picture by Writer)

    Drawing Shapes inside a Vary

    Now we are going to use the above operate now we have outlined, and use the for loop to loop by means of a spread of numbers, every comparable to a facet. We are going to 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 many others as much as a decagon, we are going to loop by means of the numbers 3 to 11, as 11 is excluded within the vary of a for loop.

    Allow us to additionally add the supply of drawing every form with a distinct shade. For that, we are going to create an inventory of colours, and the for loop can even loop by means 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 now we have created our listing of colours, we are going to modify our operate to incorporate the color-changing function, after which loop by means 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, now we have explored the turtle module, understood its primary features and OOP idea, and used it to create shapes. This was an intermediate-level Python tutorial that required a primary degree understanding of lessons and objects, defining and calling features, in addition to customizing colours with the assistance of Trinket. It is a basic-level instance of what might be accomplished with the Turtle module. We are able to discover the module additional and study lots of coding by means of it!



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleMeta Acquires AI Wearable Startup Limitless. What Does This Mean for User Privacy?
    Next Article The Machine Learning “Advent Calendar” Day 11: Linear Regression in Excel
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    Enabling small language models to solve complex reasoning tasks | MIT News

    December 12, 2025
    Artificial Intelligence

    New method enables small language models to solve complex reasoning tasks | MIT News

    December 12, 2025
    Artificial Intelligence

    New MIT program to train military leaders for the AI age | MIT News

    December 12, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    IT as the new HR: Managing your AI workforce

    November 7, 2025

    Why We Should Focus on AI for Women

    July 2, 2025

    Graph Neural Networks Part 4: Teaching Models to Connect the Dots

    April 29, 2025

    Why the humanoid workforce is running late

    May 6, 2025

    From Tokens to Theorems: Building a Neuro-Symbolic AI Mathematician

    September 8, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    Top Rossum Alternatives for Document Processing in 2024

    April 5, 2025

    YOLOv1 Paper Walkthrough: The Day YOLO First Saw the World

    December 5, 2025

    The Rise of Semantic Entity Resolution

    September 14, 2025
    Our Picks

    Enabling small language models to solve complex reasoning tasks | MIT News

    December 12, 2025

    New method enables small language models to solve complex reasoning tasks | MIT News

    December 12, 2025

    New MIT program to train military leaders for the AI age | MIT News

    December 12, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

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