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!
You can’t use a list as a Python dict key: Why not? Dicts run “hash” on a key to choose a pair’s storage location. To avoid pairs getting lost, mutable builtins (list, set, dict) cannot be used as dict keys.
TL;DR: If you teach Python, then you should check out course-setup (https://pypi.org/project/course-setup/) at PyPI! I’ve been teaching Python and Pandas for many years. And while I started my teaching career like many other instructors, with slides, I quickly discovered that it was better for my students — and for me! — to replace them with…
A Python Pandas column contains comma-separated values. How can you get the first of those? First, break each string into a list: Then use .str[0] to grab the first element from the resulting list:
Want to check whether a Python Pandas string series only contains digits? Use str.isdigit: This returns a boolean series, with df’s index. You can then convert the digit-containing strings to integers:
Want to retrieve a slice from a string column in your Python Pandas data frame? Use .str.slice:
Want to retrieve from a string column in your Python Pandas data frame? Use .str.get: But wait: You can use [], in place of .get: