Get a daily drop of Python knowledge 🐍💧 -> https://mathspp.com/drops
Pydon'ts – free Python book 👉 https://mathspp.com/books/pydonts
I just have fun writing code like this for myself 🤪
👇 yay or nay?
I just have fun writing code like this for myself 🤪
👇 yay or nay?
When I do, I create it by inheriting from `list`.
This gives me almost everything I want for free!
When I do, I create it by inheriting from `list`.
This gives me almost everything I want for free!
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.
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.
I mean, after 200 ⭐️⭐️⭐️⭐️⭐️ reviews, this anonymous user could think for a second and realise that the issue wasn't mine/the book's...
🤡
I mean, after 200 ⭐️⭐️⭐️⭐️⭐️ reviews, this anonymous user could think for a second and realise that the issue wasn't mine/the book's...
🤡
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:
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:
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.
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.
The function `oxford_comma`, shown below, demonstrates this technique.
What's the point of the smaller fragments?
The function `oxford_comma`, shown below, demonstrates this technique.
What's the point of the smaller fragments?
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.
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.
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
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
The returned list with the splits (and possibly the remainder of the string) has a maximum length of `maxsplit + 1`.
The returned list with the splits (and possibly the remainder of the string) has a maximum length of `maxsplit + 1`.
But I'll do it either way, it's the logical follow-up to my uv cheat sheet.
But I'll do it either way, it's the logical follow-up to my uv cheat sheet.
Very informative git history.
Very informative git history.
But at least I'll sleep well tonight, knowing all “See also” call outs are correctly formatted.
But at least I'll sleep well tonight, knowing all “See also” call outs are correctly formatted.
You can get the cheatsheet from here: mathspp.com/blog/uv-chea...
You can get the cheatsheet from here: mathspp.com/blog/uv-chea...
(I'm turning my “uv cheatsheet” blog post into an actual cheatsheet you can download.)
(I'm turning my “uv cheatsheet” blog post into an actual cheatsheet you can download.)
This doesn't split the string then count, so it'll work for ridiculously large strings (e.g. files you can't load into memory)
Plus, I had fun making it branchless (i.e., without explicit `if`s) by doing arithmetic with the flag `in_word` 😅
This doesn't split the string then count, so it'll work for ridiculously large strings (e.g. files you can't load into memory)
Plus, I had fun making it branchless (i.e., without explicit `if`s) by doing arithmetic with the flag `in_word` 😅
You'll need lots of shiny badges like the one you can see below for my most recent project 👇
You can create custom badges at shields.io, it's a pretty cool service!
Very professional, much wow!
You'll need lots of shiny badges like the one you can see below for my most recent project 👇
You can create custom badges at shields.io, it's a pretty cool service!
Very professional, much wow!
As my reviewer noted, my poor (or genius?) choice of directory name makes it look like my terminal is barking all the time 🤣
What do you say?
Poor choice for a directory name or genius move?
As my reviewer noted, my poor (or genius?) choice of directory name makes it look like my terminal is barking all the time 🤣
What do you say?
Poor choice for a directory name or genius move?
However, I thought I'd be able to express this easily with recursion but nothing obvious comes to mind...
Any suggestions?
However, I thought I'd be able to express this easily with recursion but nothing obvious comes to mind...
Any suggestions?
But I only discovered today that you can specify a default value to associate with all keys!
I thought you always had to initialise the dict with all keys mapping to `None`!
But I only discovered today that you can specify a default value to associate with all keys!
I thought you always had to initialise the dict with all keys mapping to `None`!
EuroPython '25 took place in Prague 🇨🇿 this year and this was the fourth EuroPython I attended.
It was simultaneously the one I enjoyed the most and the one that was the most tiring. 😰
This was also the most rewarding conference for me so far.
EuroPython '25 took place in Prague 🇨🇿 this year and this was the fourth EuroPython I attended.
It was simultaneously the one I enjoyed the most and the one that was the most tiring. 😰
This was also the most rewarding conference for me so far.