Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Python Cheat Sheet (docs.google.com)
143 points by vacipr on Sept 4, 2012 | hide | past | favorite | 34 comments


This cheat sheet sucks. Some examples:

> list1.extend(object1): Extends list1 with object1 creating a whole list instead of append which jams a an object inside the list.

Extend takes an iterable and not an object. Not sure I can even comprehend that sentence.

> dictionary1.update(dictionary2): updates dictionary1 with values from identical keys in dictionary2

Identical to what? Update adds all key-value pairs from dictionary2. I guess the text might refer to the fact that update overwrites existing values.

> ‘string1’.strip(): Returns one string with whitespace removed.

Only at the start and end of the string.

> ’string1’.count(object1): Returns the number of times that object1 appears in string1.

This method doesn't exist as far as I can tell and I'm not sure how an object "appears" in a string.


> This cheat sheet sucks. Some examples:

Make a better one and get it to the front page of HN.


Whether or not I make one has no bearing on this one's quality.


Looks like it exists, and works (at least for strings):

  >>> 'test'.count('e')
  1
  >>> 'test'.count('')
  5
  >>> 'test'.count('tes')
  1
  >>> 'test'.count('t')
  2
  >>> 'test'.count('apple')
  0
Seems to be the equivalent of `in` on a string but with a count.


It does exist for strings, but the list contains both `.count('string2')` and `.count(object1)`.


It also works on lists, I think he got confused.


Aren't all iterables objects? You can turn any object into an iterable by adding through __iter__ method IIRC


Yes, but all objects are not iterables. If you list1.extend(object1) as the cheat sheet describes and object1 has no __iter__, it won't work. Likewise, you can iterate strings, so understanding the target of the iterable will be frustrating for list_of_strings.extend('newstring').

This is better described in python's native help documentation:

  $ python
  >>> help()
  >>> list
searching for extend via '/extend':

   |  extend(...)
   |      L.extend(iterable) -- extend list by appending elements from the iterable
Concise and correct.


I'm not critizing this, which looks nice esp for people learning (and I'm sure somepeople "work" better with cheat sheets)

But, one reason I like Python is it's small and regular enough to not need a cheat sheet (this does not apply to stdlib which has large swaths of wtfness, and is changing as people keep bloating up the language, ternary)

And, any decent editor should be highlighting, tab completing and hooked into pyhelp to provide a context aware , dynamic, interactive "cheat sheet". E.g vim with following vim plugins pydoc, python_calltips, supertab and proly should throw in pep8 and pyflakes. Check it -> https://github.com/njharman/dotfiles


I like this:

http://rgruet.free.fr/PQR27/PQR2.7.html

Other versions available.

Go to the page and search for your thing, easy.


Thanks for sharing this, and thanks to the OP for these great resources. After taking a few Udacity courses, this will help me retain/expand on my Python skills!


Its really good but no way quick reference.


No offense to the author, but the length of this thing! may as well just use http://docs.python.org/library/index.html


More like a cheat packet than a cheat sheet


I think the length is just fine. Longer than other cheat sheets, sure, but still far easier to scan than the official documentation.


Any idea why they appear to prefer `cmath.pi` and `cmath.e` rather than `math.pi` and `math.e`?


[deleted]


While that convention is generally true, in this case the c in cmath refers to complex numbers.


rom is correct, both the math and cmath modules are implemented in C. And even if the math module was pure Python I don't think that the speed of accessing a constant would be drastically different.


I like it a lot and looks very handy. But the author seems to be missing the point of a cheat sheet. I'd prefer something I can post on my desk to take a quick look, a 6 page summary is not a cheat sheet nor it's faster than googling.


Nice work! One minor thing I noticed... dictionary.has_key is deprecated, use the 'in' keyword instead: http://docs.python.org/release/3.1.5/whatsnew/3.0.html#built...


I don't have any language cheatsheets anymore and just use http://hyperpolyglot.org/ it has great examples and comparisons for most all languages I'm interested in, in a nice and easy to read format too.


Here is one that is literally just 1 page: http://i.imgur.com/tpze9.jpg



As someone currently going through Learn Python The Hard Way - this is an awesome resource. A lot of it is over my head, but it is an awesome way to scan through stuff without having to dig through the official documentation. It also helped solidify some of the concepts that I am learning, so thank you.


Not mine,found it on reddit.I submitted this here because I thought it might be useful to some people.

Here is the original link: http://www.reddit.com/r/Python/comments/zbwme/is_anyone_inte...


Will some Reddit'r please go and rough him up a bit over the file format (docx). I was going to post this here, until I noticed that it was a repost from Reddit. Cheers

Please use non-proprietary document formats. It's the polite thing to do. http://www.fsf.org/campaigns/opendocument/reject


Yeah, that's the spirit! Some person makes a free document that they think will help people, so let's push document formats down their throat.

Be sure to tell them about Linux while you're harping on their file format choice.


I understand what and why are you saying that. But I don't think it would be polite at all to rough the author up. Considering the time that (s)he took to write this down and decided to shared with the world.

Sorry, but it sounded really weird to me to ask for politeness in this context. What I would do is suggesting the change of format, or even better do it by myself if that would be bothering me that badly. Just saying...



Shameless plug: I have a two-page cheat sheet: http://pedrokroger.net/python-quick-reference/


I like this. Anyone feel like extending this to Numpy? I tend to use the Matlab/Numpy conversion page all the time.


iPython[1] or bPython[2] will usually be a better reference since you can explore API's, documentation, and source code directly.

[1] http://ipython.org/

[2] http://bpython-interpreter.org/


Amazing! Thanks for this.


Sweet, thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: