TLAC · Think Like A Computer

8 basic concepts of programming in TLAC

Learn to program, one instruction at a time.

By Usmar A. Padow (amigojapan) · Author’s homepage

This lesson is for complete beginners. You can follow the instructions with the turtle board game, a coin, and paper. No programming experience is needed. Start with the order of instructions, then add one new idea at a time.

The execution links below open a step-by-step debugger. Use Next and Previous to watch the current instruction, variables, and board or text output. The execution page keeps your selected language.

0. Following instructions in order

A program is a set of instructions. The computer follows them in a specified order, usually from top to bottom unless a condition, loop, or function changes the flow. The order matters: the computer does not rearrange your instructions to match what you meant.

Playing TLAC: use a small coin to mark the current instruction. Blue numbers such as 0001 tell you where you are. Yellow cards contain instructions; green cards contain comments for the reader.

A morning routine, in order

0001

Function call: Brush my teeth

0002

Function call: Comb my hair

0003

Function call: Go to work

This order means brushing your teeth, combing your hair, and then going to work. Now compare it with the same actions in a different order:

The same actions, a different result

0001

Function call: Brush my teeth

0002

Function call: Go to work

0003

Function call: Comb my hair

Here you leave for work before combing your hair. All three instructions are still present, but the sequence changes the result. This is why checking the order is one of the first steps in finding a programming mistake.

The original lesson also refers to this Richard Feynman lecture when discussing how literally computers follow instructions.

1. Variables

A variable is a name for a stored value. Think of a labeled box: the name tells you which box to use, and its current contents are the value. A variable can hold a number such as 5 or text such as “hello”. Text is called a string.

Names such as x or last_name are easy to use. In the examples, names do not contain spaces: last_name is one name, while “last name” is a phrase. Strings are shown in quotation marks to distinguish them from names and numbers. For example, last_name can store “Einstein”.

The kind of value matters. An identification number such as 123456789 can be written as a number, but “123-45-6789” is text because it includes separators. Choose a representation that preserves the information you need, including leading zeros when they matter.

A variable’s value can change. Setting x to 3 and later setting x to 5 replaces the old value; x now holds 5. This operation is called assignment. It records a value at a particular point in the program, rather than declaring a mathematical equation that must always be true.

Playing TLAC: write each variable’s name and current value on a separate sheet. Cross out the old value whenever an assignment changes it.

2. Statements and assignment

A statement is an instruction that performs a step of the program. Assignments, function calls, and conditions are examples. Follow these three assignments in order:

Change the value of x

0001

Set x to 3

0002

Set x to 5

0003

Set x to its current value + 1

  1. Line 0001 sets x to 3.
  2. Line 0002 replaces that value with 5.
  3. Line 0003 reads the current x (5), adds 1, and stores 6 back in x.

In the notation x = x + 1, first calculate the expression on the right using the old value. Then put the result in the variable on the left. That destination is sometimes called the lvalue. The final value is 6; the instruction does not mean that a number is mathematically equal to itself plus one.

3. Conditions and blocks

A condition asks a question with a true or false answer. A block is a group of instructions that belongs together. If a condition is true, follow its block; if it is false, skip that block. The colored enclosure and indentation show which instructions belong to it.

Initial board: X=4, Y=9, facing up

Coordinates start at 0. X counts columns from the left and Y counts rows from the top. X=4 is the fifth column. “Move forward” moves the turtle one square in the direction of its head unless another distance is given.

Choose which blocks to execute

0001

Green cards like these are comments

0002

Comments do not change what the program does

0003

Read them to understand the instructions

0004

If X is greater than 3, then

0005

X is greater than 3, so

0006

this group of instructions, inside the

0007

colored block, will run

0008

Function call: Move Turtle Forward 1 space

0009

If X is less than 3, then

0010

X is not less than 3 here, so

0011

this block will be skipped

0012

Function call: Move Turtle Forward 1 space

0013

Set X to 2

0014

This assignment moves

0015

the turtle to X=2, two squares

0016

from the left edge (the third column)

0017

If X is less than 3, then

0018

After changing X,

0019

X is less than 3, so this block

0020

will run

0021

Function call: Move Turtle Forward 1 space

Lines 0001–0003 are comments; they explain the program without moving anything. At line 0004, X is 4, so “X is greater than 3” is true. Enter the block and execute the forward move on line 0008. X stays 4; moving up changes Y from 9 to 8.

After line 0008: X=4, Y=8

At line 0009, X is still 4, so “X is less than 3” is false. Skip the block, including the move on line 0012. Line 0013 then sets X to 2: move the turtle sideways to the third column without changing Y or its direction.

After line 0013: X=2, Y=8

