stoneskin

learning coding with Scratch , Python and Java

Session 1.1 Get Start With Python

1.1 what is coding

1.2 What is Python

1.3 Install Python

1.4 Install a Python Editor

1.5 Your first python code

Open a Editor, input upper lines, save as file hello_world.py

in terminal, input hello_world to run it. or in editor menu click the run button.

1.6 Drawing with python Turtle

1.6.1 Drawing a line with python Turtle

In your Python editor, create a new file and name it as MyFirstTurtle.py

# Code sample MyFirstTurtle.py
import turtle,time

# Draw line with length 100
turtle.forward(100)

# waiting 10 sec and exit python running window
time.sleep(10)

Notice the time.sleep(10) is used for terminal that will close after python script finished. If you use IDLE it will not necessary.

myfirstptyhon

1.6.2 Make turn and draw another line

please add below code before time.sleep() of code in 2.1


# make turn 90 degree and draw another line
turtle.left(90)
turtle.forward(100)

1.6.3 Continue and draw a square

Repeat the code in 2.2 twice, you could get a square. Below example, in stand of using turtle directly, I chose use a turtle.Pen() instance.

# Code sample MyFirstTurtle.py
import turtle,time
t = turtle.Pen() # make a turtle Pen instance.

# Draw line with length 100
t.forward(100)

# make turn 90 degree and draw another line
t.left(90)
t.forward(100)

# continue turn and drawing line twice
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)

# waiting 10 sec and exit python running window
time.sleep(10)

square

1.6.4 if you make a right turn

Turtle left turn command is t.right(). Please try to drawing lines like below image:

stair

1.6.5 Make Another turtle

With below code, you could multiple turtles, please try and see how it works

import turtle
t1 = turtle.Pen()
t2 = turtle.Pen()
t1.right(90)
t2.left(90)
t1.forward(100)
t2.forward(100)

1.6.6 More about Turtle

Please check the document of Turtle Graphics https://docs.python.org/3/library/turtle.html