Skip Navigation

Python Cheat Sheet

Overview

Python Cheat Sheet The Python cheat sheet is a one-page reference sheet for the Python programming language.

If you like the cheat sheets, and want to say thanks, please consider buying me something from my Amazon Wishlist. Thankyou very much to those who have already hunted it down and sent me something - I'm very grateful!

Downloads

The Python Cheat Sheet is released under a Creative Commons License (Attribution, Non-Commercial, Share Alike).

Please note: If you wish to link to a cheat sheet from elsewhere, please link to this page so others find all available versions, the license and the description.

sys Variables

Thumbnail highlighting sys variable section. Python has a variety of built-in variables which give information about the system on which it is running.

sys.argv

Thumbnail highlighting sys.argv. sys.argv contains command line arguments in a zero-based list.

os Variables

Thumbnail highlighting os variables. In a similar fashion to the "sys" variables, Python also has built in variables which give information about the operating system on which it is running. This section also lists the "registered" OS names.

Methods

Thumbnail highlighting methods. Python, as you might expect, has a huge variety of standard methods for working with strings, lists, files and classes, and this section lists those with, where possible, compulsory arguments.

Indexes and Slices

Thumbnail highlighting indexes and slices. Indexes and slices have been known to trip up plenty of new programmers, and occasionally experienced programmers new to Python. This section of the cheat sheet shows by example how indexes and slices work in Python.

Date and Time

Thumbnail highlighting Date and Time. These sections of the cheat sheet include the DateTime and Time methods, as well as the various Date Formatting options available for the strftime and strptime functions.

Thanks

This is the second cheat sheet created with the involvement of the Cheat Sheets Google Group, and the first to include twitter (@CheatSheets). Many thanks to Charly Walther and Dan Horning for their suggestions and feedback. Finally, thanks also to the 1017 people who requested this cheat sheet!

Translations

24 comments

Thanks for this. I jumped into django without knowing python too well and I will use this reference quite a bit while I catch up on all of the python I should know.
 United Kingdom #2: January 22, 2009
Thanks for the cheat sheet. I'd like to learn Python this year, and no doubt this will be invaluable!
Thanks a lot...

But I have one question - why you don't place "Indexes and Slices" into first column and don't use whole second column for "Methods"???
@Greg, @Paul: Thanks.

@Steward: Just a matter of space. The middle column would be longer than the sheet if I had moved the class methods into it. I would have had to remove useful info.
Oh... I see... maybe you can make blank rows more shortly :)
Thanks. Very nice sheet. Really useful at daily work.
Wes
United States #7: January 23, 2009
Very nice.

Possible addition for the indexes & slices:
a[::-1] reverses a list ( [5,4,3,2,1,0] )
As usual great stuff! A must read post for any newbie!
I've added a couple of extra items to the sys.argv list to demonstrate how command line attributes (e.g., -c and --h) work.

@Wes: Great idea. I've had a quick look and it's going to be tough to squeeze in but I'm sure I'll find a way.

@Benjamin, @Me: Thanks :)
A very useful sheet. Cool!
I am getting ready to prep for a new python class. Thank you so much for your sheets. I send my student to your site often. :-)
Damn your good! Came here for a Python cheat sheet and its on the front page!
Hi! Yet another wonderful cheat sheet that I'll certainly be using from now on, thanks! The html, html character entities, css and javascript ones already help me a great deal and are all properly posted to the wall behind my monitor. The other ones I use are the PHP, regular expressions, subversion, MySQL, Ruby on Rails and microformats ones, though these ones are kept handy on a clipboard on the corner of my desk. This one is probably going to the clipboard too...
Cool. Shorter than the famous http://www.limsi.fr/Individu/pointal/python/pqrc/. Can be useful.
I tried to add a few suggestions for cheat sheets but the add button is disabled. The ones I wanted to add were emacs, TeX and LaTeX.
Artur
Poland #16: June 18, 2009
Indexes and Slices
b=a[:] Shallow copy of a

This is not a shallow copy, or am I wrong?
 Hannover/Germany #17: July 3, 2009
Great one! Thanks a lot for your work.
I am doing a spanish translation of this cheatsheet to use in a brochure for my company at PyCon Argentina, and am creating the PDF using restructured text and rst2pdf. Here you can see what it looks like:
http://lateral.netmanagers.com.ar/static/machete.pdf .

Is that attribution good enough for you? I can change it :-)
#18 Roberto: That attribution is perfect, thanks. If you're happy for me to do so, I'd like to add a link to the Spanish version for others to use if they prefer - please let me know if that is ok and if so where you would like the link to point.
Cool. As soon as it´ s ready I ´ll post the link here.
Done, posted my version at http://netmanagers.com.ar/machete (spanish and english), the sources are available in github.

This version is one A4 page, meant to be folded in 3.
Great work, Roberto! I've added the link above.
Great article!!!

-- KIZILSUNGUR
I leran python for my django app. thx a lot

Post Your Comment

· Comments with keywords instead of a name have their URLs removed.
· Your email address will not be displayed or shared.

Live Comment Preview

 United States #25: 1 minute ago