This is the 11th day of my participation in Gwen Challenge

Let’s start by taking a closer look at Python and other programming languages. There are many of them, over 600, I’m told. But really commonly used also so dozens of, after all, who is better to use who is more powerful, it can be said that in the programming community there is also the law of survival of the fittest.

So C is probably the language you’re most exposed to. In C language, we mainly learn its pointer and memory, as the bottom language, it is the main problem to solve the performance of the computer system, but also suitable for the development of the bottom program of the computer system.

C may also be in tune with The Times, since it was born in the age of computer systems. Java was born in the era of The Internet and Windows. At that time, the cross-platform problem needed to be solved. As an object-oriented high-level language, Java proposed a cross-platform method to solve the cross-platform problem, so Java became popular and has been popular until now. We’re usually going to look at cross-platform, objects, runtimes, and so on, mainly to solve cross-platform problems.

Said to the C language, so there is a C + +, it is on the basis of the C development, it has the characteristics of object-oriented, and C have similar place, and the underlying language are commonly used to write large software development, small make up in the Pr was found in C + +, although I do not know if all the development Pr for C + +, but at least the composition of the C + +. When learning C++, we will probably learn objects, polymorphism, inheritance and so on.

There is also a kind of front-end language, the above mentioned is called back-end language, front-end language including HTML, CSS, JS and so on, proficient in the front and back end we call full stack engineers. Front-end language mainly deals with web pages, small procedures, website construction and so on.

Now that we’ve talked about these languages, let’s start drawing Python.

Previously useful Python temperature conversion, this has the function of drawing, feel Python function quite many, do not know what will have fun next time.

Drawing in Python is possible because there are so many libraries (I hear there are over 13 million) that we can use directly, and developers around the world have contributed a lot to the expansion of the Python library.

Python’s libraries are divided into standard libraries, which are function modules installed with the interpreter, and third-party libraries, which are installed on their own. The following libraries are the basic standard libraries for Python. The libraries described below, as well as the packages and modules we will encounter in the future, are now generally referred to as modules.

Let’s take a look at its source code:

# pythondraw.py import turtle as t t.setup(650,350,200,200). t.pencolor("red") t.seth(-40) for i in range(4): T circle(40,80/2) t circle(40) t circle(16,180) t circle(40 *2/3) t circle() Today we are going to learn how to draw with the Python library.Copy the code

1. GO!

#PythonDraw.py
Copy the code

First we use the symbol # to name the program, and the part after # is not written in the program code, because # is the same as the C language // function, called comment function. You can write anything after #, but programmers tend to use comments to explain the code, because after a while you might forget what part of the code was written for. Large programs have a lot of code.

2. The first line

import turtle as t
Copy the code

This line of code has a reserved word. This line of code has a reserved word. This line of code has a reserved word. Reserved words are words defined in the language and cannot be used by users as variable names or procedure names. Keywords are words that have a specific meaning in the language and are part of the grammar. The difference between them is that some reserved words are not used in the current syntax, so the difference is not significant, as you know.) Import, it introduces a library turtle, yes, it’s called Turtle.

The main function of import is to introduce a module. In this program, we introduce a module called Turtle.

There is also an as after turtle, what is this for? If we didn’t write as, we would have to change t to Turtle in all statements following this line. So the function of AS is to use T instead of turtle.

In fact, the initial encoding style of importing library with import is as follows:

Import < library name > < library name >.< function name >Copy the code

There are other uses for import, such as combining the reserved words from and import, so that later calls to functions do not need to be written as < library name >.< function name >. Is it a bit simpler than the above method, but this is also a disadvantage, if the program is larger, the use of more functions, so it is likely to cause the phenomenon of the same name, so that the program becomes mixed.

From < library name >import< function name > #Copy the code

Import +< library name >+as+(custom name), this custom name can be arbitrary, which will also be very convenient.

It is said that turtle library was born quite early, I think in 1969, it is also an entry-level graphing function library. The picture below was drawn in Turtle, and you should be able to draw better pictures after reading this article.

In the Turtle library, you can learn the basic methods of using the Turtle library, such as the window layout for drawing, the coordinate system for drawing (spatial coordinate system and angular coordinate system), and the RGB color gamut that you encounter when buying a computer. So RGB is what, today we will briefly understand it.

RGB is made up of three basic colors (red, blue, and green). In RGB colormode, each color ranges from 0 to 255 or from 0 to 1, so how to use it? Turtle provides turtle.colormode(mode) to set the mode. Such as turtle.colormode(1.0) indicates that RGB small value mode is used, and Turtle.colormode (255) indicates that RGB integer value mode is used. Below are some basic color values, and more color RGB values are up to your friends to search.

Before we start looking at more functions in the Turtle library, let’s start with a quick taxonomy. The turtle program syntax elements are: the Turtle pen control function; Turtle Motion control function; Turtle direction control functions and so on, because this program basically talks about these kinds of control functions, there are other statements about loops in the program.

3. The second line

T.s etup (650350200200).Copy the code

