gto76 / python-cheatsheet Goto Github PK
View Code? Open in Web Editor NEWComprehensive Python Cheatsheet
Home Page: https://gto76.github.io/python-cheatsheet/
Comprehensive Python Cheatsheet
Home Page: https://gto76.github.io/python-cheatsheet/
I love this cheat sheet!
It would be great to add some points about how to make production-grade code, such as:
Some points about how "import" works would be great too (one of the most used yet not understood feature in Python)
I realize that those are quite broad topics (especially the last one), but that would sure be very helpful
Hi,
What was the design choice around using strong emphasis (double asterisks) for most non-code text?
Firstly, nice doc.
For exceptions, having a comment on which is executed for what failures would be good for else and finally
from datetime import datetime, strptime
now = datetime.now()
now.month # 3
now.strftime('%Y%m%d') # '20180315'
now.strftime('%Y%m%d%H%M%S') # '20180315002834'
<datetime> = strptime('2015-05-12 00:39', '%Y-%m-%d %H:%M')
The first line has a typo and should be from datetime import datetime, strftime
.
But you can't import strftime from the root module.
datetime.datetime.now() returns a datetime object, and those can be called with strftime I.E.
from datetime import datetime
datetime.now().strftime('%b')
# --> Feb
Simply the first line just needs the last word deleted off. Don't forget the typo in the last line either ;)
I'd take this opportunity to mention a few cool tricks about from datetime import date
but I'm at work right now. If I get a chance later I may open a pull request.
Full python code cheats
Python
Read thru the source, and in the process markdown linted and reformated the source for readability.
I don't know how/if I can upload the git commit, so I attached source file here.
Feel free to use any of my suggested changes to the source.
Thanks
README.md
Navigating the HTML version of this cheat sheet would be easier if there was a floating table of contents.
RomaMolora
Клалаьа
Thank 👍
Thanks for the Cheat Sheet
The definition of any(collection) says "False if empty", but that's not the whole story.
any( [ False ] ) is False even though the argument is not empty.
thank you
In python 3.8 there is a more elegant way to calculate the product of the elements.
Instead of
product_of_elems = functools.reduce(lambda out, el: out * el, <collection>)
it can be done like this
product_of_elems = math.prod(<collection>)
No issue at all. I just wanted to thank you for making your cheatsheet available. Really comprehensive and helpful – very much appreciated! 👍
The codes are superb!
Add a directory on top of the Readme to skip to desired stuff
Add a LICENSE.txt file in the root of the repo, with an appropriate license (my vote would be for MIT, BSD, Apache, or CC-BY)
In this section:
out = [i+j for i in range(10) for j in range(10)]
out = []
for i in range(10):
for j in range(10):
out.append(i+j)
The loop variables i and j should be swapped in either the first or the second part. Maybe one of the 10s could even be changed into another value to make it clearer? For instance:
out = [i+j for j in range(5) for i in range(10)]
out = []
for i in range(10):
for j in range(5):
out.append(i+j)
I wonder if you’ve considered the operator convenience functions itemgetter and attrgetter for list sorting:
https://docs.python.org/3.7/howto/sorting.html#operator-module-functions
I find them cleaner than using lambda functions, and the documentation claims that they also have better performance.
Would be nice to have nice printable PDF for the wall hangers enthusiasts 🥇
import itertools
nested_list1 = ["s", "r", [32, [32], 3]]
flattened_list1 = list(itertools.chain.from_iterable(nested_list1))
print(flattened_list1)
nested_list2 = [1, [1, 1, 3, [224, 4], []], [], [2, [23]]]
flattened_list2 = list(itertools.chain.from_iterable(nested_list2))
print(flattened_list2)
# RESULT
# ['s', 'r', 32, [32], 3] (flattened_list) <-- not completely flat
# TypeError: 'int' object is not iterable (flattened_list2) <-- doesnt work at all
I suggest:
from copy import deepcopy
def flatten(nested):
"""Flatten an arbitrarily nested list."""
nested = deepcopy(nested)
flat = []
while nested:
sublist = nested.pop(0)
if isinstance(sublist, list):
nested = sublist + nested
else:
flat.append(sublist)
return flat
list1 = flatten(nested_list1)
list2 = flatten(nested_list2)
print(list1)
print(list2)
# RESULT
# ['s', 'r', 32, 32, 3] (list1)
# [1, 1, 1, 3, 224, 4, 2, 23] (list2)
The basic flow control syntax for if
, else
, elif
, for
, and while
do not seem to be included.
I learned that there are two ways to encode and decode bytes. One has been included:
<bytes> = <str>.encode(encoding='utf-8')
<str> = <bytes>.decode('utf-8')
the other is
<bytes> = bytes(<str>,encoding='utf-8')
<str> = str(<bytes>,encoding='utf-8')
Just to remind:-D. May be you just choose the one you prefer~
index.html
from README.md
at commit)Hello,
I think adding a licence file would be useful as the actual licence of the project is a bit ambiguous (it is by default not possible legally to use any of the code in the cheatsheet).
Is it on purpose ?
Thank you
I've seen tons of "cheat sheets" but this one is different. I'm still trying to figure how to to make it part of my coding environment. I may add some tools around it, like I did a tool called "How Do I" that queries Stack Overflow. I made a GUI. I may make a GUI for this too or something that allows easy access from PyCharm so I never leave my coding environment.
I dunno yet exactly how to integrate this amazing documentation, but, I do know it's a true gem of a find.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.