CODING180
Chapter 1 of 614m

Data Structures and Files in Python Coding

Data Structures and Files in Python Coding

In this chapter, we’ll shape data with lists, tuples, sets, and dictionaries. Then we’ll read and write files so your programs can work with real text. As with earlier chapters of the python coding language series, we’ll keep things friendly and practical.

Before you dive in, make sure your environment is ready.

If you need a refresher on installing tools, jump back to Getting Started with the Python Coding Language.

We’ll also lean on earlier ideas from Variables and Data Types in the Python Coding Language, Control Flow Fundamentals for Python Coding, and Functions and Modules for Python Coding Beginners.

What you’ll learn now:
- Core collection types: lists, tuples, sets, dictionaries
- Indexing, slicing, and common list methods (append, pop)
- Basic list and dictionary comprehensions
- Reading and writing text files with with open(...) as f
- A quick tour of pathlib for file paths, plus OS path tips

Core Data Structures in the python coding language

Lists: ordered, mutable collections

Use a list when order matters and you want to change items later.

nums = [10, 20, 30, 40]

first = nums[0]        # 10
last = nums[-1]        # 40 (negative index from the end)

subset = nums[1:3]     # [20, 30]
step_slice = nums[::2] # [10, 30]

nums.append(50)        # [10, 20, 30, 40, 50]
val = nums.pop()       # pops from end -> val=50; nums now [10,20,30,40]
nums.pop(1)            # remove item at index 1 -> [10, 30, 40]

del nums[0]            # [30, 40]

Tips:
- Lists make great stacks with append/pop from the end.
- For queue behavior (pop from the front), lists are slow, use collections.deque instead.

Tuples: ordered, immutable pairs/groups

Tuples are like lists you can’t change. They’re great for fixed records.

point = (3, 4)
user = ("alice", 42)

name, age = user       # name='alice', age=42

single = ("only_one",)

Immutability helps prevent accidental changes. It makes tuples safe as dictionary keys when they contain only immutable items.

Sets: unique, unordered values

Use sets for fast membership tests and to remove duplicates.

raw = ["a", "b", "a", "c"]
unique = set(raw)          # {'a', 'b', 'c'}

A = {1, 2, 3}
B = {3, 4}
print(A | B)               # union -> {1,2,3,4}
print(A & B)               # intersection -> {3}
print(A - B)               # difference -> {1,2}
print(A ^ B)               # symmetric difference -> {1,2,4}

Remember: {} creates an empty dict, not a set. Use set() for an empty set.

Dictionaries: key/value lookups

Dictionaries map keys to values, perfect for settings, user records, or quick lookups.

person = {
    "name": "Maya",
    "city": "Oslo",
    "age": 29,
}

print(person["name"])        # 'Maya'
person["age"] = 30           # update value

nickname = person.get("nickname", "(none)")

for k, v in person.items():
    print(k, v)

Keys must be immutable (e.g., strings, numbers, or tuples of immutables). Assigning an existing key overwrites the old value.

Comprehensions: concise build-and-filter

Comprehensions let you create collections in one readable line.

They pair nicely with what you learned in Control Flow Fundamentals for Python Coding.

They also fit the functions you wrote in Functions and Modules for Python Coding Beginners.


nums = [1, 2, 3, 4, 5]
squares_of_even = [n*n for n in nums if n % 2 == 0]  # [4, 16]

names = ["ana", "ben", "chris"]
name_lengths = {name: len(name) for name in names}    # {'ana':3, 'ben':3, 'chris':5}

Use comprehensions for simple build/filter tasks. If the logic gets complex, write a regular loop for clarity.

File I/O: read and write text safely

Use with open(...) as f to manage files. Python automatically closes the file, even if an error occurs.


lines = ["First line", "Second line"]
with open("notes.txt", mode="w", encoding="utf-8") as f:
    for line in lines:
        f.write(line + "\n")

with open("notes.txt", encoding="utf-8") as f:
    content = f.read()             # one big string

with open("notes.txt", encoding="utf-8") as f:
    for line in f:
        print(line.strip())

