Contents

Programming Fundamentals › Programming Basics

Data Type

The kind of value something is, which determines what operations are valid on it.

Also known as: data types, type of a value, types, value type

A data type says what kind of value something is, and so what you can do with it. You can add numbers, but not meaningfully “add” a boolean and a date.

TypeExamplesTypical operations
Integer42, -7arithmetic (integers)
Float3.14arithmetic, approximate (floating point)
String"hello"concatenation, searching (strings)
Booleantrue, falselogic (boolean)
List / array[1, 2, 3]indexing, appending
Dictionary / object{"name": "Ana"}lookup by key
Null / noneNone, null“no value” (null)
type(42)        # <class 'int'>
type("42")      # <class 'str'>
42 + 1          # 43
"42" + "1"      # "421"   (strings concatenate)
"42" + 1        # TypeError

The text "42" and the number 42 look alike and behave differently. Many bugs come from mixing them.

Static vs dynamic typing

  • Statically typed languages (Java, Go, TypeScript) check types before running, and you declare them.
  • Dynamically typed languages (Python, JavaScript) check types while running, so a wrong type shows up as an error at that line (static vs dynamic typing).

Converting

You can convert between types, sometimes explicitly (int("42")) and sometimes automatically, with surprises (type conversion, type coercion).

Habits

  • Know the type of each value, especially data coming from outside (forms, JSON, CSV), which is often all strings.
  • Choose the right type: money as integers or decimals, dates as dates, IDs as strings when you don’t do arithmetic on them.
  • When an error says “unsupported operand type”, check the types first.