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

15

u/[deleted] Aug 16 '16

very very nice, would be even nicer if the titles in the .pdf file would be searchable :)

7

u/ehmatthes Aug 16 '16

That's interesting to note. I made the sheets in Word and then exported them to pdf. When I revise the set I'll probably do it using a proper pdf application. I think the formatting in the titles made them non-searchable when they were rendered to pdfs. I think all the non-title text is searchable, including code.

4

u/The3rdWorld Aug 17 '16

these are brilliant, any chance they exist with less colour for easier printing?

9

u/ehmatthes Aug 17 '16

That's a great suggestion. I'll look at making a b/w version of the all-in-one for printing, and send you a message when it's available.

2

u/The3rdWorld Aug 17 '16

great, thanks that'd be brilliant.

2

u/VivisClone Oct 15 '16

Any update on this? I'd love them as well! Thanks for all your hard work!

1

u/throwaway_the_fourth Jan 23 '17

Any update on this? I'd love them as well! Thanks for all your hard work!

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? :)

10

u/bbatwork Aug 16 '16 edited Aug 16 '16

Actually it depends on the version of python you are using... Python 2.7 range returns a list, Python 3.5 range returns a generator, and xrange doesn't even exist, it was renamed range.

And looking at the cheat sheets, it seems to be written for version 3, so the example is correct.

4

u/PurelyApplied Aug 16 '16

Yeah... Is this sub not pro Py3? I thought we were assuming python 3 unless there was evidence to the contrary.

1

u/[deleted] Aug 16 '16

I see!

I was not even thinking about Python 3.x. Thanks for pointing that out :)

2

u/bbatwork Aug 16 '16

I do the same in reverse all the time :)

9

u/0rontes Aug 16 '16

Very good. I've been confused on Classes for a while, and your example was very straightforward.

4

u/pasokan Aug 17 '16

If there is sufficient interest, I will produce similar cheatsheets in LaTeX; I am saying similar because I am not certain if everything is reproducible.

1

u/GoGades Aug 16 '16

Very nice !

In the "if statements" column (page 1), "Boolean values" section - either it's wrong (should be ==), or it's for boolean value assignment, which would be confusing under if statements. Made me do a double-take at any rate.

2

u/ehmatthes Aug 16 '16

It's mean to demonstrate boolean value assignment. I'll clarify the label on that section.

1

u/deltopia Aug 17 '16

I really like this; thank you so much for sharing it with us. My question/attempt to point out a flaw (I don't mean it to sound critical, promise!) -- under Dictionaries -> Accessing a value -> Print("The alien's color is '.... shouldn't the single-quote after "is" be a double quote?

1

u/ehmatthes Aug 17 '16

Good catch! I just fixed that, I'll push it shortly.

1

u/ccnewbie Aug 17 '16

Another quote error: "Simulate searching.""" Should have """ instead of a single leading ".

1

u/ehmatthes Aug 17 '16

Thanks, I'll get that fixed.

1

u/ccnewbie Aug 18 '16

Thanks for putting this together. It has been useful and always a quick and easy reference sheet.

2

u/[deleted] Aug 17 '16

I downloaded your old ones yesterday. Thanks for the update!

2

u/rastamonstahh Aug 17 '16

I've been reading your book for about two weeks now, and I've learned so much! This is even more useful. Thank you so much !

1

u/saintPirelli Aug 16 '16

I'm so gonna print that out at work tomorrow.

1

u/tom1018 tomfaulkner Aug 17 '16

Great guide. Thank you for sharing.

1

u/TamSanh Aug 17 '16

I believe your classes example in the full document is wrong. All classes should inherit from object.

class Dog(object):
    pass

2

u/zahlman zahlman Aug 17 '16

This is only in 2.x. In 3.x it happens anyway.

1

u/TamSanh Aug 17 '16

So if the syntax is superfluous in 3.x and meaningful in 2.x, it should be always done, no?

3

u/zahlman zahlman Aug 17 '16

I would say no, because I want to encourage people to migrate to 3.x. "Explicit is better than implicit" doesn't mean you have to say things that are obvious and assumed by default. There are way too many of those.

1

u/TamSanh Aug 17 '16

If that's the case, then recommend specifying in your documentation explicitly that it's a cheat sheet for 3.x. The only version references are your python book plug that says "Covers Python 3 and 2." Of course, maybe it's just so obvious that it's a cheat sheet for 3.x, or I should have assumed it by default; but I didn't.

1

u/[deleted] Aug 17 '16

save

1

u/washerenowisnt Aug 17 '16

Thank you so much, those are great!

1

u/[deleted] Aug 17 '16

Thanks for this! I've been trying to figure out a way to keep a short list of everything I've been learning in one place. Now you've done it for me.

1

u/Cyb3rdude Aug 22 '16

Awesome work! Printed these out and threw them in my work binder. Thanks!

1

u/affalatoon Oct 22 '16

May thee bless thy soul !

1

u/Lollypop036 Feb 11 '17

Thanks so much for this

1

u/felon_truth Aug 16 '16

This is really good! It will help alot. Now I don't have to read all those beginner books to learn the basics :) Thanks alot for sharing.

4

u/[deleted] Aug 16 '16

It's a Cheat sheet, not a Learning sheet. Go trough a book to learn the basics, it will benefit you in the end.

2

u/felon_truth Aug 16 '16

Much of the basics allready in there. Compressed, in a few pages. It's also a good way to learn the syntax if you allready know the basics from another language.

1

u/[deleted] Aug 16 '16

Since it's so compressed I still recommend new people to not use it as a 'book' but as a 'practice tool'. And true, if you know some basics it is certainly usefull

0

u/[deleted] Aug 16 '16

[deleted]

2

u/KimPeek Aug 16 '16

No, to combine strings you use the join method. Using .format() is a handy way to insert a variable into a particular point in a string. While your way works in this instance, it will become unusable if you try to join a list or variable quantity of words into a sentence or something similar.

3

u/sgthoppy sgtlaggy Aug 16 '16

'{} {}'.format(first, last) and ' '.join((first, last)) are synonymous in this case, but note that in the join line, you need to put the variables into a list/tuple.

I see and use format more often when combining separate variables, while I see and use join more often when operating on a list.