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 everything you'll build in Python. When you're writing code, Python needs to know what kind of data you're working with—is it a number you can do math on? Text you want to display? A collection of items you need to loop through? Understanding data types means understanding how Python thinks about information, and that determines what operations you can perform, how much memory your program uses, and whether your code will run or crash.
You're being tested on more than just definitions here. Exams will ask you to predict behavior, choose the right type for a task, and debug type-related errors. The concepts that matter most are mutability vs. immutability, ordered vs. unordered collections, and when to use each type. Don't just memorize what each type looks like—know what problems each one solves and how they behave differently.
These are Python's simplest data types—single values that aren't made up of other elements. Primitive types are immutable, meaning their values cannot be changed in place.
$$-3$$, $$0$$, $$42$$)$$3.14$$, $$-0.001$$, $$2.0$$)$$0.1 + 0.2$$ doesn't equal exactly $$0.3$$ in PythonTrue or False—technically a subclass of integers where True == 1 and False == 0if, while) and logical operations==, !=, >, < all return boolean valuesCompare: int vs. float—both are numeric and support arithmetic, but floats introduce precision issues while integers remain exact. If a problem involves counting discrete items, always use integers; if it involves measurement or division, expect floats.
Strings handle all text-based information in Python. Strings are immutable sequences of characters—any "modification" actually creates a new string object.
'hello'), double ("hello"), or triple ('''hello''') quotes all worktext[0:5]), concatenation (+), and methods like .upper(), .split()Compare: str vs. int—"42" and 42 look similar but behave completely differently. You can't do math on "42" without converting it first using int(). Type conversion errors between strings and numbers are among the most common bugs.
These types store multiple items in a specific sequence. The key distinction is mutability—whether you can change the contents after creation.
.append()), removed (.pop()), or changed after creation[1, "apple", 3.14, True] is perfectly validlist[0] or ranges with list[1:3](x, y), RGB colors (255, 128, 0), or function return valuesCompare: Lists vs. Tuples—both are ordered and support indexing, but lists are mutable while tuples are not. Use lists when you need to modify contents; use tuples when data should remain constant. If an exam asks about hashability or dictionary keys, tuples work but lists don't.
These types organize data by relationships rather than position. Dictionaries map keys to values; sets store unique items without duplicates.
{"name": "Alice", "age": 25} maps keys to their associated values{1, 2, 3} automatically removes duplicates and has no index positions|), intersection (&), and difference (-)x in my_set is faster than checking x in my_listCompare: Dictionaries vs. Sets—both use curly braces and require hashable elements, but dictionaries store pairs while sets store single values. Use dictionaries when you need to associate data; use sets when you only care about unique membership.
| Concept | Best Examples |
|---|---|
| Immutable primitives | int, float, bool, str |
| Mutable collections | list, dict, set |
| Immutable collections | tuple, frozenset |
| Ordered sequences | list, tuple, str |
| Unordered collections | dict (Python 3.7+ maintains insertion order), set |
| Key-value storage | dict |
| Unique value storage | set |
| Hashable (can be dict keys) | int, float, str, bool, tuple |
Which two collection types are ordered and support indexing, and what's the key difference between them?
You need to store user preferences where each setting name maps to a value. Which data type is most appropriate, and why?
Compare and contrast lists and sets: when would you choose one over the other for storing a collection of items?
Why does "5" + 3 raise an error while 5 + 3 works fine? What would you need to do to fix it?
If you need to use a collection as a dictionary key, which types would work and which would fail? Explain the underlying principle.