In the code written in Turtle, after we run we will see a form that is turtle’s canvas space where the smallest unit we use is pixels. Since there is space, we will have coordinates to regulate it. On our computer screen, the top left corner is (0,0), and it’s the same in canvas space. If we don’t set the window position ourselves, it will automatically default to the middle of the computer screen.

In the Turtle library, there is a turtle.setup function that controls the location and size of a window. Its basic form for the turtle. The setup (width, height, startx, starty), the first two as the width and height, as well as the window of the x, y coordinates, x and y can not fill in, system will default to position in the middle of the computer screen. Setup is not required for drawing, only when we need to control the size and position of the window.

Create a position on the computer screen where the width is 650 pixels, the height is 350 pixels, and the upper left corner of the form is 200,200.

In the canvas window, it also has coordinates to regulate. In the coordinate system of this window, there are absolute coordinates and turtle coordinates. In absolute coordinates, the turtle is in the center of the window, so the center coordinate is (0,0), with the turtle’s head facing the right side of the window. As shown in figure:

So how do turtles move? How do you write motion in code? Turtle provides a function called turtle.goto(x,y). As an example, the circle is the origin (0,0), and we can see that the turtle (actually seeing a point) starts from the center and follows the arrow to the final point.

So what about the direction of movement? We can see the direction of movement in the picture below. Turtle forward(d), called turtle.fd, turtle.bk(d), or turtle.bk(d). There is also a slightly more complicated function for the turtle’s direction of movement. Called turtle.circle (r, Angle), it indicates a curved motion centered at a point on the left, with both sides moving in straight lines.

I mentioned that there are two kinds of spatial coordinate system and angular coordinate system. Knowing the spatial coordinate system, let’s come to kangkang angular coordinate system (as shown below). In the angular coordinate system, the positive x axis represents 0 degrees and 360 degrees, and the positive Y axis represents 90 degrees and -270 degrees. Such a coordinate system is called absolute coordinates.

Turtle provides a turtle. Seth (Angle) function to change the turtle’s direction of travel. We can also see that Angle is the only argument in parentheses, so it only changes the direction of travel. It doesn’t change the distance traveled.

In the angular coordinate system of turtle movement, there are other functions to indicate the Angle of turtle movement, such as turtle.left(Angle) to make the current turtle left, and turtle.right(Angle) to make the current turtle right.

The previous said so many functions, in fact, some statements in the program has been included, so we now in the form of code blocks to explain for you.

4. Brush control function

t.penup()
###t.fd(-250)
t.pendown()
t.pensize(25)
t.pencolor("red")
Copy the code

Let’s look at the syntax element as a module, the above statement (ignore the second line, it is not a paintbrush control function). Except for the second line, they all have a pen in front of them. In the pen control function, penup and pendown are usually paired. If the pen can fly up, it must also fall down. Turtle. penup and turtle.down are also known as turtle.pu and turtle.pd, which are initials.

Pensize is used to control the width of the pen. It is also called turtle.width. In this application, we set the size of the brush to 25, which is still a bit large according to the image after the application runs.

It is also easy to see that the pencolor is related to the color, we use the color string (remember the color string must be lowercase oh) to paint the turtle (the pen) color, can also be used to describe the RGB value, such as: T. pencolor(0.63,0.13,0.94), or even pack the decimals into a tuple and put them directly into a pencolor function, such as T. pencolor((0.63,0.13,0.94)).

5. Motion control function

t.fd(-250)
Copy the code

This sentence can be interpreted as the turtle moving backwards 250 pixels.

Tc ircle (40 reached)Copy the code

The control motion function was covered a little bit in the second line of code, and I’ll add it here. Circle (r,extent=None), which draws an extent Angle with a radius of r.

By default, the center is to the left of the turtle, but if it is negative, the center is to the right of the turtle, as shown in the example below:

6. Direction control function

t.seth(-40)
Copy the code

Turtle provides a turtle.setheading(Angle) function to change the turtle’s direction of travel. Only Angle is in parentheses, so it only changes the direction of travel, not the distance. The alias of turtle.setheading(Angle) is turtle.seth(Angle).

The direction control function is also used to indicate the Angle of the turtle’s movement, such as turtle.left(Angle) for the current turtle to the left and turtle.right(Angle) for the current turtle to the right.

7. Loop statements

For I in range(4):# I count from 0 t.cle (40,80) t.cle (-40,80)Copy the code

Finally, let’s talk about loop statements and range functions. Loop we use for… The in structure (thought of as a fixed collocation) also contains a range function. The basic structure of a loop statement is:

For < variable >range(parameter)# Specifies the number of times the statement is executedCopy the code

Here is an example of a simple program:

So what does the range function do in this, it does is generate a loop sequence count. Range contains two methods of use. The first is range(N), which is used to produce a sequence of 0 to n-1 integers. Another, not too different, is range(M, N), which is used to produce sequences of N-M integers from M to N-1.

8. Run the program

t.done()
Copy the code

There is a final statement here that keeps the window from closing, without which it will automatically close after drawing “Python”. The image below shows that “Python” has been drawn successfully. It doesn’t look like much, but it’s ok

Finally, we must learn English well. The name of many functions in this program has a great relationship with English. English will make it more convenient to understand the program.