Sept. 21, 2021


Teacher Resources
...

Originally posted: Sept. 21, 2021
Last edited: 1 year, 2 months ago


Variable names

NOTE: variable names are also called identifiers.

Variables' names should follow some rules:

  • Must begin with a letter (a - z, A - Z) or underscore (_) but do not choose a name that begins and ends with two underscores, such as __varname__.
  • Other characters can be letters, numbers or underscore _.
  • Python is case sensitive, for example, MYBOOK, Mybook, MyBook, myBook, and mybook are all different variables or identifiers.
  • Can be any (reasonable) length.
  • Do not use a keyword as a variable name. Python uses keywords for other purposes.

NOTE: I recommend when you name the variable to follow these advices.

  • Choose a meaningful name instead of a short name; home_worker is better than hw.
  • Avoid long variable names; number_of_student_in_class is too long
  • Be consistent; home_worker not HomeWorker.
  • For some special cases, begin a variable name with an underscore(_) character.
 

Assign a value of an obj. ref to another ref.

How to do that?

See the following example:

 
r = 'red'
y = 'yellow'
print(r, y)

b = 'blue'
print(b)

b = r
print(r, y, b)
 
red yellow
blue
red yellow red
 

You need to understand how the assignment operator works.

 

When we type b = r, this means that we are giving the value of the right object reference (r) to the variable in the left of the assignment which is (b).

When the statement is executed:

b = r

What happens is that the value of b, which was originally 'blue', will change to 'red' which is the value of r.

Great!

Now you have learned how to create an object reference and assign a value to it. Next, you will learn the different commands that assign values to tina the turtle.

 

Stamp:

Turtles can make a stamp of their shape that stays there even if they leave:

tina.forward(100)
tina.stamp()
tina.backward(100)

Color:

Turtles can change into all kinds of awesome colors.

# Red!
tina.color("red")
tina.forward(100)

# No, Blue!
tina.color("blue")
tina.backward(100)

Circle:

You guessed it:

tina.forward(100)
tina.circle(10)
tina.backward(100)

Fill:

Turtles have a fill mode that will fill in a shape.

tina.fill(True)
tina.forward(100)
tina.left(90)
tina.forward(100)
tina.left(90)
tina.forward(100)
tina.left(90)
tina.forward(100)
tina.fill(False)

Goto:

Know exactly where you want your turtle to go? Make her go there:

tina.goto(100,100)

See if you can figure out the extent of the coordinate system.

Setx

Set the x coordinate.

tina.setx(100)

Sety

Set the y coordinate.

tina.sety(100)

Hide

Peek-a-boo turtles.

tina.hideturtle()

Write

Turtles are literate.

tina.write("Heck Yeah!", None, "center", "16pt bold")

The Pen:

You may have noticed that the turtles draw anywhere they move. We can control that behavior with .penup():

# No line:
tina.penup()
tina.forward(100)

# Yes line:
tina.pendown()
tina.backward(100