Как нарисовать окружность в python
Перейти к содержимому

Как нарисовать окружность в python

  • автор:

turtle — Turtle graphics¶

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.

Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle , give it the command turtle.forward(15) , and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25) , and it rotates in-place 25 degrees clockwise.

Turtle can draw intricate shapes using programs that repeat simple moves.

../_images/turtle-star.png

By combining together these and similar commands, intricate shapes and pictures can easily be drawn.

The turtle module is an extended reimplementation of the same-named module from the Python standard distribution up to version Python 2.5.

It tries to keep the merits of the old turtle module and to be (nearly) 100% compatible with it. This means in the first place to enable the learning programmer to use all the commands, classes and methods interactively when using the module from within IDLE run with the -n switch.

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

The object-oriented interface uses essentially two+two classes:

The TurtleScreen class defines graphics windows as a playground for the drawing turtles. Its constructor needs a tkinter.Canvas or a ScrolledCanvas as argument. It should be used when turtle is used as part of some application.

The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when turtle is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible.

All methods of TurtleScreen/Screen also exist as functions, i.e. as part of the procedure-oriented interface.

RawTurtle (alias: RawPen ) defines Turtle objects which draw on a TurtleScreen . Its constructor needs a Canvas, ScrolledCanvas or TurtleScreen as argument, so the RawTurtle objects know where to draw.

Derived from RawTurtle is the subclass Turtle (alias: Pen ), which draws on “the” Screen instance which is automatically created, if not already present.

All methods of RawTurtle/Turtle also exist as functions, i.e. part of the procedure-oriented interface.

The procedural interface provides functions which are derived from the methods of the classes Screen and Turtle . They have the same names as the corresponding methods. A screen object is automatically created whenever a function derived from a Screen method is called. An (unnamed) turtle object is automatically created whenever any of the functions derived from a Turtle method is called.

To use multiple turtles on a screen one has to use the object-oriented interface.

In the following documentation the argument list for functions is given. Methods, of course, have the additional first argument self which is omitted here.

Draw Circle in Python Using turtle circle() Function

To draw a circle in Python, we can use the circle() function from the Python turtle module.

The turtle module in Python allows us to create graphics easily in our Python code.

We can use the turtle module to make all sorts of shapes in Python. For example, we can draw rectangles and draw triangles easily in Python with the turtle module.

One other shape which is easy to draw with turtle is a circle.

The defining feature of a circle is the radius. To draw a circle in Python, we can use the turtle circle() function from the turtle module.

We can define a simple function which will take one argument, the radius of our circle, and make the circle.

Below is a simple example of how to use Python to create a circle.

turtle circle

Cool Patterns You Can Make With circle() in Python

There are a few cool patterns you can create with the Python turtle circle() function.

One example is a spiral.

Below is the Python code for creating a spiral, and the output of the spiral with the turtle module.

turtle spiral

You can also make concentric circles easily with the circle() function. To make concentric circles, we make circles starting in the middle, and keep increasing the radius until the end.

Below is the Python code for creating concentric circles.

turtle concentric circles

How to Draw a Circle with Different Colors in Python

With turtle, we can easily add color to the shapes we create.

The main function you can use to change the color of a line is with the turtle pencolor() function.

Below is an example and the output of how to draw a green circle using pencolor() in Python.

turtle color green circle

To fill a shape, there are a few steps to take. We use the fillcolor() function to define the fill color of our shape, and then use the begin_fill() and end_fill() functions to define when to begin and end filling shapes with the fill color.

Below is an example and the output of how to fill a circle with the color ‘blue’ using fillcolor(), begin_fill() and end_fill() in Python.

turtle fill color blue circle

Hopefully this article has been helpful for you to learn how to draw a circle in Python.

Other Articles You'll Also Like:

  • 1. How to Group and Aggregate By Multiple Columns in Pandas
  • 2. How to Check if String Contains Uppercase Letters in Python
  • 3. Python turtle write() – Write Text to Turtle Screen
  • 4. Python Prime Factorization – Find Prime Factors of Number
  • 5. Python Turtle Fonts – How to Write Text with Different Fonts in Python
  • 6. Using Python to Check if Number is Divisible by Another Number
  • 7. Set Widths of Columns in Word Document Table with python-docx
  • 8. Multiple Condition if Statements in Python
  • 9. Symmetric Difference of Two Sets in Python
  • 10. How to Split List in Half Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

Teaching kids how to code with Python Turtle

Bruno Leal

Teaching how to code from a young age has become increasingly usual. There are good reasons to do so, since the ability to code has been viewed as one of the most important for the years to come, but also — and probably more importantly — because learning to code offers kids a way to develop and train a range of different skills perceived as useful in everyday life and in other school subjects, like creativity, problem solving, persistence and even collaboration.

Although learning how to code can be difficult and scary, there are some approaches that can mitigate those difficulties. First of all, we should — obviously — take the age of the kid into consideration, but also have in mind that learning should be fun.

«El cerebro sólo aprende si hay emoción» (The brain only learns if there is emotion)

Francisco Mora, neuroeducation specialist

I was about 15 or 16 when I started programming and I did it using BASIC (Beginner’s All-purpose Symbolic Instruction Code). But there’s a chance that some of you reading this text first learned programming — or at least are familiar — with the Logo language, especially if you are a little older than me.

Logo is known by its turtle graphics that are vector graphics / images produced by a small turtle-shaped cursor moving around the screen, obeying to a list of programmed instructions. It requires some geometry knowledge, particularly on angles and angle measure. That means that — in theory — kids around 8-10 years old should be able to understand the mechanics of the exercises to be performed.

The disadvantage of learning Logo is that it has little to no practical use in the real world. It was conceived only to teach programming concepts and is a somewhat dead language.

Fortunately, Python has a turtle module that can be used to learn coding the same way you would with Logo, although — obviously — using a different syntax.

Python is the most widely used programming language worldwide and the one that has grown the most in recent years. And it is easy to explain why. It’s easy to read and to learn; it is very versatile, with its uses ranging from web development to data science and artificial intelligence, not forgetting its ease of use to write simple scripts that can automate many of your day-to-day tasks; it’s also highly extensible with its library system. Python is used not only by programmers, but also by many researchers, students and amateur coders, which helps to explain its growth and position as market leader.

How to use Python turtle?

So, the idea is to command the cursor to move around, starting on the (0, 0) position of the x-y cartesian plane. Once imported, the turtle module has instructions to move the cursor, like «forward(20)» — to move it 20 pixels in the direction it is facing — or «right(90)» — to rotate 90 degrees clockwise. Using only these simple commands, the cursor can draw shapes and pictures the way you want.

The code below instructs the drawing of a square.

The code above uses the object oriented programming paradigm. Although in a simple way (just a turtle object with three methods: forward, left and done), it can still be a difficult concept to explain to those who are starting to program. If we want to simplify, we can work around this eventual problem using another technique for importing the turtle package.

Although it is not a recommended practice on «real» programming, in this case it makes the code more easily readable and helps to keep the focus on what ‘s important, without the need to explain — as such an early stage — the concepts of object and object-oriented language.

What can we draw with Python turtle?

Well, basically everything, if you have the skills. But we should start with the basics, even more when teaching.

Drawing a Circle

What about starting with a circle? Since the module contains a specific method to draw a circle, we’re going to spice things up a little by changing the line colour. The following code draws a 50-pixels radius circle. Well, if we’re strict, it’s actually a circumference of a circle.

To fill the area inside the circumference we just have to add three more instructions.

Drawing polygons

Let’s start by drawing a hexagon.

It’s the right time to introduce the students to loops, particularly a for loop. Since the Python method «range(n)» creates a sequence of n numbers (ranging from 0 to n-1), the line «for i in range(6)» will instruct Python to execute the code inside the for loop for 6 times. That means that the cursor will go forward 100 pixels, then turn left 60º and repeat these two steps for a total of 6 times.

Why 60º? Well, because it’s the result of dividing 360º by 6 (the number of sides of the polygon).

As you can see, learning this way is not only about learning to code; it also helps students to better understand geometry and train geospatial comprehension skills.

Generalizing the code above, we can redefine it to create any polygon, given the desired number of sides.

The one showed below will draw a 5-sides polygon — a pentagon.

The main difference lies in the use of a variable to save the desired number of sides. Probably, kids in the age range mentioned earlier (8–10 years) are not yet familiarized with the mathematical concept of variable. But it shouldn’t be hard to explain them that it only represents a mean to store a value that can be changed.

The sky is the limit

Knowing the basics and with access to the package’s full reference, there are many activities and challenges that can be devised and put into practice.

I leave some examples below.

First, a rainbow benzene with six different colours.

The used angle of turn of 59º is meant to add a slight variation factor from the hexagon shape. The new methods used should be easy to understand. The «speed» method allows to change the drawing speed. The «pencolor» method changes the colour of the pen.

Now, a green-branched fractal tree.

And finally, a Koch Snowflake.

The construction of the two figures above is— obviously — way too complex to be understood by 8–10 year old kids. But can be a good way to explain concepts like recursion to older students.

Python Turtle Circle

In this Python tutorial, we will learn How to create a circle in Python Turtle and we will also cover different examples related to the Python Turtle circle. And, we will cover these topics.

  • Python turtle circle
  • Python turtle half circle
  • Python turtle circle spiral code
  • Python turtle circle spirograph
  • Python turtle circle fill color
  • Python turtle circle center
  • Python turtle circle steps
  • Python turtle circle color
  • Python turtle Inverted circle
  • Python turtle circle commands

Table of Contents

Python turtle circle

In this section, we will learn How to create a circle whit the help of a turtle in Python turtle.

The circle is a round shape like a ring. In Python turtle, we can draw a circle with the help of a turtle. Turtle is working as a pen and they draw the exact shape of a circle.

Code:

In the following code, we draw a circle with the help of a turtle and the turtle gives the exact shape of a circle-like ring.

tur.circle(90) It is used for drawing a circle with the help of a turtle.

Output:

After running the above code we get the following output in which we see a circle is drawn with the help of a turtle.

Python turtle half circle

In section, we will learn how to draw a half-circle with the help of a turtle in Python turtle.

The half-circle is also known as a semicircle. It is formed by cutting the whole circle along with its diameter.

Code:

In the following code, we import some modules from turtle import *, import turtle as tur, and draw a half-circle with the help of the turtle.

Turtle-It is a pre-installed library that is used for creating shapes and pictures.

tur.speed(1) is used to manage the speed of the circle.

tur.circle(90,extent = 150) It is used for drawing half circle.

Output:

After running the above code we get the following output in which we see a half-circle is created with the help of turtle.

Python turtle circle spiral code

In this section, we will learn how to create a circle spiral code in Python turtle.

A Spiral is a cylindrical coil-like structure or we can say that wind around a point while moving further from a point.

Code:

In the following code, we draw a spiral circle with a radius =8 this spiral circle is drawn with the help of a turtle.

Output:

After running the above code we get the following output in which we see a spiral circle is drawn

Python turtle circle spirograph

In this section, we will learn how to draw circle spirographs in Python turtle.

A spirograph is a device used for drawing different types of curves and also draw beautiful patterns which attract the user’s eye.

Code:

In the following code, we import the turtle library for drawing the required curve and set the background color as “black”.Choose the color combination for color in (‘green’, ‘yellow’, ‘red’,’pink’, ‘blue’, ‘orange’,’cyan’): inside this and the beautiful coloured spirograph shown on the screen.

We also draw a circle of the chosen size.

Output:

After running the above code we get the following output as we showed a beautiful colored circle spirograph is drawn.

Python turtle circle fill color

In the following code, we will learn how to fill color in a circle in Python turtle.

We Can fill the color with the help of tur.fillcolor() and add any color we want in the argument to give the style to the shape.

Code:

In the following code, we draw a color-filled circle with the help of a pen and also manage the speed. speed(1) is a slow speed that helps the user to easily identify the shape.

  • tur.fillcolor(“red”) is used for set the fill color as red.
  • tur.begin_fill() is used to start the filling color.
  • tur.circle(100) is used for drawing the circle with radius.
  • tur.end_fill()is used for ending the filling of the color.

Output:

After running the above code we see the following output in which we see a circle is drawn with a beautiful “red” color is filled inside.

Python turtle circle center

In this section, we will learn about the circle center in Python turtle.

The center of a circle is the point that divides the circle into equals parts from the points on the edge.

Code:

In the following code, we import the turtle package from turtle import *, import turtle as tur also draw the circle of radius 60. Radius is the distance from the center point to any endpoint.

Output:

In the following output, we see a circle is drawn with the help of a turtle inside the circle there is a center point.

Python turtle circle steps

In this section, we will learn about how to draw a circle with steps in Python turtle.

We use turtle.circle(radius,extend=None,steps=None) for creating circle.

  • radius: Radius shows the radius of the given circle.
  • extent: It is part of a circle in degree as an arc.
  • steps: It divides the shape of the circle in an equal number of the given step.

Code:

In the following code, we import turtle library from turtle import *, import turtle as tur we draw a circle of radius 150 pixels with the help of a pen.

tur.circle(150) It is used to draw a circle of radius 150 pixels with the help of a pen.

Output:

After running the above code we get the following output in which we see a circle is drawn.

Python turtle circle color

In this section, we will learn how to change the circle color in python turtle.

Color is used to give the attractive look to shape. Basically, the default color of the turtle is black if we want to change the turtle color then we used tur.color().

Code:

In the following code, we set the background color as black with pensize(2) and the speed of drawing a circle is speed(1). We give two-color to circle and also give a size for drawing circle tur.circle(100).

Output:

After running the above code we get the following output as we see a beautiful colored circle is shown in this picture.

Python turtle Inverted circle

In this section, we will learn how to draw an inverted circle in Python turtle.

Inverted means to put something in an opposite position. The inverted circle is drawn in a clockwise direction rather than in an anticlockwise direction.

Code:

In the following code, we import the turtle library for drawing an inverted circle. Turtle is a pre-installed library used to draw different shapes and pictures.

  • t.right(90) is used to move the turtle in right.
  • t.forward(100)is used to move the turtle in the forwarding direction after moving the right.
  • t.circle(-100)is used for drawing the circle of radius.

Output:

After running the above code we get the following output in which we see an inverted circle is drawn.

Python turtle circle commands

In this section, we will learn how circle commands work in python turtle.

The different commands are used to draw different shapes and they also help to move the turtle in any direction. We will discuss the turtle circle command below.

  • circle()-circle() command is used to draw a circle shape with the help of a turtle.
  • forward()– The forward() command is used to move the turtle in a forwarding direction.
  • right()– A right() command is used to move the turtle in a clockwise direction.
  • penup()– Penup() command is used for picking up the turtle pen.
  • pendown()-Pendown() command is used for puts down the turtle pen.
  • color()– color() command is used for changing the color of the turtle pen.
  • shape()-Shape() command is used to give the shape to the turtle.

Code:

In the following code, we used some commands that help to make a circle. And the circle we make looks attractive.

Output:

After running the above code we get the following output in which we see inside an output the circle is made and loop is working.

So, in this tutorial, we discuss Python Turtle circle and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python turtle circle
  • Python turtle half circle
  • Python turtle circle spiral code
  • Python turtle circle spirograph
  • Python turtle circle fill color
  • Python turtle circle center
  • Python turtle circle steps
  • Python circle color
  • Python turtle Inverted circle
  • Python turtle circle commands

Fewlines4Biju Bijay

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *