🧵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.
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., )
Boolean
- Two possible values—
trueorfalse—forming the basis of all logical operations - Control flow essential; every
ifstatement,whileloop, and conditional expression evaluates to a boolean - Logical operators like
AND,OR, andNOTcombine 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 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: )
- 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 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 (), intersection (), and difference () 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
| Concept | Best Examples |
|---|---|
| Exact numeric values | Integer, Char (as numeric code) |
| Decimal/fractional values | Float |
| Logical control flow | Boolean |
| Text manipulation | String, Char |
| Fixed-size ordered collection | Array |
| Dynamic ordered collection | List |
| Immutable ordered collection | Tuple |
| Key-value association | Dictionary/Map |
| Unique element storage | Set |
| Mutable collections | List, Dictionary, Set |
| Immutable types | Integer, Float, Boolean, String, Tuple |
Self-Check Questions
-
Which two collection types are ordered and allow mixed data types? What's the key difference between them?
-
You need to store student IDs mapped to their grades for fast lookup. Which data type is most appropriate, and why?
-
Compare arrays and lists: when would you choose an array over a list despite its fixed-size limitation?
-
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?
-
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?