Technovation logo

1.5. Learning to Code

1.5.1. Why learn Coding?

image of children and benefits of learning to code (ClipartKey.com 3061179)
  • Develop critical thinking skills

  • Learn how to create — not just use — technology

  • Fuel innovation and discovery in all disciplines

1.5.2. Terminology

Code refers to a list of intructions which a computer can follow to complete some job.

Code is commonly also called a program.

Coding is the process of writing a program.

A computer runs a program to complete the job described by the program.

A programming language gives the words and the rules to use for writing a program.

The distinctions between these terms can be illustrated by analogy. What do you think?

image of teens following a recipe (clipart-library.com/clipart/26)
clipart of dog pondering an equation involving bones (CoolCLIPS_vc016297)
image of a trainer trying to get a dog to jump through a hoop (Cliparts.zone/clipart/675010)

1.5.3. Python Turtle Graphics

You’ll be learning coding using Python Turtle Graphics, which is a dialect of the Python programming language intended for teaching programming.

Think of a (Python Turtle Graphics) turtle as your new (virtual) pet. Instead of using words and gestures to teach it to roll over or sit, you’ll be writing computer programs to teach it how to draw pictures on a screen.

For example, the code in the box below instructs the turtle to draw a square.

To run the code, press the green Run button. The result will be shown below this Active Code box. So you may need to scroll the browser view up to see it.

Q-4: What happens if you change the input to one or more of the turtle.forward(100) commands (i.e. you replace the 100 with a different number)? (Hint: Try it – replace one or more of them and then run the program again!)

Q-5: What happens if you change the input to one or more of the turtle.left(90) commands (i.e. you replace the 90 with a different number)? (Hint: Try it!)

Q-6: If you haven’t changed the inputs and run the program several times, do that now. After changing and running the code in an Active Code box, what does the slider next to the Run button let you do?

1.5.4. Code-Along

A code-along is when an instructor thinks out-loud in order to illustrate how they would create code to solve some problem. But instead of just listening and watching, you copy what they are writing into an Active Code window in your own book, so you can experiment with running the code and you will have a copy of the working program in your own book when it’s completed.

The challenge for our first code-along is to instruct the turtle to draw an equilateral triangle instead of a square:

Image of an equilateral triangle drawn using Python turtle

Write a program to instruct the turtle to draw an equilateral triangle like the one shown above.

It’s logical, right?

Now try your hand at writing a program to draw a hexagon:

Image of a hexagon drawn using Python turtle

Write a program to instruct the turtle to draw a hexagon.

A (Python Turtle Graphics) turtle knows many more commands than just forward and left. Following are some that will be useful in the remaining exercises for this week.

See if you can guess what each command does before revealing our explanation.

turtle.up()

turtle.down()

turtle.backward(L)

turtle.right(D)

turtle.circle(L)

turtle.goto(X, Y)

turtle.color(C)

We’ll introduce more turtle commands as we go along. But if you are curious, you can learn all about Turtle Graphics, including all of the commands that the turtle understands, here.

With these commands, we can instruct the turtle to draw more interesting diagrams.

For example, here’s a program that draws a six-pointed star in blue and green.

Run the program and scroll down to see what the turtle draws.

The turtle runs the commands in your code exactly as you have written them and in the exact order (your dog probably is not as obedient!).

Sometimes the order doesn’t matter and sometimes it is crucial.

Q-11: What happens if you reverse the order of the turtle.up() and the turtle.goto(-100, -50) commands? (Hint: Try it.)

Q-12: What happens if you reverse the order of the turtle.down() and the turtle.forward(200) commands? (Hint: Try it.)

../_images/circles.png
You have attempted of activities on this page