Turtle Graphics
Using Variables in Python
Positioning the turtle pointer in variable locations for new squares is possible by calculating the new location:
import turtle
def draw_a_square_for_me(square_size):
turtle.forward(square_size)
turtle.right(90)
turtle.forward(square_size)
turtle.right(90)
turtle.forward(square_size)
turtle.right(90)
turtle.forward(square_size)
turtle.right(90)
def move_turtle_back(square_spacing):
turtle.penup()
turtle.backward(square_spacing)
turtle.left(90)
turtle.forward(square_spacing)
turtle.right(90)
turtle.pendown()
def draw_a_square_and_move_back(square_color, size_of_square, space_between_squares):
turtle.pencolor(square_color)
draw_a_square_for_me(size_of_square)
move_turtle_back(space_between_squares)
size = 100
spacing = 10
turtle.penup()
turtle.goto(-1 * size / 2, size / 2)
turtle.pendown()
turtle.pensize(5)
turtle.speed(7)
draw_a_square_and_move_back("Red", size , spacing)
spacing = spacing + 5
size = size + 2 * spacing
draw_a_square_and_move_back("Blue", size , spacing)
spacing = spacing + 5
size = size + 2 * spacing
draw_a_square_and_move_back("Green", size , spacing)
spacing = spacing + 5
size = size + 2 * spacing
draw_a_square_and_move_back("Yellow", size , spacing)
spacing = spacing + 5
size = size + 2 * spacing
draw_a_square_and_move_back("Orange", size , spacing)
spacing = spacing + 5
size = size + 2 * spacing
draw_a_square_and_move_back("Pink", size , spacing)
turtle.done()
You can modify the calculations to make the squares concentric, or you can make more artistic impressions of your own.
Variables are data containers to store, modify and reuse text (Strings), numbers (integers, floating point numbers), boolean values (true, or false), or other complex data types. Numbers can be cast from one type to another. Text strings can be generated from numbers and vice versa. It is possible to find out the type of the value contained in the variable using the type()
function as in:
variable1 = 12
variable2 = "Some text"
variable3 = 3.14
variable4 = True
print("variable1 is of type ", type(variable1))
print("variable2 is of type ", type(variable2))
print("variable3 is of type ", type(variable3))
print("variable4 is of type ", type(variable4))
which will output
variable1 is of type <class 'int'>
variable2 is of type <class 'str'>
variable3 is of type <class 'float'>
variable4 is of type <class 'bool'>
The following words are reserved for their functional purposes and they cannot be used as a name for variables, arguments, functions, methods, classes, etc.
and elif if print
as else import raise
assert except in return
break exec is try
class finally lambda while
continue for not with
def from or yield
del global pass
You can check the list of your own interpreter from the command line terminal:
python
help("keywords")
You can also check the individual description of each keyword. As an example, for the and keyword, you can type:
help("and")
and study its description.
You can also find a list of the reserved words in https://www.w3schools.com/python/python_ref_keywords.asp