Python Basics Onepager
github.com
github.com
I don't mean to be negative, but that is an impediment to me using it
* Python Crash Course cheatsheet (https://ehmatthes.github.io/pcc_3e/cheat_sheets/)
* Comprehensive Python cheatsheet (https://gto76.github.io/python-cheatsheet/)
* Scientific Python cheatsheet (https://ipgp.github.io/scientific_python_cheat_sheet/)
* Common beginner errors (https://pythonforbiologists.com/29-common-beginner-errors-on...)
* Python regular expression cheatsheet (https://learnbyexample.github.io/python-regex-cheatsheet/) — my blog post, includes examples as well
Also there a small typo in: `"".join(["a", "B", "c"])` # 'abc'`.
fun1(fun2(fun3(xyz, 10)), 12)
I expect it to be more like this: xyz | fun3(_, 10) | fun2(_, 12) | fun1
Unfortunately, Python creators reserved "|" for dictionary merging :/ class Placeholder:
def __init__(self) -> None:
pass
_ = Placeholder()
class Arrow:
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
if not any(isinstance(arg, Placeholder) for arg in args) and \
not (kwargs and any(isinstance(arg, Placeholder) for arg in kwargs.values())):
return self.func(*args, **kwargs)
else:
placeholder_indices = [i for i, arg in enumerate(args) if isinstance(arg, Placeholder)]
placeholder_kwargs = {k: v for k, v in kwargs.items() if isinstance(v, Placeholder)}
original_kwargs = {k: v for k, v in kwargs.items() if not isinstance(v, Placeholder)}
def new_func(new_args):
assert len(new_args) == len(placeholder_indices) + len(placeholder_kwargs), "ERROR: Wrong number of arguments"
return self.func(
*[new_args[placeholder_indices.index(i)] if i in placeholder_indices else arg for i, arg in enumerate(args)],
**{k: new_args[len(placeholder_indices) + i] for i, k in enumerate(placeholder_kwargs)},
**original_kwargs)
return Arrow(lambda *new_args: new_func(new_args))
def __ror__(self, other):
if isinstance(other, tuple):
return self.func(*other)
return self.func(other)
@Arrow
def add(x, y, z):
return x + y + z
@Arrow
def multiply(x, y):
return x * y
@Arrow
def subtract(x, y):
return x - y
@Arrow
def add_one(x):
return x + 1
print((1, 5) | add(_, 3, _) | add_one | subtract(_, 2) | subtract(50, y=_))90%+ loops I used in my coding is to iterate elements in a list-like object. It feels very natural to use "for x in arr: do_sth(x)". And the same pattern can be reused with your typical "step increase by 1" loop by using `for i in range(LEN)`, instead of having to memorize another different pattern of `for (i=0; i<LEN; i++)`.
I particularly hate JS's forEach structure. for one, it doesn't follow how I think, and it's different from the other form of loops. And for two, I hate to type these pesky symbols (`a.forEach(e=>{doSth(e);})`) than just word(s).
Furthermore, JS also introduced `for in` and `for of`, which are great, but they're somewhat counterintuitive to my taste. You use `for ele of array` and `for index in array`, sounds fair. To iterate keys of objects, you use `for ... in`, but there is no `for ... of`. To make things worse, not all Array-like objects even support it. I find myself constantly "re-discover" (by that I mean I've google'd it multiple times before) the trick of things like `[...arr]`, and I hate it.
Some disclaimer: I know above is purely my preference, and not trying in anyway to say one is superior than the other. And I imagine for experienced programmers, they probably don't even think of all of these when using, it's just muscle memory. But for me, someone who code in Python/JS about half and half, I still struggle with, and have to google, basic syntax and structure in JS frequently, while I can do similar things without thinking in Python since when I was very new to the language. (And I know Python didn't invent these syntaxes.)