Skip to content
GetITechSkills

Logic and Programming

Variables and Data Types Explained

Beginner · Concept · 6 min read · Updated October 30, 2026

Imagine a shelf in a library with a label on each box. The label says "overdue fines" or "opening hours", and the box holds the actual thing. You can swap what is in a box without changing the label, and anyone who reads the label knows where to look. A variable works the same way in a program. It is a label attached to a value that the computer keeps in memory.

Values come in different kinds, and the kind decides what you can do with them. You can add two numbers, but adding two names means something else. This entry covers how to create variables, the main kinds of data, how to change one kind into another, and the mistakes that catch most beginners.

What a variable is

A variable is a name that points to a stored value. You create one by giving a name and a value in a single line, which is called an assignment. In Python it looks like this:

title = "Garden Notes"
pages = 240
price = 12.50
in_stock = True

The equals sign here does not mean "is equal to" the way it does in math class. It means "store the value on the right under the name on the left". Read the first line as "title now refers to the text Garden Notes".

Because the label and the value are separate, you can point a name at a new value at any time:

price = 12.50
price = price - 2
print("Sale price:", price)

This prints Sale price: 10.5. The second line first works out the right side (12.50 minus 2 is 10.5) and then stores that result back under the name price. Python shows 12.50 as 12.5, since trailing zeros are not kept in a number.

In JavaScript the same idea is spelled with a keyword in front, such as let price = 12.5; for a value that may change and const title = "Garden Notes"; for one that should not be reassigned.

Choosing good names

A name is a message to the next reader, and that reader is often you in a month. price is clearer than p, and days_until_due is clearer than d. Most languages have a few rules about names. In Python they can contain letters, digits and underscores, they cannot start with a digit, and they are case-sensitive, so Price and price are two different variables. Programmers also follow style habits. Python code usually writes multi-word names in lowercase with underscores, while JavaScript code usually writes them in camelCase, like daysUntilDue.

The main data types

A data type describes what kind of value something is and what operations make sense on it. Most languages share the same basic set:

Type Python name What it holds Example
Whole number int Counts with no decimal part 240
Decimal number float Numbers with a fractional part 12.5
Text str A sequence of characters in quotes "Garden Notes"
True or false bool One of two values True
Nothing None A deliberate "no value" None

You can ask Python what type a value has with type():

print(type(pages))
print(type(price))
print(type(title))
print(type(in_stock))

The output is:

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

Numbers

Whole numbers and decimals behave as you would expect for plus, minus, times and divide. One detail surprises people: in Python, the / operator always gives a decimal result, even when the answer is whole. So 7 / 2 is 3.5, and 6 / 2 is 3.0. If you want the whole-number part of a division, use //, so 7 // 2 is 3.

Decimal numbers are stored in a way that cannot represent every fraction exactly. The classic example is 0.1 + 0.2, which Python prints as 0.30000000000000004. This is not a bug in Python. Most languages behave the same way. For money, programmers often store whole cents as integers or use special decimal tools, and they compare decimals with a small tolerance rather than exact equality.

Text

Text values are called strings. You put them in quotes, single or double. You can join strings with +, and you can place values inside text with an f-string:

print(f"{title} has {pages} pages")

This prints Garden Notes has 240 pages. The letter f before the opening quote tells Python to fill in whatever is inside the curly brackets.

True and false

A boolean holds only True or False. Booleans are the result of every comparison, such as pages > 200, and they are what decisions are built on. The entry on if and else shows how programs use them.

Converting between types

Types matter most when they are mixed up. Look at what happens with the number 3 written as text:

quantity = "3"
print(quantity + quantity)
print(int(quantity) + int(quantity))

The first line prints 33, because adding two strings joins them. The second prints 6, because int() converts each string to a whole number first. Converting a value from one type to another is called type conversion, or casting. The matching functions are int(), float() and str().

Conversion can fail. int("three") raises an error, because the text is not a number. This matters because data coming from a form or a file often arrives as text, even when it looks like a number. A safe habit is to convert early and to expect that some input will be wrong.

Some languages convert types quietly for you. In JavaScript, "3" + 4 gives "34" while "3" - 1 gives 2, and the rule that decides which one happens is easy to forget. Python is stricter: "3" + 4 stops with an error. Strict languages feel harsher at first, but they point at the problem sooner.

A note on typing styles

Python and JavaScript are dynamically typed, which means a variable can point to a number now and text later. Languages such as Java and C# are statically typed, which means each variable has a fixed type declared in advance. Both approaches work. The trade-off is flexibility against early warnings.

Truthy and falsy values

Many languages let you use a non-boolean value in a decision. Python treats 0, 0.0, empty text "", an empty list and None as false, and most other values as true. The Python reference lists exactly which built-in values count as false. This shortcut is handy, but it can hide bugs, such as a score of 0 being treated as "no score at all". When the difference matters, compare explicitly.

Common mistakes

  • Confusing = with ==. One equals sign stores a value. Two equals signs ask whether two values are equal. Mixing them up changes the program or stops it.
  • Using text where a number is needed. Data from a form arrives as text. Convert it before doing math, or you get joined strings instead of sums.
  • Misspelling a name. totl and total are two different names. Most languages report an error for a name that was never created, and that message usually points to a typo.
  • Trusting decimals to be exact. Small rounding differences are normal. Round when you display a result, and avoid comparing decimals for exact equality.
  • Reusing one variable for unrelated things. A name that holds a price on one line and a customer name on the next is confusing. Give each idea its own name.
  • Starting a name with a digit. 2nd_place is not allowed in Python. Use a name such as second_place.

FAQ

What is the difference between a variable and a constant?

A variable can be given a new value later. A constant is meant to stay the same, such as a tax rate used across a program. JavaScript has const for this. Python has no strict constants, so programmers write the name in capital letters, like TAX_RATE, as a signal.

Does a variable store the value itself?

In practice, a variable refers to a value held in the computer's memory. For a beginner, thinking of it as a labeled box is accurate enough for almost every task.

Why do languages have separate types for whole numbers and decimals?

They are stored differently and behave differently. Whole numbers are exact, while decimals trade exactness for range. Keeping them apart helps the computer and the programmer avoid surprises.

What happens if I use a variable before creating it?

The program stops with an error that names the missing variable. In Python the error is called a NameError. Check the spelling and make sure the assignment runs earlier than the line that uses it.

How do I store many values under one name?

Use a collection such as a list or a dictionary. They are covered in lists, arrays and dictionaries, and the whole area is mapped on the Logic and Programming page.

Sources

  1. An Informal Introduction to Python (opens in a new tab) — Python Software Foundation (docs.python.org). Numbers, strings and assignment in the official tutorial
  2. Built-in Types (opens in a new tab) — Python Software Foundation (docs.python.org). Reference for int, float, bool, str and truth value testing
  3. Grammar and types (JavaScript Guide) (opens in a new tab) — MDN Web Docs (Mozilla). How JavaScript declares variables and which types it has

Next in Logic and Programming: If and Else: How Programs Make Decisions

  • Logic and Programming

    What Is Programming? A Simple Introduction

    Programming means writing exact instructions for a computer. Learn what code is, how a program runs, and what a first small program looks like.

    • Beginner
    • Concept
    • 7 min read

    Updated October 30, 2026

  • Logic and Programming

    If and Else: How Programs Make Decisions

    Learn how if, elif and else let a program choose between paths, how comparisons and and/or/not work, and the mistakes that break decisions.

    • Beginner
    • Concept
    • 6 min read

    Updated October 30, 2026