Pathlib: cleaner paths across OSes

Pathlib makes paths safer and more readable than manual strings.

from pathlib import Path

base = Path.cwd()                 # current working directory
text_dir = base / "data"
text_dir.mkdir(exist_ok=True)     # create folder if missing

file_path = text_dir / "words.txt"
file_path.write_text("hello world\nhello python", encoding="utf-8")

content = file_path.read_text(encoding="utf-8")
print(content)

Platform tips:
- Windows paths use backslashes (e.g., C:\files\notes.txt). Backslashes start escapes (\n, \t), so either double them (\\) or use raw strings r"C:\files\notes.txt".
- macOS/Linux use forward slashes (/home/user/notes.txt). Paths are often case-sensitive.
- Prefer pathlib when possible: Path("C:/files/notes.txt") works on Windows and macOS/Linux.

If you want to try the code right away in your browser, use a top Python online IDE.

It lets you run the examples and exercises.

Practical Exercises

Let’s cement the ideas with two small, useful tasks.

Exercise 1: Word counter (from a text file)

Goal: Read a text file and report the top N most frequent words.

Steps:
1. Ask for a file path (use input()).
2. Read the file’s text with with open(...) as f and f.read().
3. Normalize: lowercase, remove punctuation, and split on whitespace.
4. Use a dictionary to count occurrences.
5. Sort by count and print the top 10.

Starter code:

from pathlib import Path

path_str = input("Enter path to a .txt file: ")
path = Path(path_str)
text = path.read_text(encoding="utf-8")

for ch in ",.;:!?\"'()[]{}":
    text = text.replace(ch, " ")
words = [w for w in text.lower().split() if w]

counts = {}
for w in words:
    counts[w] = counts.get(w, 0) + 1

top = sorted(counts.items(), key=lambda kv: kv[1], reverse=True)[:10]
for word, freq in top:
    print(f"{word}: {freq}")

Tips:
- Large files? Consider reading line by line instead of all at once.
- You can skip small stopwords like "the" or "and" with a set and a comprehension.

Exercise 2: Simple contacts lookup (dictionary)

Goal: Store name → phone/email and look them up by name.

Steps:
1. Start with a dictionary of contacts.
2. Use a loop to prompt for a name; print contact info if found.
3. Allow adding a new contact if it’s missing.

Starter code:

contacts = {
    "maya": {"phone": "555-1010", "email": "maya@example.com"},
    "ben":  {"phone": "555-2020", "email": "ben@example.com"},
}

while True:
    name = input("Lookup name (or 'quit'): ").strip().lower()
    if name == "quit":
        break
    info = contacts.get(name)
    if info:
        print(f"Phone: {info['phone']} | Email: {info['email']}")
    else:
        print("Not found. Add it.")
        phone = input("Phone: ")
        email = input("Email: ")
        contacts[name] = {"phone": phone, "email": email}
        print("Saved!")

For more practice that reinforces these data structures and file handling concepts, check out these Python exercises for beginners.

Troubleshooting Corner

  • FileNotFoundError: Confirm the path and current working directory (print(Path.cwd())). Try absolute paths.
  • UnicodeDecodeError: Specify encoding="utf-8" when opening files.
  • KeyError on dict access: Use .get(key, default) or check key in dict first.
  • Mutating lists during iteration: Build a new list with a comprehension or iterate over a copy (for x in items[:]).

Summary and What’s Next

You learned how to structure and persist data, skills you’ll use in nearly every project. Key ideas:
- Lists: ordered, mutable; index, slice, append, pop
- Tuples: immutable records; easy unpacking
- Sets: unique values; fast membership and set ops
- Dicts: key/value lookups; .get() to avoid KeyError
- Comprehensions: compact build/filter for lists and dicts
- Files: with open(...) as f for safe reads/writes; pathlib for robust paths

Next up, you’ll bring it all together.

Build something small but real in Build a Mini Project in the Python Coding Language.

You’re well on your way in the python coding language journey, keep going!

Research & References: Want a printable cheat sheet and sample files for this chapter? Complete the quick form below and click the button to gain instant access.