Stewdent.eu
▾

Variables and Data Types

An introduction to variables and data types in programming, covering how programs store and work with information.

Last updated: 30 January 2026

★☆☆☆☆ Level 1computingprogrammingVariablesdata-typesLesson5 min read

Report an error

What Is a Variable?

Imagine a labeled box sitting on a shelf. You can write a name on the outside of the box — say, "age" — and then put a number inside it. Later you can open the box, read what is inside, or replace the contents with something new. That is exactly what a variable is in programming: a named storage location in a computer's memory that holds a value.

When a program runs, it needs to keep track of information — a player's score, a user's name, the result of a calculation. Variables give programmers a way to store and retrieve those pieces of information by name rather than by raw memory address.

age = 16
name = "Alice"
score = 0

After these lines execute, the computer reserves a small piece of memory for each name and stores the given value inside it. Any time the program later refers to age, it gets back 16.

Declaring and Assigning Variables

Declaration is the act of telling the program that a variable with a certain name exists. Assignment is giving that variable a value. In many languages these happen in one step:

temperature = 22.5

You can change the value stored in a variable at any time:

score = 0
score = score + 10   # score is now 10
score = score + 5    # score is now 15

The right-hand side of an assignment is always evaluated first, then the result is stored in the variable on the left.

Note

A variable's name should clearly describe what it holds. Names like x or temp1 make code hard to read. Names like playerHealth or totalCost make the program's purpose much clearer.

Common Data Types

Not all values are the same kind of thing. A computer needs to know whether a value is a whole number, a decimal, a piece of text, or a yes/no flag — because it stores and processes each kind differently. This is what data types represent.

Integer (int)

An integer is a whole number with no decimal part. It can be positive, negative, or zero.

lives = 3
temperature = -5
year = 2025

Float (float)

A float (short for floating-point number) is a number that can have a fractional part.

price = 9.99
pi = 3.14159
percentage = 72.5

String (str)

A string is a sequence of characters — letters, digits, spaces, punctuation. Strings are written between quotation marks.

greeting = "Hello, world!"
username = "alice_99"
empty = ""

Boolean (bool)

A boolean holds one of only two possible values: True or False. Booleans are the result of yes/no questions and drive decision-making in programs.

isLoggedIn = True
hasWon = False
Tip

Think of a boolean as a light switch — it is either on (True) or off (False). Every time a program makes a decision ("should I show this menu?", "has the player lost all lives?"), it ultimately comes down to a boolean check.

Why Data Types Matter

Data types are not just labels — they determine how the computer stores a value in memory and what operations are allowed on it.

For example, adding two integers gives you the sum:

a = 10
b = 3
result = a + b   # result is 13

But adding two strings concatenates them (joins them together):

first = "Hello"
second = " World"
message = first + second   # message is "Hello World"

Mixing types carelessly leads to errors. Trying to add an integer and a string directly is not meaningful to the computer and will usually cause a type error:

age = 16
greeting = "I am " + age   # ERROR in most languages

To fix this, you must explicitly convert the integer to a string first:

greeting = "I am " + str(age)   # "I am 16"
Warning

Always be aware of what type a variable holds before performing operations on it. Type errors are one of the most common beginner mistakes in programming.

Putting It All Together

Here is a short program that uses several different data types together:

playerName = "Alice"
lives = 3
score = 0.0
isPlaying = True

score = score + 50.5
lives = lives - 1

print("Player: " + playerName)
print("Lives remaining: " + str(lives))
print("Score: " + str(score))

Output:

Player: Alice
Lives remaining: 2
Score: 50.5

Each variable plays a clear role: the string stores a name, the integer counts lives, the float tracks a decimal score, and the boolean could be used to control a game loop.

Practice Problems

What data type would you use to store a person's age?

Show answer
Integer (int), because age is always a whole number.

What is the difference between declaration and assignment?

Show answer
Declaration creates a variable (tells the program it exists). Assignment gives the variable a value. In most languages these happen together: age = 16.

What is the output of: x = 4 followed by x = x + 3?

Show answer
x holds the value 7, because 4 + 3 = 7.

Why would adding a string and an integer cause an error?

Show answer
Because the two types are incompatible — the computer does not know whether to treat the integer as a number to add or as characters to join. You must convert one type first.