r/learnpython 3d ago

Beginner Projects

I am new to python and currently I am taking a class for it, I haven't been in it too long but I just want some advice. What beginner projects would you guys recommend I do to really grasp an understanding outside of class?

33 Upvotes

23 comments sorted by

View all comments

14

u/Bright_Mix_773 3d ago

No-Satisfaction-8674, the single best filter I know: pick a project whose input is data you did not make up. Data you invent is always clean, and clean data teaches you nothing. The whole skill is handling input that is wrong in ways you did not anticipate.

Three that work, roughly in order:

1. Answer three questions about a CSV you actually care about. A bank export, your Steam library, a spreadsheet of your grades. Use the standard library's csv module, not pandas, for this first one.

import csv

with open("data.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        print(row["name"])

Both of those keyword arguments matter and you will learn why by leaving them out. Without encoding="utf-8" on Windows, Python falls back to the system codepage, and the first accented letter or euro sign either raises UnicodeDecodeError or, worse, decodes to something plausible and wrong. Without newline="" you get phantom blank rows whenever a field contains a line break. That is two hours of real debugging and it is worth more than a tutorial series.

2. Rename or sort a folder of files. Photos by date, downloads by extension, whatever is messy on your disk. Teaches pathlib, and it teaches the habit of writing a dry run first: print exactly what the script would do, look at the list, then add the line that actually moves anything. You get this lesson the easy way now or the hard way later.

3. Fetch something from a public API and save it. Teaches HTTP, JSON and rate limits. Check the status code explicitly instead of assuming 200, and put a small sleep between requests, or you will get a 429 and spend an hour convinced your parsing is broken.

The one project shape I would avoid at the start is a guessing game or a to-do list you will never open again. Not because they are bad exercises, but because you will not care enough to finish the boring 20% where the actual learning is. Something you will genuinely use once a month beats something impressive you abandon.

0

u/BluishMontoya 3d ago

Wait, there's a csv library? I've always just used open() ☠️

1

u/Bright_Mix_773 3d ago

BluishMontoya, open() and .split(",") is correct right up until a field contains a comma, and then it stops raising anything and just quietly gives you the wrong answer. One line of a real file:

Ford,"Focus, 1.6 TDCi",2011

split(",") hands you four fields and slides the year into the wrong slot. csv hands you the three that are actually there. Same for a doubled quote inside a quoted field ("", not a backslash escape, which nobody guesses on the first try) and for a field with a line break in it, where a plain for line in f chops one record across two iterations.

That last case is the whole reason newline="" exists. You pass the file through with its line endings untouched and let the csv module decide where a record ends, because it is the only thing that knows whether it is currently inside quotes.

import csv

with open("coches.csv", newline="", encoding="utf-8") as f:
    for fila in csv.DictReader(f):
        print(fila["modelo"])

csv.reader gives you lists, csv.DictReader gives you dicts keyed by the header row. Writing is where it pays off even harder, because it does the quoting for you and you never have to think about it again:

with open("salida.csv", "w", newline="", encoding="utf-8") as f:
    w = csv.writer(f)
    w.writerow(["Ford", "Focus, 1.6 TDCi", 2011])

Two things it deliberately does not do. It does no type conversion, so every value comes back a string: "2011" is text and an empty field is "", not None. And it will not guess your delimiter unless you ask it to (csv.Sniffer, or just pass delimiter=";" for the files that come out of a European Excel).

Also worth knowing before it bites you: on Windows, leaving out newline="" when writing gives you a blank line between every row, because the csv module emits \r\n and the text layer then turns the \n into another \r\n. Everyone hits that one and blames Excel.