r/learnpython • • Aug 16 '16

Beginner's Python cheat sheets - updated

In the spring I posted a set of cheat sheets aimed at beginners, and many people found them useful. I've since completed the set of sheets, and wanted to share the full set. The additional sheets cover files and exceptions, testing, Pygame, matplotlib, Pygal, and Django.

You can download individual sheets, or download a pdf that includes all the sheets in one document. These sheets aim to remind you of Python's syntax, but they also include brief explanations of the concepts behind the syntax as well.

I wrote these sheets as supporting documentation for readers of Python Crash Course, but I think they're useful for beginners in general. I hope you find them helpful, and I'm happy to answer any questions about them.

447 Upvotes

52 comments sorted by

View all comments

3

u/[deleted] Aug 16 '16 edited Aug 16 '16

I haven't looked through all of it, and I am still very much a beginner in the language.

However, I looked through the Lists Cheat Sheet, and lots of items are unnecessarily complex, and some information is plain out wrong.

I would be happy to post my observations, if they are welcome. I definitely commend the work in putting this together. :)

1

u/ehmatthes Aug 16 '16

What do you see that's wrong?

1

u/[deleted] Aug 16 '16

I don't know what I saw that was wrong, but it was probably a mistake on my part. Sorry about that. Some suggestions though:

To make a list of numbers 1 to 1 million:

numbers = range(1, 1000001)

since range returns a list (ie list() is unnecessary)

For reverse, you can generate a reversed list with slicing:

lists = [1, 2, 3, 4]
reverse_lists = lists[::-1]

I also believe this is a very useful feature in general that should be explained (iterating backwards).

I also believe that it complicates the explanations that everything is assigned to a value, and that you 'print' that value without showing what the result will actually be.

Other than that, it is a great cheat sheet. I'll definitely keep one handy :)

2

u/PurelyApplied Aug 16 '16

numbers = range(1, 1000001) since range returns a list (ie list() is unnecessary)

range returns a generator, not a list.

-1

u/[deleted] Aug 16 '16

Nope.

xrange does, however.

2

u/PurelyApplied Aug 16 '16

Yeah, as already mentioned, it was a misunderstanding of 2 vs 3. I always assume it's 3 in this sub unless there's evidence to the contrary, and hadn't actually looked through the sheets since I was on mobile.

I still haven't gone through the sheet in depth, but noticed that the corner claims "Covers Python 3 and Python 2". There are some significant differences that are worth explicitly mentioning, or it might be worth making separate sheets for 3 and 2.

Tagging OP for visibility. /u/ehmatthes

1

u/ehmatthes Aug 16 '16

I followed the same convention in these cheat sheets that I followed in the book. The material focuses on Python 3, but if something wouldn't work as shown in Python 2 there's a note about it.

For example, list(range(1,101)) doesn't cause an error in Python 2 so I didn't make a note about it. Classes in Python 2 should inherit from object, so there is a note about that.

1

u/zahlman zahlman Aug 17 '16

Would you consider making a note about using generators when you can get away with it and only explicitly dumping them into a list when necessary? :)