Line 0017 asks the same question again, but now X is 2. This time “X is less than 3” is true, so execute line 0021. The turtle moves up one square to X=2, Y=7. A condition uses the values at the moment it is checked.

Final board: X=2, Y=7

4. Procedures and functions

A function definition gives a name to a reusable group of instructions. A function call asks the program to perform that group. Defining a function does not run its body. We already used calls in the morning routine:

Calling named actions

0001

Function call: Brush my teeth

0002

Function call: Comb my hair

0003

Function call: Go to work

Now define a new turtle action: lay four eggs in a square. “Place an egg” marks the square under the turtle. “Turn right” rotates it 90° clockwise without moving to another square. These are built-in TLAC actions; the square-making function below is a new action defined by the programmer.

Define one egg pattern; use it three times

0001

Functions example

0002

Define a reusable group of instructions:

0003

Function definition: Lay four eggs in a square

0004

Function call: Place an egg under the turtle

0005

Function call: Move Turtle Forward 1 space

0006

Function call: Place an egg under the turtle

0007

Function call: Turn Turtle Right

0008

Function call: Move Turtle Forward 1 space

0009

Function call: Place an egg under the turtle

0010

Function call: Turn Turtle Right

0011

Function call: Move Turtle Forward 1 space

0012

Function call: Place an egg under the turtle

0013

The next two turns

0014

make the turtle finish

0015

facing up

0016

Function call: Turn Turtle Right

0017

Function call: Turn Turtle Right

0018

Set X to 0

0019

Display: board after changing X

0020

Display the board

0021

Function call: Lay four eggs in a square

0022

Set X to 5

0023

Display: board after changing X

0024

Display the board

0025

Function call: Lay four eggs in a square

0026

Set X to 8

0027

Display: board after changing X

0028

Display the board

0029

Function call: Lay four eggs in a square

Lines 0003–0017 define the function. Start the main instructions at line 0018: set X to 0, show the board, and call the new function on line 0021. Follow its body, then continue after the call. Next set X to 5 and call it again at line 0025. Finally set X to 8 and call it at line 0029.

Each call lays four eggs in a 2 × 2 square and finishes facing up. The three calls make three squares: twelve marked cells altogether. Instead of copying the same instructions three times, we write the pattern once and reuse it. That makes the program shorter and easier to read or change.

Parameters: give a function the details it needs

A function name follows the same naming idea as a variable name. Some functions also accept parameters: values used for that call. “Move forward 5 squares” gives the movement function a distance of 5. A different call can give it another distance without redefining the movement instructions.

The display instructions in this example label and show the board after changing X. They help you see what happened; they do not lay eggs or turn the turtle. Changing X moves the turtle directly, whereas the forward instruction moves it in the direction of its head.

Returning from a function: when the function finishes, continue at the instruction after the call. A return instruction can finish the function early and can also send back a result. This example reaches the end of its body without an explicit return. A function used mainly for an action, rather than a returned value, is sometimes called a procedure.

5. Input and output

Output: show what happened

Output is information or a visible result produced by a program. In TLAC, it may be a changed board or something written on paper. Here is a very small program:

Move forward five squares

0001

A very small program

0002

Function call: Move forward 5 squares

Before: X=4, Y=9
After: X=4, Y=4

The turtle starts facing up. A distance of 5 moves it from Y=9 to Y=4 while X stays 4. The changed board is the output. A printed message can also report the movement and its move number.

Input: receive information from a player

Input is information received by the program. In TLAC, a player can provide input by moving a piece when asked. The program can read the resulting position and remember it.

Remember a player’s chosen position

0001

User input example

0002

0003

Set the starting position

0004

Move the turtle directly to X=4, Y=9

0005

Ask the player: put the turtle in the top-left corner

0006

Example player response: move to X=0, Y=0

0007

Save the turtle’s X in savedX

0008

Save the turtle’s Y in savedY

0009

Ask the player: put the turtle in the bottom-right corner

0010

Example player response: move to X=9, Y=9

0011

Move the turtle directly to savedX, savedY

  1. Line 0004 sets the usual starting position to X=4, Y=9. Blank line 0002 does nothing.
  2. Line 0005 asks the player to move to the top-left corner. Line 0006 records an example response: X=0, Y=0.
  3. Lines 0007 and 0008 save the current X and Y in savedX and savedY. These are copies of the values at this moment.
  4. Lines 0009 and 0010 ask for and demonstrate a move to the bottom-right corner, X=9, Y=9.
  5. Line 0011 returns the turtle to savedX, savedY. In this example it returns to X=0, Y=0.

The example-response instructions let the saved execution demonstrate the player’s actions. When playing on paper, the person performs those actions. A player may choose another position or make a mistake: the program remembers the position it actually reads, not the position you hoped they would choose. This makes input useful for games where one person acts as the computer and another controls the pieces.

