Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 24/04/2026Your frontier of competence is expanded by LLMs based on your current knowledge. 210
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 12/03/2026Python decorators blueprint: a step by step explanation of the full decorator anatomy. Steal this for when you're writing your own decorators. 172
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 19/02/2026`itertools.compress` explained in a simple diagram. Useful when you need to filter an iterable of data based on a second iterable of flags or selectors. 010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 03/02/2026If they had used the built-in `range` this wouldn’t happen. 250
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 02/02/2026It's pretty cool how much you can get done in Python with so little code if you leverage the standard library. 010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 30/01/2026One of my students today made this mistake. If you have a project and want to test it, your tests will import your project. So, you need to install pytest as a dependency INSIDE your project. If you install it globally, when you run it, it can't see your own project. 200
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 29/01/2026Parse JSON from a string with a single function call. Don't use this if you have JSON in a file. Use `load` for that. This is for when the data comes in a payload, for example. 000
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 28/01/2026When writing CLIs and scripts for the terminal, use `sys.exit` to set the exit code of your program. ✅ An exit code of 0 means success; your program terminated without any issues. ❌ Any other integer exit code means “error”. 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 27/01/2026Before being added in Python 3.10, itertools.pairwise was commonly implemented in terms of itertools.tee. Isn't this elegant? 121
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 26/01/2026`typing.reveal_type` explained: Runtime vs static type checking time. 000
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 25/01/2026Python typing pop quiz. Take a look at the function `choice` shown below. Note how it's being called as `choice(3, "hey!")`. Will this type check or not? If it does, what's the revealed type? The right answer might surprise you! It surprised me. 🤷 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 23/01/2026Python has a flexible mechanism built into `pathlib` to look for files with filenames that follow a given pattern. You can use `[...-...]` to represent character ranges and `*` to match arbitrary text. But note that `glob` does NOT support regex syntax! This is just similar. 121
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 21/01/2026These two typing features look similar... But they're very different. Here's how to use NewType and TypeAlias in Python 👇 111
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 21/01/2026Just finished scheduling 3 tips for today, tomorrow and Friday. If you want to get smarter about Python in just 3 min/day, follow the QR code to sign-up. 010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 20/01/2026Convert data to a JSON string without writing to a file: Just use `json.dumps`! (The final S stands for String!) ```py import json data = {"k1": True, "k2": [73, 42, 10]} s = json.dumps(data) print(type(s), s) # <class 'str'> {"k1": true, "k2": [73, 42, 10]} ``` 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 19/01/2026Let me teach you to break a Python law. Iterators can only be traversed once: ```py squares = (x ** 2 for x in range(3)) for sq in squares: print(sq, end=", ") # 0, 1, 4, for sq in squares: print(sq, end=", ") # <no output> ``` The second loop produces no output! 140
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 18/01/2026`itertools.pairwise` takes an iterable and produces overlapping pairs of consecutive elements: ```py from itertools import pairwise queue = ["John", "Joe", "Ana"] for f, b in pairwise(queue): print(f"{f}'s ahead of {b}.") # John's ahead of Joe. # Joe's ahead of Ana. ``` 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 17/01/2026This uv cheatsheet lists 40+ of the most and useful uv commands. It groups the commands into 9 sections that correspond to major features/capabilities that uv has. The cheatsheet is free to download and comes in light mode, dark mode, and high-contrast mode. mathspp.gumroad.com/l/cheatsheet... 043
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 16/01/2026A regex match has the method `groups`. This method returns a tuple with all of the groups that the pattern contained. Here's an example 👇 200
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 14/01/2026When a method returns the argument `self`, use `typing.Self` to annotate the return type of the method: ```py from typing import Self class P: def some_method(self) -> Self: ... return self ``` Why is this useful? 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 13/01/2026Whenever you are using validator functions you run the risk of forgetting to call the validator. This means you'll be passing around data that might be invalid. You can prevent this from happening by writing a “parser” function instead of a validator. But what do I mean? 100
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 12/01/2026You can use the module `json` to read and write data in the JSON format, which is very suitable to represent the most common Python built-in types: - lists - dictionaries - strings - integers - floats - Booleans. 200
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 11/12/2025It's so ANNOYING to break out of nested loops. Auxiliary variables, conditional statements, non-linear paths through your code... It's a mess! But if you extract the looping logic to a generator, you get: - less indentation ✨ - a flat loop you can easily break out of ✨ 1172
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 10/12/2025📢 Public service announcement: Don't buy from me: Don't buy my books. Don't sign-up for my courses. Don't attend my cohorts. Apparently, I'm a scammer. 🤷 At least, that's my interpretation of this comment from a subscriber... Should I delete them from my mailing list? 230
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 08/12/2025A visualisation of day 7 from Advent of Code. Worked on this during yesterday's stream, although the colouring was added off stream. 041
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 07/12/20257 days of Advent of Code diagrams from my streams and analysis sessions! 030
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 04/12/2025Anatomy of a list comprehension. Understand this and you'll never need to look up list comprehensions again. 031
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 02/12/2025Here's a lie developers tell themselves: “Recursion is useless” That's a coping mechanism because you're afraid of recursion... But you shouldn't be afraid! Recursion is actually nice ✨ And it's a very natural way to express certain algorithms... Let me show you an example. 100
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 26/11/2025In today's session of the Algorithm Mastery Bootcamp we talked about linear recurrence relations and how to represent them as matrices. The next step? Using fast exponentiation with repeated squaring to compute terms at LIGHTNING FAST speeds ⚡️🤓 030
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 20/11/2025Here's how to NOT get a file name from a path in Python. The string method `split` has a counterpart `rsplit` that starts splitting from the end of the string. This is useful if you only want the final segment(s) of a string. 120
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 19/11/2025Do you work in Hollywood or at a publisher? If you do, I've got the perfect Python string method for you: `str.title` Changes the case of any string into title case. “star wars: the empire strikes back” becomes “Star Wars: The Empire Strikes Back”. Much more professional! 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 17/11/2025Need a temp file in Python? Maybe you need to test a function that requires a file-like object. Or maybe you need a buffer because you're processing an amount of data so large that it doesn't fit into memory... In these situations, `tempfile.TemporaryFile` can help you out. 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 14/11/2025When I'm feeling fancy, I define some methods in a more “functional” way. I just have fun writing code like this for myself 🤪 👇 yay or nay? 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 14/11/2025When doing Advent of Code I always end up needing a stack. When I do, I create it by inheriting from `list`. This gives me almost everything I want for free! 241
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 13/11/2025You know what's a pain to implement in Python? A moving average function. Manually keeping track of all the values being considered for the window is a pain! Unless you use `collections.deque`. Its param `maxlen` makes the window automatically evict the values you don't need. 2583
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 12/11/2025I was going through the OVERWHELMINGLY POSITIVE reviews of my book “Pydon'ts” and I always giggle when I find this 1-star review. I mean, after 200 ⭐️⭐️⭐️⭐️⭐️ reviews, this anonymous user could think for a second and realise that the issue wasn't mine/the book's... 🤡 010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 11/11/2025Your future you will thank you for this: The keyword `assert` takes an expression to its right. If the expression evaluates to `True`, or Truthy, everything is fine. But if it doesn’t, you get an `AssertionError`. That's a pretty unhelpful/generic error... Here's the fix: 120
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 10/11/2025All Python objects have a Truthy/Falsy value. This is the value of an object when used in a Boolean context. For example, when used in the condition of an `if` statement. Most objects are Truthy, with a few exceptions. For most types, the “nothing” or “empty” value is Falsy. 120
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 07/11/2025When building complex readable strings, consider building them out of smaller fragments. The function `oxford_comma`, shown below, demonstrates this technique. What's the point of the smaller fragments? 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 06/11/2025Python has a very well-known naming convention: Names that start with a leading underscore are “private”. This means the outside world has no business using them. E.g., attributes and methods starting with `_` in a class mean they're for that class only. 010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 05/11/2025The methods `strftime` and `strptime` can be used to convert dates/times into strings and vice-versa. Here's a mnemonic to help you: 👉 `strptime` has a “P” for “Parse date/time”, so string -> date 👉 `strftime` has an “F” for “Format date/time”, so date -> string 010
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 04/11/2025The string method `str.split` has a parameter `maxsplit` that determines the maximum number of splits that will be performed. The returned list with the splits (and possibly the remainder of the string) has a maximum length of `maxsplit + 1`. 041
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 03/11/2025Hah, you beat me to it. But I'll do it either way, it's the logical follow-up to my uv cheat sheet. 150
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 31/10/2025This is your friendly reminder that str.split accepts a parameter to determine the maximum number of splits. You get up to n splits and the remainder of the string. 110
Rodrigo Girão Serrão 🐍🚀 @mathspp.com · 28/10/2025Live-coding during trainings + automatically syncing to git so everyone has access to what I'm typing = Very informative git history. 000