Tutelage

Lessons › coding and computer science · taught by Leo

Variables and data types in programming

Learn what variables are, how assignment works, and how data types like numbers and strings affect code.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

Why programs need names

Think of a variable as a name tag for a value. It lets your program save something and use it again.

Without a name, the value is easy to lose. With a name, you can point to it later.

Variable = name + value

A variable is a labeled place in memory that holds a value.

The name is what you write. The value is what is stored there.

For example, score can hold 10, and age can hold 16.

Same idea in code: you give a value a label so you can reuse it.

Predict the variable role

In the code: age = 16. What is age doing here?

  1. It is the variable name
  2. It is the data type
  3. It is a comment
Show answer

It is the variable name: Yes. age is the name that stores the value.

Assign a value to a name

Here is the basic move. First you choose the name, then you assign the value.

The equal sign in programming usually means store this value in this name, not math equality.

After that, the name can stand in for the value anywhere you use it.

Quick check on assignment

What does x = 5 usually mean in code?

  1. x gets the value 5
  2. x is equal to 5 forever
  3. x means 5 plus 1
Show answer

x gets the value 5: Correct. It stores 5 in x.

Kinds of data: the type

Now we get to data types. A type tells you what kind of value something is.

Numbers can be used in calculations. Text, or strings, is just characters in quotes.

That difference matters because code treats them differently.

Why types matter

The type tells the computer what operations make sense.

If something is a number, you can add it, subtract it, or compare it.

If something is text, the computer reads it as letters and symbols.

So the type helps prevent mistakes.

Pick the data type

Which value is text, not a number?

  1. 42
  2. 3.14
  3. "42"
Show answer

"42": Yes. Quotes usually mean text, so that is a string.

Store data with a type

A variable can hold different kinds of data, like a number or text.

The type tells you how that value behaves.

So when you read code, always ask: what is the name, and what kind of value is inside?

Your turn: name and type

In username = "Mia", what is the data type of the value?

  1. number
  2. string
  3. boolean
Show answer

string: Correct. "Mia" is text, so it is a string.

Common mistake: name vs value

A common mistake is mixing up the name and the value. They are not the same thing.

Another mistake is forgetting the type. Numbers and strings do different jobs.

If you separate name, value, and type, the code gets much easier to read.

Quick memory checklist

Here is your checklist. First, choose a name.

Second, store a value. Third, check the type.

Last, use the name again when you need the value.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app