6. Loops

A loop repeats a block of instructions. Instead of writing similar commands many times, describe which values to use or when to stop. Always ask: what repeats, what changes, and when does the repetition finish?

Count upward with a for loop

Use each number from 5 through 9

0001

Count from 5 through 9, including both ends

0002

For each i in 5, 6, 7, 8, 9, repeat

0003

Move the turtle directly to X=i, Y=i

0004

0005

Write: “Counted from 5 to 9”

At line 0002, i takes the values 5, 6, 7, 8, and 9. For each value, line 0003 uses i for both coordinates. The turtle visits (5,5), (6,6), (7,7), (8,8), and (9,9), making a diagonal path. Both 5 and 9 are included; 10 is not. The message on line 0005 is outside the loop, so it is written once after the loop finishes.

Count downward

A countdown is useful for a rocket launch or any job whose remaining count gets smaller. Here the count controls how far the turtle moves each time.

Move 3, then 2, then 1, then 0 squares

0001

Count down from 3 through 0

0002

For each i in 3, 2, 1, 0, repeat

0003

Function call: Move forward i squares

The counter starts at 3 and decreases by 1 each time, including the final value 0. The movements are 3 + 2 + 1 + 0 = 6 squares altogether. Moving zero squares leaves the turtle in place. From X=4, Y=9 facing up, it ends at X=4, Y=3.

Do first, check afterward: a do-while pattern

Sometimes the body must run at least once before checking whether to continue. This example repeats, increases a counter, and leaves the loop when the counter passes a limit. It has the behavior of a do-while loop: perform the body first, then decide whether to repeat.

Stop after the counter passes its limit

0001

Repeat first, then check whether to stop

0002

Set n1 to 1

0003

Set n2 to 3

0004

Set step to 1

0005

0006

The body will run at least once

0007

Repeat until an instruction stops this loop

0008

Write: “The value of n1 is” followed by n1

0009

1. Run the repeated action

0010

Function call: Move forward n1 squares

0011

0012

2. Increase the counter by step

0013

Add step to n1 and save the result in n1

0014

0015

3. Check whether n1 has passed n2

0016

If n1 is greater than n2, leave the loop

0017

If n1 is greater than n2, then

0018

Leave the innermost loop

0019

Write: “n1 has passed the limit”

The starting values are n1=1, n2=3, and step=1. Each pass writes n1, moves forward n1 squares, and adds step to n1. The movement distances are therefore 1, 2, and 3. After the third pass n1 becomes 4, so “n1 is greater than n2” is true and the loop exits. The final message is outside the loop.

Notice that the counter increases here. The condition is a stopping test: leave when n1 is greater than n2, otherwise repeat. Without a change that eventually makes the stopping test true, the loop could continue forever.

Nested loops: multiplication tables

A loop can contain another loop. The outer loop chooses a row; the inner loop works through every item in that row. Here i and j each take the values 1, 2, and 3.

Make a 3 × 3 multiplication table

0001

For each i in 1, 2, 3, repeat

0002

Start with empty output text

0003

0004

The inner loop changes the second number

0005

For each j in 1, 2, 3, repeat

0006

Add each multiplication to the output row

0007

Add “i × j = their product” to output, with spaces

0008

0009

Display the completed row

0010

Display the text in output

0011

0012

Separate the rows with blank lines

0013

Display blank lines

For i=1, the inner loop runs with j=1, j=2, and j=3 before i changes. Then the same three j values are used for i=2 and again for i=3. There are 3 × 3 = 9 multiplications. Start a fresh output string for each row, append the three equations, and display the completed row.

1 × 1 = 1    1 × 2 = 2    1 × 3 = 3
2 × 1 = 2    2 × 2 = 4    2 × 3 = 6
3 × 1 = 3    3 × 2 = 6    3 × 3 = 9

Several loops: build a pyramid

Build each row from spaces and stars

0001

Build a pyramid of characters using

0002

an outer loop and two inner loops

0003

0004

Function definition: Make one pyramid row — inputs: ch, times, triangle_width

0005

Start with empty output text

0006

0007

1. First inner loop: add spaces

0008

Include the final count in this repetition

0009

Set repeat_end to triangle_width − times

0010

Repeat for counts 0 through repeat_end

0011

Add one space to output

0012

0013

2. Second inner loop: add stars

0014

Set repeat_end2 to times × 2 − 2

0015

Repeat for counts 0 through repeat_end2

0016

Add character ch to output

0017

0018

Display the text in output

0019

0020

3. Outer loop: make rows 1 through 5

0021

Use the row numbers 1, 2, 3, 4, 5

