If you want to learn more about the `match` statement":
I have a great article explaining when the `match` statement ISN'T the right tool for the job: π mathspp.com/blog/pydonts/sβ¦
That's it for now!
Do you want to level up your Python π game?
Want to get awesome Python π knowledge and content daily?
Then follow @mathsppblog because that is exactly what I do here on Twitter.
Also, if you found value in this thread, show your love π and retweet it π
Here's a TL;DR
1. `match` declares what object we are looking at; 2. `case` defines the cases we want to handle; 3. `|` separates multiple options; 4. use `if`s after `case`s to refine conditions; 5. use `_` to match any object at all (kind of like an `else`);
6. object destructuring works on custom classes; 7. object destructuring gives access to objects' attributes; 8. __match_args__ allows to remove boilerplate from `case`s; 9. object destructuring on built-ins works like basic type validation; 10. sub-patterns can be named;
11. use `*` with iters to match arbitrary sections; 12. `case` with dict don't check full structure; 13. `case` with dict can check just key or key/value pairs. 14. use `**` to match arbitrary portion of dict; and 15. use `**` with `if` to enforce specific dict structure.
Bye π
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
Do you want to master Python π list comprehensions?
Do you want to become more proficient working with lists in Python π?
If you answered with βyesβ, then here's a cheatsheet of mine to help you out.
This is also a thread π𧡠that breaks it down and explains everything:
First off, here is the anatomy of a list comprehension.
There's 4 parts to a list comp:
1. enclosing brackets to create the list [];
2. expression that transforms your data;
3. `for` iteration over the initial data;
4. (optional!) condition to filter some data.
Do list comprehensions look confusing?
Maybe.
But that's because you didn't realise that list comprehensions have equivalent `for` loops!
The coloured boxes below show the equivalent bits in the list comp and in the `for` loop: