Table of contents
1.
Introduction
2.
Steps to draw a Polygon turtle
2.1.
Approach
2.2.
Code
2.2.1.
Input
2.2.2.
Output
2.2.3.
Input
2.2.4.
Output
2.2.5.
Input
2.2.6.
Output
3.
Frequently Asked Questions
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Polygon in Turtle Python

Introduction

Python has a built-in module called turtle. It allows us to draw any drawing using a turtle and the turtle module's methods, as well as some logical loops. The four methods offered in the turtle module are used to create turtle drawings. It is a helpful tool for learning Python's basic programming syntax. It can be used to teach children programming. It functions similarly to a canvas or easel board, allowing you to draw various figures by sending directions to the Turtle.

The turtle library in Python may be used to create different forms on a canvas. The programmer can direct a pen in a particular direction by specifying its heading and distance. In this article, we'll look at how to use the Turtle module to create various shaped Polygons. Any polygon shape can be simply drawn given the number of sides (x) and length of sides (l). Let's look at some examples to assist us to grasp it better.

Also see, Merge Sort PythonFibonacci Series in Python

Steps to draw a Polygon turtle

Approach

  • Import a turtle and create an object from it.
     
  • You may take the number of sides(x) and length(l) of the polygon by the user or by yourself.
     
  • Iterate the loop for x times.
     
  • Move forward turtle l length and turn it 360/x degree.

 

Code

# draw any polygon in turtle
# import turtle  
import turtle
  
# creating turtle object
ob = turtle.Turtle()
  
# Accepting the number of sides of the polygon as input
x = int(input("Enter the number of sides of the polygon"))
  
# Accepting the number of sides of the polygon as input
l = int(input("Enter the length of sides of the polygon"))
  
  
for _ in range(x):
    turtle.forward(l)
    turtle.right(360/x)
You can also try this code with Online Python Compiler
Run Code

 

Input

8

100

Output

Input

4

100

Output

Input

3

100

Output

You can practice by yourself with the help of online python compiler for better understanding.

You can also read about Palindrome Number in Python here.

Check out Escape Sequence in Python

Frequently Asked Questions

1. What is the purpose of the turtle in Python?
Turtle is a Python package that comes pre-installed that allows users to create drawings and shapes using a virtual canvas. The turtle is the name of the onscreen pen that you use to sketch with, and it is also the name of the library. 
 

2. In Python, how do you fill a turtle with a shape?

  • Use the turtle to fill a shape with a color. before drawing the shape, use the turtle.begin_fill() instruction.
     
  • After the shape has been drawn, use the turtle.end_fill() command.
     
  • The shape will be filled with the current fill color when the turtle.end_fill() instruction is executed.
     

3. In Python, what does the turtle represent?
The turtle is both the name of the onscreen pen that you use to draw with and the name of the library. In brief, the Python turtle library is a fun and engaging approach for new programmers to develop a feel for Python programming. Turtle is mostly used to introduce children to computer technology.
 

4. Is turtle a pre-installed library?
Yes, turtle comes in the standard python package, so it does not need to be downloaded explicitly. You can import it directly into your program.
 

5. What is the default shape of the turtle?
The string must be the name of a shape that is currently specified. The default shape for all turtles in new models is "default."

Conclusion

In this article, we draw different types of polygons like squares and triangles using turtle in python. We hope that this blog will help you understand the concept of the turtle library and one of the applications, i.e., to draw the different types of shapes; if you like to learn more about it, check out our other blogs Python libraries.

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass