{"id":14903,"date":"2021-04-24T16:37:52","date_gmt":"2021-04-24T16:37:52","guid":{"rendered":"https:\/\/www.askpython.com\/?p=14903"},"modified":"2021-05-05T14:46:45","modified_gmt":"2021-05-05T14:46:45","slug":"python-turtle","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-turtle","title":{"rendered":"Python Turtle: Say hello to the Turtle of the coding world!"},"content":{"rendered":"\n<p>Hello fellow learner! Today you will be introduced to Python Turtle, the turtle of the coding world who is used to create fun drawings and shapes on your screen with just some simple steps. <\/p>\n\n\n\n<p>Let&#8217;s Begin!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Introduction to the Python Turtle Library<\/strong><\/h2>\n\n\n\n<p><strong>Python Turtle<\/strong> helps users interact with the programming language better by drawing various things on a virtual canvas. <\/p>\n\n\n\n<p>It makes use of a virtual pen as well known as a <strong>turtle<\/strong>. <\/p>\n\n\n\n<p>One can draw and make different shapes and pictures with the help of the python turtle library. Mostly the turtle library is used to draw shapes, create designs, and make images. But it can also be helpful in creating mini-games and animations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Getting Started with Python Turtle<\/strong><\/h2>\n\n\n\n<p>You need to import the <code>turtle<\/code> library in order to use its methods and functionalities. It also comes with a Python standard kit and hence, does not require additional installation. <\/p>\n\n\n\n<p>The next step involves creating the virtual canvas to draw the various objects on. We can name the canvas\/screen according to our needs and interests and displaying it. The code below does the creation and displaying of the screen for the user.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; first-line: 1; gutter: true; title: ; notranslate\" title=\"\">\n#Importing Module\nimport turtle as tur\n\n# Creating Screen\nwind = tur.Screen()\n\n# Displaying Screen\nsc = tur.getscreen()\ntur.mainloop()\n<\/pre><\/div>\n\n\n<p>The output of the code mentioned above results in the screen which is displayed below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_turtle_window.png\" alt=\"Initial Python Turtle Window\" class=\"wp-image-14915\" width=\"830\" height=\"734\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_turtle_window.png 961w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_turtle_window-300x265.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_turtle_window-768x679.png 768w\" sizes=\"auto, (max-width: 830px) 100vw, 830px\" \/><figcaption>Initial Turtle Window<\/figcaption><\/figure><\/div>\n\n\n\n<p>One can see the output of the code on this screen and the little black triangular shape in the middle of the screen is called the turtle which can be used to draw the required shapes and objects.<\/p>\n\n\n\n<p>First, one needs to create the turtle and then use the position functionalities on the turtle. Turtle supports four basic movements namely forward, backward, left, and right.  The forward and backward function needs the distance as a parameter on the other hand the left and right function need an angle of turning as a parameter. The code below helps to display the basic movement operations on a turtle. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport turtle\n\n# Creating Screen\nmy_window = turtle.Screen()\n\n# Creating turtle to draw\nmy_pen = turtle.Turtle()      \n\n# Moving Forward\nmy_pen.forward(150)           \n# Turning Right\nmy_pen.right(40)\n# Moving Forward\nmy_pen.forward(150)\n#Moving Left\nmy_pen.left(90)\n#Moving Backward\nmy_pen.backward(30)\n\n# Displaying Window\nmy_window.mainloop()\n<\/pre><\/div>\n\n\n<p>The output of the code is displayed in the picture below.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"961\" height=\"850\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Basic_turtle_movement_visualization.png\" alt=\"Basic Python Turtle Movement Visualization\" class=\"wp-image-14921\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Basic_turtle_movement_visualization.png 961w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Basic_turtle_movement_visualization-300x265.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Basic_turtle_movement_visualization-768x679.png 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><figcaption>Basic Turtle Movement Visualization<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Shapes using Python Turtle<\/strong><\/h2>\n\n\n\n<p>Using the basic functionalities of the turtle movements one can create some basic shapes such as triangle, square, and rectangle. One can also create shapes like a star.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Creating Pentagon<\/strong> with Turtle<\/h3>\n\n\n\n<p>The code below displays a pentagon on the screen with the help of a Python Turtle. For a regular pentagon, all edges have equal length and all angles are equal to 72 degrees.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport turtle\n\n# Creating Screen\nmy_window = turtle.Screen()\n\n# Creating turtle to draw\nmy_pen = turtle.Turtle()      \n\n#Creating a Pentagon\nfor i in range(5):\n    my_pen.forward(150)\n    my_pen.left(72)\n\n# Displaying Window\nmy_window.mainloop()\n<\/pre><\/div>\n\n\n<p>The picture below shows the output of the code above resulting in a pentagon.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"961\" height=\"850\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_pentagon_using_Turtle.png\" alt=\"Making Pentagon Using Turtle\" class=\"wp-image-14928\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_pentagon_using_Turtle.png 961w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_pentagon_using_Turtle-300x265.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_pentagon_using_Turtle-768x679.png 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><figcaption>Making Pentagon Using Turtle<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Creating a Star<\/strong> with Turtle<\/h3>\n\n\n\n<p>The code below displays a star on the screen with the help of a turtle. For a regular star shape, all edges have equal length and all angles are equal to 144 degrees. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport turtle\n\n# Creating Screen\nmy_window = turtle.Screen()\n\n# Creating turtle to draw\nmy_pen = turtle.Turtle()      \n\n#Creating a Star Shape\nfor i in range(5):\n    my_pen.forward(200)\n    my_pen.right(144)\n\n# Displaying Window\nmy_window.mainloop()\n<\/pre><\/div>\n\n\n<p>The picture below shows the output of the code above resulting in a star shape.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"961\" height=\"850\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_Star_using_Turtle.png\" alt=\"Making Star Using Turtle\" class=\"wp-image-14933\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_Star_using_Turtle.png 961w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_Star_using_Turtle-300x265.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_Star_using_Turtle-768x679.png 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><figcaption>Making Star Using Turtle<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Changing Colors with Python Turtle<\/strong><\/h2>\n\n\n\n<p>One can change the color of the screen, the turtle, and the lines drawn to make the shapes look nicer. The code below displays a rectangle and a star using different colors.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport turtle\n\n# Creating Screen\nmy_window = turtle.Screen()\nturtle.bgcolor(&#039;black&#039;)\n\n# Creating turtle to draw\nmy_pen = turtle.Turtle()\nmy_pen.color(&#039;yellow&#039;)\nmy_pen.forward(150)\nmy_pen.color(&#039;green&#039;)\nmy_pen.left(90)\nmy_pen.forward(200)\nmy_pen.color(&#039;orange&#039;)\nmy_pen.left(90)\nmy_pen.forward(150)\nmy_pen.color(&#039;pink&#039;)\nmy_pen.left(90)\nmy_pen.forward(200)\nmy_pen.right(90)\nmy_pen.color(&#039;black&#039;)\nmy_pen.forward(100)\n\ncolors = &#x5B;&#039;red&#039;,&#039;magenta&#039;,&#039;yellow&#039;,&#039;orange&#039;,&#039;green&#039;]\nfor i in range(5):\n    my_pen.color(colors&#x5B;i])\n    my_pen.forward(200)\n    my_pen.right(144)\n\n# Displaying Window\nmy_window.mainloop()\n<\/pre><\/div>\n\n\n<p>The picture below displays the output of the code.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"961\" height=\"850\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_colorful_turtle_shapes.png\" alt=\"Making Colorful Turtle Shapes\" class=\"wp-image-14939\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_colorful_turtle_shapes.png 961w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_colorful_turtle_shapes-300x265.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Making_colorful_turtle_shapes-768x679.png 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><figcaption>Making Colorful Turtle Shapes<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Congratulations! You now know about Python turtle! This Turtle Library can also create very complex shapes and have a lot of colors. <\/p>\n\n\n\n<p>Happy coding! <\/p>\n\n\n\n<p>Hope you learned something!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello fellow learner! Today you will be introduced to Python Turtle, the turtle of the coding world who is used to create fun drawings and shapes on your screen with just some simple steps. Let&#8217;s Begin! Introduction to the Python Turtle Library Python Turtle helps users interact with the programming language better by drawing various [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":14946,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-14903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=14903"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14903\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/14946"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=14903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=14903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=14903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}