Have a Python Pandas series with datetime values, and want all those until now? Compare with pd.Timestamp.now(): This returns the rows from df where the “when” column is before now.
Want to check whether a Python Pandas series contains another string? Use .str.contains: This returns a boolean series, whose index matches that of df. Keep only those rows containing ‘a’:
Keyword-only parameters in a Python function can have a default: Now call it: Remember: parameters with defaults come *after* those without.
Any Python function parameters after *args are keyword-only: Want to give k a value? Use a keyword argument: Don’t need *args? Just use *, and k is keyword only:
How to annotate *args in Python? Just set the elements’ type; we know it’s a tuple: Normally, tuples contain different types. In args, we assume all values have the same type.
Classic use of Python’s *args: Invoke it with separate int arguments: and *not* with a list containing ints:
Want your Python function to take any number of positional args? Use *args: – Can be any name, but args is traditional– The * is only in the function definition– It’s a tuple– Don’t grab elements by index; iterate over them– It might be empty– Pronounce it “splat args”
How are arguments assigned to parameters when calling a Python function? There are two ways: – Positional, based on order– Keyword, i.e., name=value, assigned by name Want to mix and match? Fine, but all positional must come before all keyword:
Every import in Python executes the module, from start to finish. Meaning? Big modules slow down your program’s startup time. Coming in Python 3.15 this October: “lazy import”, which delays the load until a name is used: lazy import MODULElazy from MODULE import NAME
Mutable builtins in Python cannot be dict keys. But your (mutable) classes can!