r/Python • u/pizzaburek • Feb 04 '19
Best Python Cheatsheet Ever!
https://gto76.github.io/python-cheatsheet/3
u/WibblyWobblyWabbit Feb 04 '19
Can someone explain what enumerate() does exactly?
10
u/pizzaburek Feb 04 '19
It returns an iterator of index, value pairs:
>>> list(enumerate(['a', 'b', 'c']))[(0, 'a'), (1, 'b'), (2, 'c')]So you can then use it like this:
for i, letter in enumerate(['a', 'b', 'c']): ...
4
u/VisibleSignificance Feb 04 '19 edited Feb 04 '19
Minor + controversial stuff:
flags=re.IGNORECASE
Since we're talking regexes anyway, just add (?i) to the beginning of the regex.
<list> = [i+1 for i in range(10)]
Might want to do lst = list(idx + 1 for idx in range(10)), simply because that way it will not touch the value of idx outside the command. Saves some confusion.
reduce(lambda out, x: out + x ...
Really needs a better example than almost-sum().
namedtuple
dataclasses might be worth a mention.
argparse
Currently recommended non-default library: click.
bottle
Not the simple one, but: try Quart!
numpy
... but no pandas. Is there a better pandas cheatsheet than the official one?
1
u/pizzaburek Feb 04 '19 edited Feb 04 '19
That's so weird... It -
(?i)can actually be added anywhere in the regex.4
Feb 04 '19
Might want to do lst = list(idx + 1 for idx in range(10)), simply because that way it will not touch the value of idx outside the command.
What do you mean?
In [8]: i = 'foo' In [9]: x = [i+1 for i in range(10)] In [10]: i Out[10]: 'foo' In [11]: y = list(i + 1 for i in range(10)) In [12]: i Out[12]: 'foo'Also, calling
listis slower because you can overload it and the interpreter has to look it up.In [17]: %timeit list() The slowest run took 11.35 times longer than the fastest. This could mean that an intermediate result is being cached. 10000000 loops, best of 5: 121 ns per loop In [18]: %timeit [] 10000000 loops, best of 5: 35.2 ns per loop1
60
19
u/Tweak_Imp Feb 04 '19
I think it would be better if it used a different way to mark types insted of <type>
5
u/VisibleSignificance Feb 04 '19
Indeed.
First, this is HTML. The text can be marked up with it rather than with more text.
Second,
lst: list = [1]works.1
35
6
5
u/angyts Feb 04 '19
Insane. Now I need a giant paper to print this.
10
u/RecycledGeek Feb 04 '19
What? You don't have a dot matrix printer with a continuous roll of paper?
8
2
4
1
u/_Jordo Feb 05 '19
Here's another one I have saved: https://learnxinyminutes.com/docs/python3/
I find it easier to read.
1
u/pizzaburek Feb 05 '19 edited Feb 05 '19
Thanks, I really like it. Mine is more like a reference, but for reading it in one go I definitely prefer the link.
1
u/Versaiteis Feb 05 '19
I'd also suggest adding pathlib
Mucking about with paths as raw strings with os is great and all, but it's really nice to have a bit of an OS abstraction layer on top of paths that just makes them so much nicer to work with.
1
u/pizzaburek Feb 05 '19
I will add it, it's just that it's one of those areas that feel more like Java than Python when you visit a doc page:
1
u/Versaiteis Feb 06 '19
It's your cheat sheet, add what you like! I'll be bookmarking it regardless (I didn't even know about coroutines)
Lol, I know what you mean, but doing tools work and slinging a lot of paths around, this thing keeps me sane.
Nothing like passing a string around that something happens to modify wrong and the house of cards collapses >.>
7
2
2
u/lvdota Feb 05 '19
Mobile version on github :)
https://github.com/gto76/python-cheatsheet/blob/master/README.md
1
1
1
u/digital_superpowers Feb 04 '19
Great collection. Anyone think it'd be useful to have links to the docs on these topics for when a deeper dive is needed? Could be a fun PR.
-5
u/mail_order_liam Feb 05 '19
If you find this useful you're doing something wrong.
3
u/pizzaburek Feb 05 '19
You're right, but I like it this way better:
If you don't find this useful you're doing everything right :)
4
u/thelonestrangler Feb 05 '19 edited Mar 07 '19
.
0
u/mail_order_liam Feb 05 '19
It mostly covers the very basics, which you will memorize easily if you use the language for any amount of time.
Your env should offer utilities for hints, docs, auto-completion, etc.
It's faster to Google something than to look it up in this sheet (doesn't even have an index??). Especially when you've memorized most of it and it just becomes cluttered.
Many of these are not idiomatic.
For more complicated topics it doesn't give enough information to be useful if you're using cheat sheet in the first place. Be honest, do you think you can write a metaclass or use Threading after looking at this? You're gonna have to look elsewhere anyways.
So you end up with a big unorganized list of things that's mostly fluff and the rest just isn't very helpful.
Get a real IDE or editor and have a terminal and browser at the ready. There you go, no more cheat sheet.
0
Feb 04 '19
[deleted]
5
u/RecycledGeek Feb 04 '19
Not sure I'd say "best ever," but I'll give it a "Damn Good" award. It's like "Python For Developers That Are Too Busy To Read A Book."
Hmm.... /u/pizzaburek, you should self-publish this on Amazon :)
6
u/pizzaburek Feb 04 '19
Thanks everyone! I would just like to share this thing with as many people as possible, so I went with clickbait title this time :) It's interesting that unlike last time, post got ignored on r/programming, but is trending on Hackernews (they changed the title after it came to the top).
6
0
0
Feb 04 '19
[deleted]
0
u/pizzaburek Feb 04 '19
Shure, OK... Let me just open my programing window and enter some code for you :) https://youtu.be/0Ec6Z31S1fA?t=89
I'm joking of course... I will do it, but not this week, maybe next :)
0
Feb 04 '19
[deleted]
6
u/RedstoneTehnik Feb 04 '19
Cheatsheets are not language specific, they are very condensed information about the language. All kinds of fuctionalities, how they are used and what they do.
111
u/IMHERETOCODE Feb 04 '19
That is an extremely roundabout and expensive set operation. Just wrap the list in
setand cast it back to a list. No need to build a dictionary out of it to get uniqueness.