upgrade
upgrade

⌨️AP Computer Science Principles

Data Types in Python

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 how computers store, process, and manipulate information—and that's exactly what the AP CSP exam tests. When you write a program, every piece of data has a type that determines what operations you can perform on it, how much memory it uses, and how the computer interprets those bits under the hood. Understanding data types connects directly to abstraction, data representation, and algorithm design—core concepts that appear throughout Units 2 and 3.

You're being tested on more than just definitions. The exam wants you to recognize why you'd choose one data type over another, how different types behave in expressions and comparisons, and what happens when you try to combine incompatible types. Don't just memorize that a list is "ordered"—know that this ordering enables iteration, indexing, and sequential processing. Each data type illustrates a fundamental programming concept, so learn the why alongside the what.


Primitive Data Types: The Building Blocks

Primitive types represent single values and form the foundation of all data in Python. These are the simplest abstractions the language provides—each maps directly to how computers store basic information in binary.

Integer (int)

  • Whole numbers without decimal points—positive, negative, or zero (e.g., 42, -7, 0)
  • Supports arithmetic operations like addition, subtraction, multiplication, and integer division (//)
  • Essential for counting and indexing—loops, list positions, and conditional comparisons all rely on integers

Float

  • Numbers with decimal points for representing fractional values (e.g., 3.14, -0.001)
  • Precision limitations existfloating-point arithmetic can introduce small rounding errors due to binary representation
  • Critical for scientific and financial calculations where whole numbers aren't sufficient

Boolean (bool)

  • Only two possible values: True or False—the simplest data type, representing a single bit of information
  • Controls program flow—every if statement and while loop depends on Boolean evaluation
  • Derived from comparisons and logical operators—expressions like x > 5 or a and b produce Boolean results

Compare: Integer vs. Float—both represent numbers and support arithmetic, but floats handle fractions while integers are exact. On FRQs about precision or counting iterations, integers are your safer choice.


Text Data: Representing Human-Readable Information

Strings allow programs to work with text—a sequence of characters that the computer treats as a single unit of data, enabling communication between humans and machines.

String (str)

  • A sequence of characters enclosed in single ('hello') or double ("hello") quotes
  • Immutable once created—operations like concatenation produce new strings rather than modifying the original
  • Supports slicing and indexing—access individual characters or substrings using bracket notation (e.g., text[0:5])

Compare: String vs. Integer—both can represent "42," but "42" (string) cannot be used in arithmetic without conversion, while 42 (integer) cannot be concatenated with text. Type mismatches are a common source of program errors.


Collection Types: Storing Multiple Values

Collections hold multiple items in a single variable. The key distinction is whether the collection is ordered, mutable, and how it organizes access to its elements.

List

  • Ordered, mutable collection—items maintain their position and can be changed after creation
  • Heterogeneous storage allowed—a single list can contain integers, strings, other lists, or mixed types
  • Rich manipulation methods.append(), .remove(), .sort(), and slicing make lists highly flexible for algorithms

Tuple

  • Ordered but immutable—once created, elements cannot be added, removed, or changed
  • Guarantees data integrity—use tuples when values should remain constant throughout program execution
  • Can serve as dictionary keysimmutability makes tuples hashable, unlike lists

Compare: List vs. Tuple—both are ordered sequences supporting indexing and iteration, but lists allow modification while tuples do not. Choose lists for data that changes; choose tuples for fixed records like coordinates (x, y).

Dictionary (dict)

  • Key-value pairs for fast lookup—retrieve any value instantly using its unique key
  • Keys must be immutable (strings, numbers, tuples)—values can be any data type, including other dictionaries
  • Ideal for structured data—modeling real-world entities like student records or configuration settings

Set

  • Unordered collection of unique elements—duplicates are automatically removed
  • Optimized for membership testing—checking if an item exists is extremely fast
  • Supports mathematical operations—union (|), intersection (&), and difference (-) mirror set theory

Compare: Dictionary vs. Set—both use curly braces {} and require immutable elements (keys for dicts, items for sets), but dictionaries map keys to values while sets only store unique items. If an FRQ asks about removing duplicates, sets are your answer.


Mutability: A Critical Concept

Understanding whether a type is mutable determines how your program behaves when data is shared or modified. Mutable objects can be changed in place; immutable objects require creating new copies.

Mutable TypesImmutable Types
ListInteger
DictionaryFloat
SetString
Boolean
Tuple

Compare: List vs. String—both support indexing and slicing, but my_list[0] = "new" works while my_string[0] = "n" raises an error. This distinction frequently appears in debugging scenarios on the exam.


Quick Reference Table

ConceptBest Examples
Numeric representationInteger, Float
Logical decision-makingBoolean
Text and character dataString
Ordered, changeable collectionsList
Ordered, fixed collectionsTuple
Key-value mappingDictionary
Unique elements / duplicates removalSet
Immutable types (safe for dict keys)Integer, Float, String, Boolean, Tuple

Self-Check Questions

  1. Which two data types are both ordered collections, and what key property distinguishes them from each other?

  2. A program needs to store student IDs mapped to their names for quick lookup. Which data type is most appropriate, and why wouldn't a list work as well?

  3. Compare and contrast how strings and lists handle modification—what happens when you try to change a single character or element in each?

  4. If you need to remove duplicate values from a collection while preserving the ability to check membership quickly, which data type should you use?

  5. An FRQ asks you to explain why "5" + 3 causes an error while 5 + 3 does not. What concept about data types does this illustrate, and how would you fix the first expression?