Beginner tutorial
These lessons are for someone new to programming. You will write a temperature report using sample readings, so you need a computer but no sensor or ESP32. If you already program, start with Toit for programmers.
You should be comfortable creating a text file and opening a terminal. The terminal is where you type the command that runs your program. We explain the programming concepts as they appear.
| Lesson | What you will make |
| 1. Your first program, on this page | Report whether one temperature is warm. |
| 2. Lists and functions | Report several readings using one reusable operation. |
| 3. Objects | Keep a room's name and readings together. Continue here when you are ready. |
Each example is a complete program. Replace the contents of your file rather than appending the example to the previous one. Run it after each change.
1. Run your first program
Install the Toit SDK, which includes
the tools to run Toit programs. Check the installation by typing toit --help
in a terminal. It should display instructions for the command.
If you already have Jaguar installed, use jag toit instead of toit in these
commands; you do not need to connect a device.
In a text editor, create a file named report.toit containing these two lines:
The two spaces before print matter. Use spaces, not the Tab key.
Save the file, open a terminal in the directory containing it, and type:
You should see:
main: marks where the program starts. The indented line underneath it is an
instruction: print displays the text between the double quotes. Text written
this way is called a string. The quotes are part of the program, but are not
printed.
Change Temperature report to Kitchen report, save, and run the command
again. The new message should appear. The program runs the saved version of
your file, so remember to save before running.
2. Give a value a name
We want the report to include a temperature. We could print 21 directly, but
naming it lets us use the same value in several places.
A variable is a name for a value. temperature := 21 creates a variable named
temperature with the value 21. The := introduces that new name.
This prints 21. There are no quotes around temperature: we want its value,
not the word “temperature”. Instructions run from top to bottom, so the variable
is created before print uses it.
Change 21 to 24, save, and run again. Then try print "temperature" and
compare the output. Put print temperature back before continuing.
3. Change the value
Once a variable exists, use = to replace its value. Use := only when
introducing the name.
The output is:
The first print uses the value at that point in the program. Changing the
variable later does not change text that has already been printed.
You can calculate a new value from the old one. Replace temperature = 24
with temperature = temperature + 3. Toit first adds three to the current
value, then stores the result. The output should stay the same.
Try adding five instead. Predict the second line before running the program.
4. Put the value in a message
To print a label and a value together, put $temperature inside a string.
Toit replaces it with the variable's value. This is called interpolation.
This prints:
Change the value to 24. You only need to change the line that sets the
temperature; the message uses that value automatically.
5. Decide what to print
Our program should say “Warm” when the temperature is at least 25 degrees.
Otherwise, it should say “Comfortable”. The question “is this temperature at
least 25?” is written as temperature >= 25.
main:
temperature := 24
print "Temperature: $temperature C"
if temperature >= 25:
print "Warm"
else:
print "Comfortable"This prints:
if asks the question. When the answer is yes, the program runs the further
indented instruction below it. When the answer is no, it runs the instruction
below else: instead. Only one of the two messages is printed.
The comparison is called a condition. Its result is true for yes or false
for no. Those two values are called booleans.
Try temperatures of 25 and 30. Both should print Warm. Try 20, which
should print Comfortable. You have now checked values below, at, and above
the threshold.
Practice before continuing
Change the report so that it describes a freezer: print Freezing when the
temperature is below zero, and Not freezing otherwise. Use < for “less than”.
Check the program with -5, 0, and 5.
You have a complete program that stores a reading, includes it in a message, and chooses a description. Continue with lists and functions to report several readings without copying the same instructions for each one.