Think you can’t modify a Python list while iterating? You can append to it: This prints 10, 20, 30, 100, 400, and 900. BUT don’t insert or remove. That’ll cause real trouble.
Think Python functions can return multiple values? They actually return ONE value, a tuple: Unpack it into separate variables:
Let users assign using [] in Python, with custom logic with __setitem__: sl.d[‘2’, ‘4’, ’99’, ‘8’]
The __getitem__ method in Python can do more than simple lookups: Use [] for custom logic — returning filenames, random numbers, or database records.
Want your Python object to support bracket notation []? Define __getitem__:
Python Pandas gotcha: You can’t join a single column! Why? join() is a data frame method. A single column is a Series. Solution: Use [[ ]] to get a one-column data frame:
I used Python Pandas to total AWS S3 payments. Data was in a CSV file. But numbers in the “Amount” column were written as “USD 12.34”. Here’s what I did:
Python gotcha: Forgetting () on method calls! Result: Error: ‘builtin_function_or_method’ object is not iterable Remember: () calls the method!
Python Pandas 3.0 introduces pd.col, which shortens queries: Also:
Think “from modname import funcname” saves memory in Python? It doesn’t! (Lots of people think it does.) The full module still loads (see sys.modules). The only difference: “modname” isn’t in your namespace, just “funcname”. “from import” is convenient. It doesn’t save memory.