Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
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 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.
true or falseโforming the basis of all logical operationsif statement, while loop, and conditional expression evaluates to a booleanAND, OR, and NOT combine booleans to create complex conditions'A' = 65 in ASCII), enabling character arithmeticCompare: 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.
These types handle textual data, from single characters to complex strings. Understanding how strings are stored and manipulated is critical for text processing tasks.
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.
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?).
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).
These collections organize data by relationships rather than just position. They're optimized for fast lookups and ensuring uniqueness.
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.
| 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 |
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?