Use parentheses to split long Python code across lines: But with parentheses, Python sees it as one line: Or in comprehensions…
Python Jupyter memory quirk: Deleting variables doesn’t always free memory! Use df.head() instead of df to keep big values out of Jupyter’s Out dict.
Playing NYT Spelling Bee? Use Python to see if your word only uses allowed letters (ignoring length + center): With sets, <= checks “subset or equal”:
Create a Python dict with keys a/b/c and values 0: But don’t do this, since the value will be shared and mutable: {‘a’: [‘x’, ‘y’], ‘b’: [‘x’, ‘y’], ‘c’: [‘x’, ‘y’]} 🤯
“round” rounds Python numbers: Remember, .5 goes to the nearest *even* value:
How can you tell if a Python datetime is a weekend (i.e., Saturday or Sunday)? Invoke weekday(). It returns an int; Mon is 0, Sat is 5 and Sun is 6. So:
Consider this Python: Surprised? That’s because “is” is the wrong comparison: • == asks: are these the same value?• is asks: are these the same object? You almost never care about question #2. And so, should rarely use “is”.
Want to make Python numbers more readable? Use _ between digits (not at the start or end):