{"id":19026,"date":"2017-03-01T06:55:28","date_gmt":"2017-03-01T06:55:28","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=19026"},"modified":"2021-03-20T14:55:35","modified_gmt":"2021-03-20T14:55:35","slug":"how-to-draw-a-square-using-turtle-graphics","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-draw-a-square-using-turtle-graphics\/","title":{"rendered":"How to draw a Square using Turtle Graphics"},"content":{"rendered":"<h3>Overview<\/h3>\n<p>In this post, we will draw a square using the Turtle graphics python program. We will learn to define a function to draw the square and invoke the function from the main.<\/p>\n<p>Import Turtle<\/p>\n<p>To import the turtle module, we can use the following statement:<\/p>\n<p>import turtle<\/p>\n<p>Create a turtle screen and a turtle object:<\/p>\n<pre>WindowTurtleScreen=turtle.Screen()\r\n\r\ntDocsTurtle = turtle.Turtle()<\/pre>\n<h3>Program<\/h3>\n<pre>#Import turtle module \r\nimport turtle\r\n\r\n#############################################\r\n# Function to draw square                   #  \r\n# using Turtle graphics                     #\r\n#############################################\r\ndef TurtleSquare(tDocsTurtle,sideLength):\r\n\r\n    tDocsTurtle.penup()\r\n    tDocsTurtle.goto(-100, -100) #just to center\r\n    tDocsTurtle.pendown()\r\n   \r\n    #Draw Square Four sides\r\n    for i in range(4):\r\n        # Draw side\r\n        tDocsTurtle.forward(sideLength)\r\n        # Turn 90 degrees\r\n        tDocsTurtle.left(90) \r\n\r\n   \r\n    \r\n########################################\r\n# Main function                        #\r\n########################################\r\ndef main():\r\n    WindowTurtleScreen=turtle.Screen()\r\n    tDocsTurtle = turtle.Turtle()\r\n    WindowTurtleScreen.title(\"Square Turtle Program - www.TestingDocs.com\")\r\n    TurtleSquare(tDocsTurtle,300) \r\n    WindowTurtleScreen.exitonclick()\r\n    \r\n\r\nmain()\r\n<\/pre>\n<h3>Sample Output<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-19048 size-full\" title=\"Turtle Draw Square Program\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Draw-Square-Program.png\" alt=\"Turtle Draw Square Program\" width=\"1692\" height=\"1017\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Draw-Square-Program.png 1692w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Draw-Square-Program-300x180.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Draw-Square-Program-1024x615.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Draw-Square-Program-768x462.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Draw-Square-Program-1536x923.png 1536w\" sizes=\"auto, (max-width: 1692px) 100vw, 1692px\" \/><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-19026\" data-postid=\"19026\" class=\"themify_builder_content themify_builder_content-19026 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview In this post, we will draw a square using the Turtle graphics python program. We will learn to define a function to draw the square and invoke the function from the main. Import Turtle To import the turtle module, we can use the following statement: import turtle Create a turtle screen and a turtle [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[211],"tags":[],"class_list":["post-19026","post","type-post","status-publish","format-standard","hentry","category-python","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=19026"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19026\/revisions"}],"predecessor-version":[{"id":19596,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19026\/revisions\/19596"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=19026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=19026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=19026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}