Data Types & Data Structures
Explore Python's built-in data types and core structures like lists, dictionaries, and sets.
Scalar Types
Python's basic building blocks are immutable scalar types.
int: Integers of arbitrary precision.float: Double-precision floating point numbers.str: Unicode strings.bool: True or False.None: Represents the absence of a value.
Lists: Ordered & Mutable
Lists are the most versatile data structure in Python.
fruits = ["apple", "banana", "cherry"]
fruits.append("date") # Add to end
first = fruits[0] # Indexing
subset = fruits[1:3] # Slicing
fruits.append("date") # Add to end
first = fruits[0] # Indexing
subset = fruits[1:3] # Slicing
Dictionaries: Key-Value Pairs
Dictionaries provide fast lookups using unique keys.
user = {
"id": 1,
"username": "pk",
"active": True
}
val = user["username"]
user["role"] = "admin"
"id": 1,
"username": "pk",
"active": True
}
val = user["username"]
user["role"] = "admin"
Tuples: Ordered & Immutable
Use tuples for fixed collections of data that shouldn't change.
point = (10, 20)
x, y = point # Unpacking
x, y = point # Unpacking
Sets: Unique & Unordered
Sets are perfect for removing duplicates and mathematical set operations.
ids = {1, 2, 2, 3} # Results in {1, 2, 3}
is_present = 1 in ids
is_present = 1 in ids
โ Practice (15 minutes)
- Create a list of 5 integers and print the middle 3 using slicing.
- Create a dictionary representing a book (title, author, year).
- Add a list of chapters to your book dictionary.
- Use a set to find the unique colors in
["red", "blue", "red", "green", "blue"].