Want to guarantee that no one can run “from .. import *” on your Python module? Include a non-existent name in the __all__ list: Using “from .. import *” on this module will result in an error!
Writing a Python module? Want to control which names are exported via “from .. import *”? Define __all__, a list of strings indicating which names will be exported via “import *”:
Using many names from a Python module? Import them all: Wait: This is usually a TERRIBLE idea! • Potential namespace collisions• Less readable code• Module upgrades affect globals in your program I know, it’s tempting — but don’t!
Python trick: 0 is falsy, 1 is truthy. Use this for even/odd checks: No need for “if n % 2 == 0” — just use the truthiness!
How many NaNs in a Python Pandas data frame? 1. Use isna, returning a data frame of booleans2. Use sum, returning the number of NaNs in each column3. Use sum again, adding those numbers
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.