Python 0 Basics Quick Start Fun tutorial (Dr. Mi Turtle drawing Turtle) 2. Variable

Source: Internet
Author: User
Tags square root

We have learned the concept of variables in high school. For example: we make x = 100, then we can launch x*2 = 200

Try this Python code below

1 Import Turtle 2  3 turtle.shape ("turtle")4 x =5  Turtle.forward (x)6 turtle.left (7) , Turtle.forward (+x)8   9 Turtle.exitonclick ()

Run the above code and the turtle will draw the following pattern

x = 100 declares the variable x and assigns it a value of 100, in the familiar middle school math language, is "Make x equals" /span>

In the next code turtle. Forward (x

Similar to turtle. Forward(2*x) is the distance of twice X units for the Turtle to advance, that is, twice times the distance of 100 units, and eventually the turtle will crawl forward 2 x 100 = 200 units of distance (the longer skew in the image pointing to the top right Line)

Prompted

    • Python (and most programming languages) uses * to represent mathematical multiplication to avoid confusion with the letter X

Unlike high school math, variables in Python can be used not only to represent numbers, but also to represent a variety of non-digital things. For example, by ipaomi = Turtle You can change Dr. Mi into a turtle, and you can use the Ipaomi variable to control the turtle's drawing.

The following code, drawing the image and just the same, the difference is that we assign a turtle to the variable Ipaomi

1 Import Turtle 2  3 Ipaomi = Turtle  4 ipaomi.shape ("turtle")  5 x = 6ipaomi.forward (x)  7 ipaomi.left (GB)  8 Ipaomi.forward (x)  9   Ipaomi.exitonclick ()

Practice

Try to draw a house (use a variable to do it, try to adjust the value of the variable, draw a different size of the house)

Prompted

    • You might need to use the open-root operation, introduce the math module, and then use the Math module's Sqrt method to perform the open-root operation.
    • For example, the following code calculates the square root of 5 and assigns the result of the calculation to the variable x
1 Import Math 2  3 x = math.sqrt (5)

"Original link" http://www.ipaomi.com/2017/11/15/python-0 basics-Quick Start-Fun Tutorial-Dr. Mi-Turtle painting-turtle-2-Change/

Python 0 Basics Quick Start Fun tutorial (Dr. Mi Turtle drawing Turtle) 2. Variable

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.