Lesson 2 — Values and variables
You are here
- Your first program ✓
- Values and variables ←
- Making decisions
- Repeating things
- Functions
- Lists and hashes
- Strings
- Organizing programs
- Classes
What are we learning?
A value is a piece of data: text, a number, true/false, and so on.
A variable is a name that refers to a value, so you can use that value again.
The idea in ordinary language
Think of a labeled box:
- The label is the variable name (
name). - What is inside the box is the value (
"Echo").
Smallest useful example
name: str = "Echo";
say(name);Output
EchoLine by line
name— the label (variable name).: str— this name holds text. In Echo,strmeans text (short for “string”).= "Echo"— put the textEchoin that name.;— end of the instruction.say(name);— print whatevernamecurrently refers to (not the wordname, but the value).
Why : str?
Echo requires a type on a new variable. A type is a short label for the kind of value allowed. You will see str (text), int (whole numbers), bool (true / false), and others later.
More kinds of values
title: str = "Echo";
users: int = 42;
ready: bool = true;
say(title, users, ready);Output
Echo 42 trueint— whole numbers (1,42,-3)bool—trueorfalse
Changing a value
After you declare a name, you can give it a new value of the same kind:
count: int = 1;
count = 2;
say(count);Output
2Notice the second line has no : int — the name already exists.
Try it — change the value
Start from:
name: str = "Echo";
say(name);Change "Echo" to your name and run again.
Predict it
What will this print?
city: str = "Paris";
say(city);Show answer
ParisFix it — name used before it exists
This fails:
count = 1;Echo needs the type the first time:
count: int = 1;Deeper (later)
Unions, destructuring, const, dynamic, and class types are covered in Variables and Types (full). Skip that until you finish the Learn path.
