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:
| Type | What it stores | Example |
|---|---|---|
| Integer | Whole numbers | 42, -7, 0 |
| Float | Decimal numbers | 3.14, -0.001 |
| String | Text (character sequences) | "hello", "Marie" |
| Boolean | True or false | true, 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
The type landscape (click to expand)
graph TD DT[Data Types] --> P[Primitive] DT --> C[Composite] P --> INT[Integer] P --> FLT[Float] P --> STR[String] P --> BOOL[Boolean] C --> ARR[Array / List] C --> OBJ[Object / Dictionary] style DT fill:#4a9ede,color:#fff style P fill:#5cb85c,color:#fff style C fill:#e8b84b,color:#fffKey: Primitive types hold single values. Composite types hold collections of values and are covered in data-structures.
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 ERRORDynamic 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 errorNeither 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: floattells 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
| Concept | What it covers | Status |
|---|---|---|
| variables | The named containers that hold typed values | complete |
| data-structures | Composite types — arrays, dictionaries, objects | complete |
| functions | How types define function contracts | complete |
Check your understanding
Test yourself (click to expand)
- Explain what a data type is and name the four universal primitive types.
- Describe why
0.1 + 0.2does not equal exactly0.3in most programming languages.- Distinguish between static typing and dynamic typing. Name two languages that use each approach.
- Interpret this error message: “TypeError: cannot concatenate ‘str’ and ‘int’ objects.” What went wrong?
- Connect data types to memory-management: why does knowing the type help the computer allocate memory?
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:#fffRelated 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
- Types and Programming Languages — Pierce’s academic reference on type systems, for those who want the deep theory
- What Every Programmer Should Know About Floating-Point — Clear visual explanation of why 0.1 + 0.2 is not 0.3
- Python Data Types — Real Python’s practical guide to types in Python
Footnotes
-
Sebesta, R. (2019). Concepts of Programming Languages. 12th ed. Pearson. ↩
-
IEEE 754 standard defines how floating-point numbers are represented in binary. IEEE. (2019). IEEE Standard for Floating-Point Arithmetic. ↩
-
Goldberg, D. (1991). What Every Computer Scientist Should Know About Floating-Point Arithmetic. ACM Computing Surveys, 23(1). ↩
-
Wikipedia. (2026). String (computer science). Wikipedia. ↩
-
TripleTen. (2024). Basic Coding Concepts. TripleTen. ↩
-
Pierce, B. (2002). Types and Programming Languages. MIT Press. ↩
