Skip to content
Happy Programming Guide
Start learning
Programming Basics

Variables Explained With Simple Examples

A variable is a name you attach to a value so your program can use it again. Here is how variables work, how to name them well, and the three mistakes almost every beginner makes.

A variable is a name you attach to a piece of information so your program can use it again later. That is it. Everything else is detail.

Python
age = 12
print(age)

You gave the number 12 the name age. From now on, writing age means 12.

Why bother naming things?#

Compare these two lines:

Python
print(40 * 12 * 0.8)
Python
hours = 40
rate = 12
after_tax = 0.8
print(hours * rate * after_tax)

Both print the same number. Only the second one tells you what the number means, and only the second one is easy to change later. Variables are mostly about making code readable by a human — usually you, in two weeks, having forgotten everything.

Creating a variable#

In Python you just pick a name and assign a value:

Python
city = "Lahore"
temperature = 31
is_raining = False

In JavaScript you add a keyword in front, usually let or const:

JavaScript
let city = "Lahore";
let temperature = 31;
const isRaining = false;

const means “I do not intend to reassign this name”. let means “this may change”. More detail in JavaScript variables explained.

The equals sign does not mean equals#

This trips up nearly everyone who did algebra at school. In most programming languages, = means “put the value on the right into the name on the left”. It is an instruction, not a statement of fact.

Python
count = 5
count = count + 1
print(count)   # 6

In maths, count = count + 1 is nonsense. In code it reads as: take the current value of count, add one, and store the result back under the same name.

To ask whether two things are equal, you use == instead. Mixing these two up is one of the most common beginner errors — see if statements explained.

Changing what a variable holds#

A variable holds one value at a time. Assigning a new value replaces the old one.

Python
score = 0
score = 10
score = 25
print(score)   # 25

The earlier values are gone. If you need to keep several values, you want a list — see arrays and lists explained.

Naming variables well#

Every language has rules and conventions. The rules stop your code running if you break them. The conventions just make you easier to work with.

Rules#

  • Start with a letter or an underscore, never a number. 2nd_place is invalid; second_place is fine.
  • No spaces. Use an underscore or capital letters instead.
  • No reserved words. You cannot name a variable if, for or class.
  • Case matters. total and Total are two different variables.

Conventions#

  • Python uses snake_case: user_name, total_price.
  • JavaScript uses camelCase: userName, totalPrice.
  • Say what the thing is, not what type it is. email beats str1.
  • Short names are fine for short-lived things. A loop counter can be i. A value used across fifty lines should not be.

A worked example#

A small program that uses variables for everything it might want to change later:

Python
item = "Notebook"
unit_price = 250
quantity = 3
discount = 0.1

subtotal = unit_price * quantity
saving = subtotal * discount
total = subtotal - saving

print("Item:", item)
print("Subtotal:", subtotal)
print("You saved:", saving)
print("Total:", total)

Change quantity to 10 and everything below updates on its own. That is the payoff: one edit in one place.

Questions people ask#

What is the difference between a variable and a constant?

A constant is a value you do not intend to change after setting it. JavaScript enforces this with const. Python does not enforce it, but programmers write constants in capitals, like TAX_RATE = 0.2, as a signal to other readers.

How many variables should a program have?

As many as it needs to stay readable, and no more. If you find yourself naming things data2 and temp3, that usually means a section of code is trying to do too much and would be clearer split into a function.

Do variables store the value or a reference to it?

It depends on the language and the type. For simple numbers and text you can safely think of it as storing the value. For lists and objects, two names can point at the same underlying thing, so changing it through one name changes it for both. That surprise is worth knowing about before it bites you.

Can a variable hold different types at different times?

In Python and JavaScript, yes — you can assign a number and later assign text to the same name. It is allowed, but it makes code harder to follow, so most people avoid it.

Where to go next#

Next lessonWhat are data types?

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *