Chapter 3 python features and python drawing (turtle library)

Python is a general-purpose language. Its original design is to solve the logic of the problem, try to hide the complex and subtle differences, and users do not need to care.
I personally think it's a bit like the concept of an operating system, hiding "ugly" hardware. In fact, the previous languages ​​also worked hard on generality and cross-platform, but python did the best.
It has two features:
①Completing the same function, the code amount of python is only 10% of that of C language, which is equivalent to a 10-fold increase in efficiency.
②With more than 130,000 third-party libraries, these libraries are provided by engineers and enthusiasts around the world and grow rapidly at a rate of 20,000 per year.

Python libraries can be roughly divided into two categories: standard libraries and third-party libraries.
The standard library is included with the program when python is installed.
Third-party libraries need to be installed separately before they can be used.

Personal doubt: In theory, as long as a certain standard language is used, it is universal and cross-platform, such as standard c, standard c++, etc. The simple knowledge I have learned tells me that the so-called cross-platform, when it is really implemented, The same is to call the underlying api of the respective platform, but this work is done by the compiler or interpreter when compiling or interpreting, similar to the precompiled #ifdef of the c language.
In other words, high-level languages ​​are now cross-platform.
Since they are all calling libraries and reusing other people's code, theoretically any language can be used. Why do other languages ​​with a longer development time than python have far less libraries than python? Calling libraries in other languages ​​can do the same thing.
write picture description here
The teaching assistant gave the answer.


draw a python

import turtle
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
    turtle.circle(40,80)
    turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.done()

It works fine, but I don't know how to get rid
write picture description here
of the console.
Once you learn how to draw pythons, you can learn circles, five-pointed stars, national flags, robot cats, etc. If you master the method of drawing a line, you can draw the whole world (in theory).
The turtle library, also known as the turtle library, is an entry-level implementation of the python drawing system. Turtle drawing was born in 1969 and belongs to one of the standard libraries of python.

Basic principle:
imagine that there is a turtle, in fact, in the center of the window, walking on the canvas, the trajectory it walks forms the drawn figure.
The turtle can be programmed to change color, change width, etc.
①The drawing form of turtle is a canvas space, and the minimum unit is pixel.

② The setup method can set the starting position (upper left corner) and size, width, height, startx, starty of the form. If the actual position is not set, the default window is in the center of the screen.

③The spatial coordinate system includes absolute coordinates and turtle coordinates. The absolute coordinates are based on the center of the form as (0, 0); the turtle coordinates are considered based on the vision of "turtles", which are divided into forward direction fd and backward direction. bk, left direction, right direction.

④ Angle coordinate system, including absolute angle and turtle angle, seth (angle) changes the direction (angle) of the turtle's travel, but only changes do not travel, angle is a degree. left(angle) how many degrees to turn the turtle to the left, right(angle) to the right.

⑤RGB color system, RGB is described by the three primary colors of red, green and blue to describe the color of all things, which can be represented by integer values ​​and can also be represented by decimal values.
English Chinese RGB integer RGB decimal
white white 255 , 255, 255 1, 1, 1
yellow 255 , 255, 0 1, 1,
0 magenta magenta 255, 0, 255 1, 0, 1
cyan 0, 255, 255 0 , 1, 1
blue 0, 0, 255 0, 0, 1
black black 0, 0, 0 0, 0, 0
seashell 255, 245, 238 1, 0.96, 0.93
gold 255, 215, 0 1 , 0.84, 0
pink 255, 192, 203 1, 0.75, 0.8
brown 165, 42, 42 0.65, 0.16, 0.16
purple 160, 32, 240 0.63, 0.13, 0.94
tomato 255, 99, 71 1.0, 0.39 , 0.28

The turtle library uses decimal value mode by default, of course, you can also use colormode(255) to switch to integer, and colormode(1.0) to switch to decimal.


The import keyword means to import a library, and then you can use the functions of this library.
The traditional is the library name. method to use, you can also use the from keyword to simplify import. for example

from turtle import*

The format of all turtle. source code can be simplified and not written.
The first method does not have function duplication and is much cleaner to read.
Although the second method is simplified, the function has the same name, and it is easy to be confused when reading. I don't know which library is called (or a library written by myself).
Python provides another method, which combines workload simplification and code clarity, using import library name as library alias

import turtle as tu

Take an alias tu for turtle, so that writing tu.setup has the same effect as turtle.setup.
This method is advocated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325208841&siteId=291194637