0022

For each i in 1, 2, 3, 4, 5, repeat

0023

Make one pyramid row — ch: *, times: i, triangle_width: 5

The outer loop calls the row-making function five times. It passes the star character as ch, the row number as times, and 5 as triangle_width. Inside the function, the first inner loop adds spaces, and the second adds stars. These two inner loops happen one after the other. The completed row is then displayed.

RowSpacesStars
151
243
335
427
519
     *
    ***
   *****
  *******
 *********

In the board game, displaying text means writing the characters on paper, keeping the spaces so that the shape lines up. This program combines functions, parameters, counting, and multiple loops.

7. Arrays and lists

A list stores several values in order. Each value is an item or element. An index tells you an item’s position. In these examples, counting begins at 0: the first item has index 0, the second has index 1, and so on. Arrays are another way to organize indexed values; here we use lists.

For each item: follow a list of turtle commands

A for-each loop visits each item of a list in order. The list below describes a turtle route. Take one command, decide what it means, perform the action, and then take the next command. The list itself becomes data that describes a small program.

Execute eight turtle commands in order

0001

Visit every item in a list

0002

commands: forward, forward, right, forward, forward, left, forward, forward

0003

0004

Take each command in order

0005

For each command in the commands list, repeat

0006

If the command is forward, then

0007

Function call: Move Turtle Forward 1 space

0008

Otherwise, if the command is right, then

0009

Function call: Turn Turtle Right

0010

Otherwise, if the command is left, then

0011

Function call: Turn Turtle Left

The list on line 0002 has eight commands: forward, forward, right, forward, forward, left, forward, forward. The loop begins on line 0005. The conditions choose the matching turtle action for the current command. From X=4, Y=9 facing up, two forward moves reach (4,7); turning right and moving twice reaches (6,7); turning left and moving twice reaches (6,5), facing up.

Read and change a list

Now use a list of fruit names. We keep the stored words in English so that the alphabetical order and the saved output agree in all three versions of this lesson. The initial list contains banana, apple, peach, and pear.

IndexStored value
0banana
1apple
2peach
3pear

The notation fruits[0] means the item at index 0: banana in the initial list. fruits[3] means the fourth item: pear. An index identifies a position, so the word at that position may change when the list is rearranged.

Reverse, sort, add, insert, and remove items

0001

0002

fruits: “banana”, “apple”, “peach”, “pear”

0003

Write the item at index 3 in fruits

0004

0005

Write: “All fruits, in order:”

0006

Repeat for every fruit

0007

For each fruit in fruits, repeat

0008

Write this fruit’s name

0009

0010

Write: “Fruits in reverse order:”

0011

Reverse the order of the list

0012

Reverse the order of the list

0013

For each fruit in fruits, repeat

0014

Write this fruit’s name

0015

0016

Write: “Fruits in alphabetical order:”

0017

Sort the English fruit names alphabetically

0018

Sort the English fruit names alphabetically

0019

For each fruit in fruits, repeat

0020

Write this fruit’s name

0021

0022

Add “appricot” to the end of fruits

0023

Insert “blueberry” at index 2

0024

Insert “blueberry” at index 2

0025

Remove the item at index 3 (the fourth item)

0026

Remove the item at index 3 (the fourth item)

0027

0028

Write: “The final fruit list:”

0029

For each fruit in fruits, repeat

0030

Write this fruit’s name

  1. Create the list on line 0002. Line 0003 writes the item at index 3: pear. The next loop writes every item in its current order.
  2. Reverse it on line 0012: pear, peach, apple, banana. Reversing changes the order, not the words.
  3. Sort it on line 0018: apple, banana, peach, pear. Sorting arranges the English names alphabetically.
  4. Append “appricot” on line 0022. Appending adds an item at the end. This is the spelling stored in the original example; the usual spelling is “apricot”.
  5. Insert “blueberry” at index 2 on line 0024. It becomes the third item; the later items shift one position to the right.
  6. Remove index 3 on line 0026. Index 3 is the fourth item, now peach. The final list is apple, banana, blueberry, pear, appricot.

Remember: list positions start at 0. Reading an item does not remove it. Reversing and sorting change the order; appending, inserting, and removing change the contents or length. After a change, use the list’s new order when interpreting an index.

You have now met the eight foundations: order, variables, statements, conditions, functions, input/output, loops, and lists. Try tracing an example yourself before looking at its output. When these ideas feel familiar, continue with recursion and turtle classes.

Originally written in 2017 for Python 2 and C, updated for Python 3 in 2022, and adapted to TLAC in 2025. This edition presents the same eight topics and examples in English, Spanish, and Japanese.

Back to top

Next: Advanced concepts — recursion and classes →