Remember that Python has two division operators:10 / 2 # truediv, returns float: 5.010 // 2 # floordiv, rounds down: 510 / 3 # truediv: 3.333…10 // 3 # floordiv: 3 Note: // with floats returns a float (10.0 // 3 = 3.0)
Unsure how many values you want to unpack in Python? Use * to get a list of flexible length: Note: You can only have one * variable.
Using uv to manage your Python project, and want to increase the version in pyproject.toml? Use “uv version”, specifying what level to increase: uv version –bump=minor uv version –bump=majoruv version –bump=dev Type “uv version –bump” with no value for full docs.
Make your Python comprehensions easier to read, write, and debug with multiple lines. # better than:[n**2 for n in range(10) if n%2]
Want to count values in a Python sequence? Use Counter: Bonus: Counter inherits from dict, getting its methods + operators.
Why use OrderedDict if Python dicts are now ordered? Because equality checks order too:
Make a Python class sortable with __lt__ and __eq__:
By default, str.strip in Python removes leading/trailing whitespace: It takes an optional argument, naming leading/trailing characters to remove. Great for cleaning strings:
Instead of choosing which Python function to run with if/elif/else, use a dispatch table — a dict with functions as values:
Modern Python dicts remember insertion order: This applies in for loops, too!