Programming Fundamentals

Data Types

The kind of value: integers, floats, booleans, text, and collections. The type decides the operations, the memory, and the pitfalls.

Level: beginner

What is it?

A data type says what a value is and what you can do with it. Primitive types hold single values: integers, floating-point numbers, booleans, and characters. Composite types group values: strings, arrays, lists, dictionaries, structs. The type determines the bit layout in memory, the legal operations, and the ways the value can surprise you.

Why does it matter?

Types are where most arithmetic and text bugs live: integer overflow, floating-point rounding, off-by-one on strings, or a null where a number should be. Knowing the representation explains behavior no tutorial mentions, such as why 0.1 + 0.2 is not 0.3.

Mental model

Units on a number. Ten what — meters, seconds, characters? A type is the unit system the compiler or runtime uses to refuse nonsense, like adding a phone number to a temperature.

How it works

  • Integers are exact but bounded in most languages (32 or 64 bits). Python integers grow as large as memory allows.
  • Floating point (IEEE 754) stores a sign, an exponent, and a fraction. Many decimals, 0.1 among them, have no exact binary form, so compare with a tolerance.
  • Booleans hold true or false. Many languages also treat 0, the empty string, and null as 'falsy', which is convenient and error-prone.
  • Strings are sequences of characters. With Unicode, length in characters, code points, and bytes can all differ.
  • Collections come in three shapes: ordered sequences (list, array), key-to-value maps (dict), and unique sets. Choose by the operation you do most.
  • Converting between types can lose information: float to int truncates, and string to int can fail.

Steps

  • Pick the type that fits the value
  • Store it in a fixed layout
  • Apply only legal operations
  • Convert deliberately when needed
  • Check ranges and edge values

Code example: Representation surprises

print(0.1 + 0.2 == 0.3)                 # False
print(abs((0.1 + 0.2) - 0.3) < 1e-9)    # True: compare with a tolerance
print(len("héllo"), len("héllo".encode("utf-8")))   # 5 6  (chars vs bytes)
print(int("42") + 1)                    # 43
print(int(3.9))                         # 3  (truncates, does not round)

Real-world usage

  • Choosing the representation for money, time, identifiers, and text
  • Reading and validating input from files, forms, and networks
  • Serialization: JSON, database columns, and protocol fields all carry types

Common mistakes

  • Storing money in a float.
  • Assuming len(text) is the number of bytes on the wire.
  • Comparing floats with == instead of a tolerance.

Interview questions

Why is 0.1 + 0.2 not exactly 0.3?

Binary floating point cannot represent 0.1 or 0.2 exactly, so their sum lands a tiny distance from 0.3. Compare with a tolerance, or use integer cents or a decimal type for money.

What happens on integer overflow?

In C, signed overflow is undefined behavior. In Java it wraps around silently. Python integers promote to arbitrary precision. Know your language's rule before trusting arithmetic near the limits.

Why is a string's length not always its size in bytes?

Unicode characters take one to four bytes in UTF-8, so a text with accents or emoji has more bytes than characters. APIs that count bytes and APIs that count characters disagree on such input.

TypeExampleWatch out for
int42Overflow in fixed-width languages
float3.14Rounding, NaN, comparing with ==
boolTrueTruthiness of 0, '' and null
str'héllo'Bytes versus characters
list / array[1, 2, 3]Index out of range
dict / map{'a': 1}Missing key

Prerequisites

Related concepts

Continue learning

Browse the full computer science library