@teachmepython.bsky.social
240 followers 130 following 400 posts
Posts Media Videos Starter Packs
teachmepython.bsky.social
Install the latest #Python today with uv:

uv python upgrade 3.14
teachmepython.bsky.social
#Python Tricks

Want to read the Python code of the file you are executing?

Add the following to the end of your script and it will output the entire file to stdout:
teachmepython.bsky.social
#Python 3.14 is out now!

🐍 - Free-threading
🐍 - Deferred annotations
🐍 - t-strings
🐍 - ZStandard compression
🐍 - Multi-interpreter support
🐍 - JIT compiler included for Windows and MacOS
🐍 - and more!
teachmepython.bsky.social
#Python Pop Quiz 🐍❓

What is the output of the following code?

A) Syntax Error
B) Type Error
C) Case A Found!
D)
teachmepython.bsky.social
Need to find the most common number in a sequence with #Python? 🐍🔥

The collections module has a great class for that!
teachmepython.bsky.social
#Python Pro Tip - How to flatten a list of lists 🐍🔥
teachmepython.bsky.social
Stupid #Python trick 🐍🤯

Calling a dunder method directly without using name mangling
teachmepython.bsky.social
#Python Pop Quic 🐍❓

A) {range}
B) (range)
C) [0, 1, 2]
D) (0, 1, 2)
E) {0, 1, 2}
teachmepython.bsky.social
I don't use lambdas very often, but they are very handy in #Python when you use the built-in sorted() function

By using a lambda, you can do some cool advanced sorting! 🐍🔥
teachmepython.bsky.social
What have you used #Python's case statement for? 🐍❓

If you haven't seen it, here's an example:
teachmepython.bsky.social
#Python Pop Quiz 🐍❓

What will be the output if you run this code?

A) Outer ZeroDivisionError exception caught
B) Error: You divided by zero!
C) Error: You divided by zero! Finally executed
D) Finally executed
E) None of the above
teachmepython.bsky.social
Need to time how long a short snippet of #python code takes to run? Use the timeit module! 🐍🔥

You can also use timeit to time functions and imports!
teachmepython.bsky.social
Here's a silly example of a function factory using #Python and lambdas

Normally, you wouldn't use lambdas, but you might have a list of functions or a use a generator to return functions or callables.
teachmepython.bsky.social
Here's some ternary fun with #Python 🐍🔥
teachmepython.bsky.social
How to use #Python threads to target specific functions as workers
teachmepython.bsky.social
How often do you use the ternary operator in #Python?

Here's an example in case you haven't heard of it:
teachmepython.bsky.social
Have you ever used a generator expression in #Python? They are similar to a list comprehension except that you get a generator returned.

Check it out:
teachmepython.bsky.social
Function overloading in #Python using the functools module! 🐍🔥
teachmepython.bsky.social
One of the craziest lambdas I have ever seen in #Python 🐍🚀

The obfuscated "Hello World" by Ben Kurtovic
teachmepython.bsky.social
Have you ever used #Python's handy ChainMap collection?

You can use ChainMap to combine dictionaries, among other things:
teachmepython.bsky.social
Here's a silly #Python trick. 🐍🔥

You can use lambdas to create a simple function dictionary where you can call each function using the key in the dict:
teachmepython.bsky.social
GitKracken gave me a 50% off GitKracken Pro subscription link.

I've been using GitKracken for a few years now and find it really useful, especially with complex merges or working with large teams.