upgrade
upgrade

🐍Intro to Python Programming

Python 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 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.


Primitive Types: The Building Blocks

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.

Integers (int)

  • Whole numbers without decimals—positive, negative, or zero (e.g., $$-3$$, $$0$$, $$42$$)
  • Unlimited precision in Python means integers can be arbitrarily large, unlike many other languages
  • Used for counting and indexing—list indices, loop counters, and discrete quantities all require integers

Floating-Point Numbers (float)

  • Numbers with decimal points for representing real numbers (e.g., $$3.14$$, $$-0.001$$, $$2.0$$)
  • Binary representation causes rounding errors$$0.1 + 0.2$$ doesn't equal exactly $$0.3$$ in Python
  • Essential for scientific and financial calculations—any scenario requiring fractional precision

Booleans (bool)

  • Only two possible values: True or Falsetechnically a subclass of integers where True == 1 and False == 0
  • Control program flow through conditional statements (if, while) and logical operations
  • Result from comparisons—operators like ==, !=, >, < all return boolean values

Compare: 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.


Text Data: Strings

Strings handle all text-based information in Python. Strings are immutable sequences of characters—any "modification" actually creates a new string object.

Strings (str)

  • Sequences of characters in quotes—single ('hello'), double ("hello"), or triple ('''hello''') quotes all work
  • Immutable but highly flexible—supports slicing (text[0:5]), concatenation (+), and methods like .upper(), .split()
  • Essential for user interaction—input/output, file handling, and data formatting all rely on string manipulation

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.


Ordered Collections: Lists and Tuples

These types store multiple items in a specific sequence. The key distinction is mutability—whether you can change the contents after creation.

Lists

  • Ordered, mutable collections—items can be added (.append()), removed (.pop()), or changed after creation
  • Can hold mixed data types[1, "apple", 3.14, True] is perfectly valid
  • Support indexing and slicing—access elements with list[0] or ranges with list[1:3]

Tuples

  • Ordered but immutable collections—once created, contents cannot be changed
  • Faster and safer than lists—immutability means they can be used as dictionary keys and are slightly more memory-efficient
  • Ideal for fixed data groupings—coordinates (x, y), RGB colors (255, 128, 0), or function return values

Compare: 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.


Key-Value and Unique Collections: Dictionaries and Sets

These types organize data by relationships rather than position. Dictionaries map keys to values; sets store unique items without duplicates.

Dictionaries (dict)

  • Key-value pairs enclosed in braces—{"name": "Alice", "age": 25} maps keys to their associated values
  • Keys must be unique and immutable—strings, numbers, and tuples work as keys; lists do not
  • Constant-time lookups—retrieving a value by key is extremely fast regardless of dictionary size

Sets

  • Unordered collections of unique items{1, 2, 3} automatically removes duplicates and has no index positions
  • Support mathematical set operations—union (|), intersection (&), and difference (-)
  • Optimized for membership testing—checking x in my_set is faster than checking x in my_list

Compare: 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.


Quick Reference Table

ConceptBest Examples
Immutable primitivesint, float, bool, str
Mutable collectionslist, dict, set
Immutable collectionstuple, frozenset
Ordered sequenceslist, tuple, str
Unordered collectionsdict (Python 3.7+ maintains insertion order), set
Key-value storagedict
Unique value storageset
Hashable (can be dict keys)int, float, str, bool, tuple

Self-Check Questions

  1. Which two collection types are ordered and support indexing, and what's the key difference between them?

  2. You need to store user preferences where each setting name maps to a value. Which data type is most appropriate, and why?

  3. Compare and contrast lists and sets: when would you choose one over the other for storing a collection of items?

  4. Why does "5" + 3 raise an error while 5 + 3 works fine? What would you need to do to fix it?

  5. If you need to use a collection as a dictionary key, which types would work and which would fail? Explain the underlying principle.