Data Types

A classification that tells the computer what kind of value a variable holds — whether it is a number, text, a true/false answer, or something else — which determines what operations are valid and how the value is stored in memory.


What is it?

Not all data is the same kind of thing. The number 42 is different from the text “42” — you can divide the number by 2, but dividing text by 2 makes no sense. Data types are how programming languages enforce this distinction.1

Every variable has a type, even if the language does not require you to declare it explicitly. The type determines:

  • What values the variable can hold
  • What operations are valid (arithmetic on numbers, concatenation on strings)
  • How the value is stored in memory
  • How much memory it occupies

Four primitive types exist in virtually every language:

TypeWhat it storesExample
IntegerWhole numbers42, -7, 0
FloatDecimal numbers3.14, -0.001
StringText (character sequences)"hello", "Marie"
BooleanTrue or falsetrue, false

In plain terms

Data types are container shapes. A bottle holds liquid. A box holds solid objects. A folder holds documents. You would not pour water into a folder or file a document in a bottle. Each container is shaped for its contents — and using the wrong one causes problems.


At a glance


How does it work?

Integers and floats

Integers store whole numbers. Floats (floating-point numbers) store decimals. The distinction matters because computers handle them differently internally — floats use a format that can represent very large and very small numbers but sacrifices precision.2

This is why 0.1 + 0.2 equals 0.30000000000000004 in most languages — a well-known floating-point quirk, not a bug.3

Think of it like...

Integers are like counting with whole coins. Floats are like measuring with a ruler — you can get very precise, but there are always tiny rounding errors at the smallest markings.

Strings

Strings are sequences of characters. They represent text — names, messages, file paths, URLs. Most languages let you combine strings (concatenation), search within them, split them, and transform them.4

name = "Marie"
greeting = "Hello, " + name    # "Hello, Marie"
length = len(name)             # 5
upper = name.upper()           # "MARIE"

Booleans

Booleans hold one of two values: true or false. They are the result of comparisons and the input to control-flow decisions:5

is_adult = age >= 18          # true or false
if is_adult:
    show_content()

Type systems: static vs dynamic

Languages differ fundamentally in how they handle types:

Static typing (Java, C, TypeScript, Rust): You declare the type when you create a variable. The compiler checks types before the program runs. Type errors are caught early.

int age = 32;         // age must always be an integer
age = "thirty-two";   // compile-time ERROR

Dynamic typing (Python, JavaScript, Ruby): The type is inferred from the value. You can reassign a variable to a different type. Type errors are caught at runtime.

age = 32              # age is an integer
age = "thirty-two"    # now it's a string — no error

Neither approach is better

Static typing catches bugs earlier and helps with large codebases. Dynamic typing is faster to write and more flexible. This is a trade-off, not a hierarchy. Many developers use both — Python for scripts, TypeScript for large applications.6


Why do we use it?

Key reasons

1. Correctness. Types prevent nonsensical operations — dividing text by a number, adding a date to a boolean. The earlier these errors are caught, the cheaper they are to fix.

2. Clarity. Types document intent. price: float tells you what a variable holds without reading the rest of the code.

3. Memory efficiency. Types tell the computer how much memory to allocate. An integer needs 4-8 bytes. A boolean needs 1. A string needs as many bytes as it has characters.

4. Performance. The CPU processes integers, floats, and booleans differently. Knowing the type in advance lets the compiler generate optimal machine code.


When do we use it?

  • Every time you create a variable — it always has a type
  • When designing functions — parameter types define the contract (what inputs are valid)
  • When debugging — “TypeError: cannot concatenate str and int” is a type mismatch
  • When choosing a language — the type system is one of the most impactful design decisions

Rule of thumb

If you can read a piece of code and know what type every variable holds, the code is clear. If you cannot tell, the code needs better names or type annotations.


How can I think about it?

The container shapes

Data types are like different containers in a kitchen. A measuring cup holds liquids (floats). A counting bowl holds whole items (integers). A label maker prints text (strings). A light switch has two positions (boolean).

You would not measure flour in a light switch. You would not count eggs with a label maker. Each container is designed for its kind of data, and using the wrong one produces nonsense or errors.

The form fields

Think of a web form. Each field has a type: the “age” field accepts only numbers. The “name” field accepts text. The “agree to terms” checkbox is true/false.

If you type your name into the age field, the form rejects it. That is type checking — the form enforces that each field receives the kind of data it expects.


Concepts to explore next

ConceptWhat it coversStatus
variablesThe named containers that hold typed valuescomplete
data-structuresComposite types — arrays, dictionaries, objectscomplete
functionsHow types define function contractscomplete

Check your understanding


Where this concept fits

Position in the knowledge graph

graph TD
    SD[Software Development] --> VAR[Variables]
    SD --> DT[Data Types]
    VAR --> DT
    DT --> DS[Data Structures]

    style DT fill:#4a9ede,color:#fff

Related concepts:

  • variables — the containers that hold typed values
  • data-structures — composite types that organise multiple values
  • functions — types define what inputs a function accepts and what it returns

Sources


Further reading

Resources

Footnotes

  1. Sebesta, R. (2019). Concepts of Programming Languages. 12th ed. Pearson.

  2. IEEE 754 standard defines how floating-point numbers are represented in binary. IEEE. (2019). IEEE Standard for Floating-Point Arithmetic.

  3. Goldberg, D. (1991). What Every Computer Scientist Should Know About Floating-Point Arithmetic. ACM Computing Surveys, 23(1).

  4. Wikipedia. (2026). String (computer science). Wikipedia.

  5. TripleTen. (2024). Basic Coding Concepts. TripleTen.

  6. Pierce, B. (2002). Types and Programming Languages. MIT Press.