Skip to content
Python Programming

Python Data Types

Beginner Lesson 3 of 10

In this lesson, we’ll explore Python’s collection data types that help you organize and store multiple values.

Lists are ordered, mutable collections that can hold items of different types.

# Creating lists
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]
empty_list = []
# Accessing elements (0-indexed)
print(fruits[0]) # "apple"
print(fruits[-1]) # "cherry" (last item)
print(fruits[1:3]) # ["banana", "cherry"] (slicing)
# Modifying lists
fruits[0] = "mango" # Change element
fruits.append("orange") # Add to end
fruits.insert(1, "grape") # Insert at position
fruits.remove("banana") # Remove by value
popped = fruits.pop() # Remove and return last
del fruits[0] # Delete by index
# List operations
numbers = [3, 1, 4, 1, 5]
numbers.sort() # Sort in place: [1, 1, 3, 4, 5]
numbers.reverse() # Reverse in place: [5, 4, 3, 1, 1]
print(len(numbers)) # Length: 5
print(sum(numbers)) # Sum: 14
print(min(numbers)) # Minimum: 1
print(max(numbers)) # Maximum: 5
# List comprehension
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8]

Tuples are ordered, immutable collections - once created, they cannot be modified.

# Creating tuples
coordinates = (10, 20)
rgb = (255, 128, 0)
single = (42,) # Note the comma for single-element tuple
# Accessing elements
print(coordinates[0]) # 10
x, y = coordinates # Tuple unpacking
# Tuples are immutable
# coordinates[0] = 5 # Error! Cannot modify
# When to use tuples
# - For data that shouldn't change (coordinates, RGB values)
# - As dictionary keys
# - For function return values
def get_user():
return ("Alice", 25, "alice@email.com")
name, age, email = get_user()

Dictionaries store key-value pairs for fast lookups.

# Creating dictionaries
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# Accessing values
print(person["name"]) # "Alice"
print(person.get("age")) # 25
print(person.get("country", "Unknown")) # "Unknown" (default)
# Modifying dictionaries
person["email"] = "alice@email.com" # Add new key
person["age"] = 26 # Update existing
del person["city"] # Delete key
person.pop("email") # Remove and return
# Dictionary methods
print(person.keys()) # dict_keys(['name', 'age'])
print(person.values()) # dict_values(['Alice', 26])
print(person.items()) # dict_items([('name', 'Alice'), ('age', 26)])
# Iterating over dictionaries
for key in person:
print(f"{key}: {person[key]}")
for key, value in person.items():
print(f"{key}: {value}")
# Dictionary comprehension
squares = {x: x**2 for x in range(5)}
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Sets are unordered collections of unique elements.

# Creating sets
fruits = {"apple", "banana", "cherry"}
numbers = {1, 2, 3, 3, 3} # Duplicates removed: {1, 2, 3}
empty_set = set() # Note: {} creates empty dict, not set
# Set operations
fruits.add("orange")
fruits.remove("banana") # Raises error if not found
fruits.discard("mango") # No error if not found
# Set mathematics
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a | b) # Union: {1, 2, 3, 4, 5, 6}
print(a & b) # Intersection: {3, 4}
print(a - b) # Difference: {1, 2}
print(a ^ b) # Symmetric difference: {1, 2, 5, 6}
# Membership testing (very fast)
print(3 in a) # True
FeatureListTupleDictionarySet
Ordered✅✅✅*❌
Mutable✅❌✅✅
Duplicates✅✅Keys: ❌❌
Indexing✅✅By key❌
Syntax[](){}{}

*Dictionaries maintain insertion order since Python 3.7

Remove duplicates from a list while preserving order.

numbers = [1, 3, 2, 3, 1, 4, 2, 5]
# Expected: [1, 3, 2, 4, 5]

Count the frequency of each word in a sentence.

sentence = "the quick brown fox jumps over the lazy dog"
# Expected: {"the": 2, "quick": 1, ...}

Continue to Control Flow →