Python zero-Basic Quick Start fun tutorial (dr. Mi turtle drawing turtle) 2. variable, pythonturtle

Source: Internet
Author: User

Python zero-Basic Quick Start fun tutorial (dr. Mi turtle drawing turtle) 2. variable, pythonturtle

Everyone has learned the concept of variables in middle school. For example, if we set x = 100, we can release x * 2 = 200.

Try the following Python code:

1 import turtle2  3 turtle.shape("turtle")4 x = 1005 turtle.forward(x)6 turtle.left(45)7 turtle.forward(2*x)8  9 turtle.exitonclick()

Run the above Code to draw the following pattern

X = 100 declares the variable x and assigns it a value of 100. In the familiar middle school mathematical language, it is "making x equal to 100"

In the following code, turtle. forward (x) refers to the distance between x units before the turtles. Since x has been assigned a value of 100, so it is actually the distance of 100 units before the turtle (the shorter horizontal line in the image)

Similarly, turtle. forward (2 * x) is the distance of x units that let the turtles forward 2 times, that is, the distance of 100 units that advance 2 times, eventually the turtle crawls 2x100 = 200 units forward (the long slash pointing to the top right in the image)

Tip]

  • * Is Used in Python (and most programming languages) to represent multiplication operations in mathematics, so as to avoid confusion with letters x.

Different from middle school mathematics, variables in Python can be used not only to represent numbers, but also to represent various non-numbers. For example, with ipaomi = turtle, you can turn Dr. Mi into a turtle, and then you can use the ipaomi variable to control the turtle painting.

The following code shows the same image as the previous one. The difference is that we assigned a turtle a value to the variable ipaomi.

 1 import turtle 2   3 ipaomi = turtle 4 ipaomi.shape("turtle") 5 x = 100 6 ipaomi.forward(x) 7 ipaomi.left(45) 8 ipaomi.forward(2*x) 9  10 ipaomi.exitonclick()

[Exercise]

Try to draw a house (use the variable to complete, try to adjust the value of the variable, and draw a house of different sizes)

Tip]

  • You may need to use the operation of the root number, introduce the math module, and then use the sqrt method of the math module to perform the operation of the root number.
  • For example, the following code calculates the square root of 5 and assigns the calculation result to the variable x.
1 import math2  3 x = math.sqrt(5)

[Original article] simplified/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.