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

Show parent comments

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.

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