Real Python (@realpython) header
Real Python (@realpython) profile picture

Helping Python developers build with AI: apps, agents, models, and AI-assisted coding. Expert-reviewed tutorials and courses, read by 1M+ developers a month.

Joined August 2012
1. The fix is a single line: return df.loc[df["Humidity"] >= target] It returns the same rows without a Python loop. In Spyder's Profiler, total time dropped from 3.73 s to 905.33 µs. If pandas has a built-in way to filter rows, use it before you write an iterrows() loop.
1
2
70
This pandas function loops with iterrows() to keep rows with humidity ≥ 0.90. The Spyder IDE's Profiler timed it at 3.73 s. 5 things to know: 1. The one-line fix: 905 µs 2. Why local time said 62 ms 3. Comparing runs 4. Edits your CSV never sees 5. When to skip Spyder 🧵
1
1
10
2,257
"Write a Python turtle program that draws a python reading a book." Gemini 4 Neon wrote 429 lines for it. Its python wears round reading glasses, and the open book has a tiny snake sketched on one page. Here's how it did on four more everyday Python tasks 🧵
2
4
1,792
Replying to @realpython @Tool
5. LangGraph won't register the same node name twice. A second add_node("parse_notice", ...) raises ValueError ("already present"). It bites when you extend a graph by pasting new add_node() calls under the old ones. The tutorial replaces the whole registration block each time.
1
1
482
Replying to @realpython @Tool
4. The tutorial notes that OpenAI models can't extract a date as a date type. So the Pydantic schema asks the LLM for a YYYY-mm-dd string, and a @computed_field turns it into a datetime.date. With exclude=True and repr=False, the string stays out of print() and model_dump().
1
38
1. Say your agent escalates a notice when the fine an LLM pulled from the email is $100,000 or more. If the email mentions no fine, you get None, and None >= 100_000 raises TypeError. The tutorial guards it with (max_fine or 0), so a missing fine counts as zero.
1
2
92
4. autopep8 -i vs Black on one messy function. autopep8 fixes the spaces around = and +, but leaves def add(a, b): alone, since that spacing might be there for readability. Black removes it. Ruff's formatter, built as a drop-in replacement for Black, does the same.
1
16
3. autopep8 fixes PEP 8 spacing and indentation. Run it as autopep8 messy.py, though, and your file stays as it was. It only prints the fixed code to the terminal. Add -i (or --in-place) to write the changes back to the file.
1
18
1. Your file is indented with 4 spaces. You open it in nano or Notepad, add a line and press Tab. Many simple editors insert one tab character there instead of spaces. Python sees tabs mixed with spaces in the same block and stops with a TabError, a kind of IndentationError.
1
2
96
Every model gets the same prompt: "Write a Python turtle program that draws a python reading a book." Hy4 flash wrote 276 lines and gave its snake round glasses and a book titled "Python". Here's how it did on the other everyday Python tasks 🧵
2
2
12
1,836
GPT-6.2 Sol got this prompt: "Write a Python turtle program that draws a python reading a book." 89 lines and 6 seconds later: a green snake with big eyes over an open blue book, captioned "A Python reading Python". Here's how it did on the other four everyday Python tasks 🧵
1
9
2,962
3. "Sparse is better than dense" is the one you can apply today. Pull an independent term out of a long formula and give it a name. In a sine-wave function, ω = 2 * π * f gets its own line. Each line is easier to check, and the function returns the same value.
1
33
1. Lib/this.py, the file behind import this, stores the Zen in ROT13 and decodes it with one-letter names and two nested for loops. That's the file that prints "Flat is better than nested." The standard library already does ROT13 in one call: codecs.decode(text, "rot13").
1
2
90
"Write a Python turtle program that draws a python reading a book." GPT-6.2 Astra wrote 97 lines and 1,552 drawing commands. Its snake wears round reading glasses and holds a red book with "Python" on the page. Here's how it did on four more everyday Python tasks 🧵
1
9
1,543