upgrade
upgrade

๐ŸงตProgramming Languages and Techniques I

Common Programming Data Types

Study smarter with Fiveable

Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.

Get Started

Why This Matters

Data types are the foundation of every program you'll write. When you declare a variable, choose a collection, or pass information between functions, you're making decisions about data typesโ€”and those decisions affect memory usage, performance, and what operations are legal in your code. Understanding the distinctions between primitive types like integers and floats, or between mutable and immutable collections, helps you avoid bugs and write more efficient programs.

You're being tested on more than just definitions here. Exam questions will ask you to choose the right data type for a specific scenario, explain why one collection type outperforms another, or predict what happens when types interact. Don't just memorize that a boolean holds true or falseโ€”know when to use it for control flow, how it interacts with logical operators, and why it's fundamental to conditionals. Master the why behind each type, and you'll handle any question thrown at you.


Primitive Types: The Building Blocks

Primitive types store single, simple values directly in memory. They're the most basic units of data your program can work with, and every complex structure ultimately breaks down into these fundamentals.

Integer

  • Whole numbers onlyโ€”no decimal points allowed, making them ideal for counting, indexing, and loop counters
  • Memory-efficient for exact values; integers avoid the precision issues that plague floating-point numbers
  • Arithmetic operations like ++, โˆ’-, โˆ—*, and integer division (////) return predictable, exact results

Float

  • Decimal representation allows fractional valuesโ€”essential for measurements, scientific calculations, and financial data
  • Floating-point arithmetic can introduce rounding errors because computers store decimals in binary approximation
  • Type coercion often occurs when mixing floats and integers; the result typically becomes a float (e.g., 5+2.0=7.05 + 2.0 = 7.0)

Boolean

  • Two possible valuesโ€”true or falseโ€”forming the basis of all logical operations
  • Control flow essential; every if statement, while loop, and conditional expression evaluates to a boolean
  • Logical operators like AND, OR, and NOT combine booleans to create complex conditions

Char

  • Single character storageโ€”one letter, digit, or symbol represented internally as an integer via encoding
  • ASCII and Unicode map characters to numeric values (e.g., 'A' = 65 in ASCII), enabling character arithmetic
  • String foundation; in many languages, strings are essentially arrays or sequences of char values

Compare: Integer vs. Floatโ€”both represent numbers, but integers are exact while floats allow decimals at the cost of potential precision errors. If an exam asks when to use each, think: counting and indexing โ†’ integer; measurement and division โ†’ float.


Text and Sequence Types: Working with Characters

These types handle textual data, from single characters to complex strings. Understanding how strings are stored and manipulated is critical for text processing tasks.

String

  • Character sequences enclosed in quotes; supports letters, numbers, symbols, and whitespace
  • Immutable in many languagesโ€”operations like concatenation create new strings rather than modifying the original
  • Rich operations include slicing (str[0:5]), searching (find()), and concatenation (+ or .join())

Compare: Char vs. Stringโ€”a char holds exactly one character, while a string holds zero or more. Some languages (like Java) distinguish them strictly; others (like Python) treat single characters as one-character strings.


Collection Types: Ordered Sequences

Ordered collections maintain element position, allowing you to access items by index. The key distinctions are mutability (can you change it?) and homogeneity (must all elements be the same type?).

Array

  • Fixed size determined at creationโ€”you cannot add or remove elements after initialization
  • Homogeneous elements; all items must be the same data type, enabling efficient memory allocation
  • Contiguous memory storage allows O(1)O(1) constant-time access by index, making arrays extremely fast for lookups

List

  • Dynamic sizing lets you append, insert, or remove elements freely as your program runs
  • Heterogeneous elements allowed in most languagesโ€”a single list can hold integers, strings, and objects together
  • Flexible but slower; dynamic resizing and mixed types can introduce overhead compared to arrays

Tuple

  • Immutable after creationโ€”once defined, elements cannot be added, removed, or changed
  • Mixed data types supported, commonly used to group related values (like coordinates: (x,y)(x, y))
  • Function returns often use tuples to pass multiple values back without creating a custom object

Compare: Array vs. Listโ€”arrays are fixed-size and homogeneous (faster, less flexible), while lists are dynamic and heterogeneous (more flexible, potential overhead). Choose arrays when size is known and performance matters; choose lists when you need flexibility.

Compare: List vs. Tupleโ€”both are ordered and allow mixed types, but lists are mutable while tuples are immutable. Use tuples when data shouldn't change (like dictionary keys or function returns).


Collection Types: Key-Value and Unique Elements

These collections organize data by relationships rather than just position. They're optimized for fast lookups and ensuring uniqueness.

Dictionary/Map

  • Key-value pairs where each unique key maps to a valueโ€”think of it as a lookup table
  • Fast retrieval with average O(1)O(1) time complexity for accessing values by key
  • Keys must be immutable (strings, integers, tuples), but values can be any data type including other collections

Set

  • Unique elements onlyโ€”duplicates are automatically removed upon insertion
  • Unordered storage means you cannot access elements by index; order is not guaranteed
  • Set operations like union (AโˆชBA \cup B), intersection (AโˆฉBA \cap B), and difference (Aโˆ’BA - B) mirror mathematical set theory

Compare: Dictionary vs. Setโ€”both use hashing for fast lookups, but dictionaries store key-value pairs while sets store only unique values. Use a dictionary when you need to associate data; use a set when you only care about membership and uniqueness.


Quick Reference Table

ConceptBest Examples
Exact numeric valuesInteger, Char (as numeric code)
Decimal/fractional valuesFloat
Logical control flowBoolean
Text manipulationString, Char
Fixed-size ordered collectionArray
Dynamic ordered collectionList
Immutable ordered collectionTuple
Key-value associationDictionary/Map
Unique element storageSet
Mutable collectionsList, Dictionary, Set
Immutable typesInteger, Float, Boolean, String, Tuple

Self-Check Questions

  1. Which two collection types are ordered and allow mixed data types? What's the key difference between them?

  2. You need to store student IDs mapped to their grades for fast lookup. Which data type is most appropriate, and why?

  3. Compare arrays and lists: when would you choose an array over a list despite its fixed-size limitation?

  4. A function needs to return both a status code (integer) and an error message (string). Which data type would you use to return both values, and why is it preferable to a list?

  5. You're processing a large dataset and need to eliminate duplicate entries while also checking whether specific values exist. Which data type offers the best performance for both tasks, and what is its average time complexity for membership testing?