The Python cheat sheet is a one-page reference sheet for the Python programming language.
|
argv
|
Command line args
|
|
builtin_module_names
|
Linked C modules
|
|
byteorder
|
Native byte order
|
|
check_interval
|
Signal check frequency
|
|
exec_prefix
|
Root directory
|
|
executable
|
Name of executable
|
|
exitfunc
|
Exit function name
|
|
modules
|
Loaded modules
|
|
path
|
Search path
|
|
platform
|
Current platform
|
|
stdin, stdout, stderr
|
File objects for I/O
|
|
version_info
|
Python version info
|
|
winver
|
Version number
|
|
sys.argv[0]
|
foo.py
|
|
sys.argv[1]
|
bar
|
|
sys.argv[2]
|
-c
|
|
sys.argv[3]
|
qux
|
|
sys.argv[4]
|
--h
|
|
altsep
|
Alternative sep
|
|
curdir
|
Current dir string
|
|
defpath
|
Default search path
|
|
devnull
|
Path of null device
|
|
extsep
|
Extension separator
|
|
linesep
|
Line separator
|
|
name
|
Name of OS
|
|
pardir
|
Parent dir string
|
|
pathsep
|
Patch separator
|
|
sep
|
Path separator
|
|
__new__(cls)
|
__lt__(self, other)
|
|
__init__(self, args)
|
__le__(self, other)
|
|
__del__(self)
|
__gt__(self, other)
|
|
__repr__(self)
|
__ge__(self, other)
|
|
__str__(self)
|
__eq__(self, other)
|
|
__cmp__(self, other)
|
__ne__(self, other)
|
|
__index__(self)
|
__nonzero__(self)
|
|
__hash__(self)
|
|
|
__getattr__(self, name)
|
|
|
__getattribute__(self, name)
|
|
|
__setattr__(self, name, attr)
|
|
|
__delattr__(self, name)
|
|
|
__call__(self, args, kwargs)
|
|
|
append(item)
|
pop(position)
|
|
count(item)
|
remove(item)
|
|
extend(list)
|
reverse()
|
|
index(item)
|
sort()
|
|
insert(position, item)
|
|
|
capitalize() *
|
lstrip()
|
|
center(width)
|
partition(sep)
|
|
count(sub, start, end)
|
replace(old, new)
|
|
decode()
|
rfind(sub, start ,end)
|
|
encode()
|
rindex(sub, start, end)
|
|
endswith(sub)
|
rjust(width)
|
|
expandtabs()
|
rpartition(sep)
|
|
find(sub, start, end)
|
rsplit(sep)
|
|
index(sub, start, end)
|
rstrip()
|
|
isalnum() *
|
split(sep)
|
|
isalpha() *
|
splitlines()
|
|
isdigit() *
|
startswith(sub)
|
|
islower() *
|
strip()
|
|
isspace() *
|
swapcase() *
|
|
istitle() *
|
title() *
|
|
isupper() *
|
translate(table)
|
|
join()
|
upper() *
|
|
ljust(width)
|
zfill(width)
|
|
lower() *
|
|
|
close()
|
readlines(size)
|
|
flush()
|
seek(offset)
|
|
fileno()
|
tell()
|
|
isatty()
|
truncate(size)
|
|
next()
|
write(string)
|
|
read(size)
|
writelines(list)
|
|
readline(size)
|
|
|
len(a)
|
6
|
|
a[0]
|
0
|
|
a[5]
|
5
|
|
a[-1]
|
5
|
|
a[-2]
|
4
|
|
a[1:]
|
[1,2,3,4,5]
|
|
a[:5]
|
[0,1,2,3,4]
|
|
a[:-2]
|
[0,1,2,3]
|
|
a[1:3]
|
[1,2]
|
|
a[1:-1]
|
[1,2,3,4]
|
|
a[::-1]
|
[5,4,3,2,1]
|
|
a[::-2]
|
[5,3,1]
|
|
b=a[:]
|
Shallow copy of a
|
|
today()
|
fromordinal(ordinal)
|
|
now(timezoneinfo)
|
combine(date, time)
|
|
utcnow()
|
strptime(date, format)
|
|
fromtimestamp(timestamp)
|
|
|
utcfromtimestamp(timestamp)
|
|
|
replace()
|
utcoffset()
|
|
isoformat()
|
dst()
|
|
__str__()
|
tzname()
|
|
strftime(format)
|
|
|
%a
|
Abbreviated weekday (Sun)
|
|
%A
|
Weekday (Sunday)
|
|
%b
|
Abbreviated month name (Jan)
|
|
%B
|
Month name (January)
|
|
%c
|
Date and time
|
|
%d
|
Day (leading zeros) (01 to 31)
|
|
%H
|
24 hour (leading zeros) (00 to 23)
|
|
%I
|
12 hour (leading zeros) (01 to 12)
|
|
%j
|
Day of year (001 to 366)
|
|
%m
|
Month (01 to 12)
|
|
%M
|
Minute (00 to 59)
|
|
%p
|
AM or PM
|
|
%S
|
Second (00 to 61⁴)
|
|
%U
|
Week number¹ (00 to 53)
|
|
%w
|
Weekday² (0 to 6)
|
|
%W
|
Week number³ (00 to 53)
|
|
%x
|
Date
|
|
%X
|
Time
|
|
%y
|
Year without century (00 to 99)
|
|
%Y
|
Year (2008)
|
|
%Z
|
Time zone (GMT)
|
|
%%
|
A literal "%" character (%)
|
Thanks Dave.
Are model names capitalized or not? How about references to models in views.py or forms.py etc? It looks like Flask might do this but not clear. Does it even matter? Thanks!
There seems to be some UTF-8 weirdness with the footnote superscripts in the date formatting section.
@cosmin - if you click on the thumbnail image to the right of the comments it will pop up a window where you can download the PDF.
Cosmin - click the "Download" link at the top-right of the sheet. My thanks also to Dave Child for this tool - your work is much appreciated.
Curious, is there a way to download this as a PDF? I'm sometimes away but I like to carry stuff like this with me. Thank you.
Not "patch" os.pathsep The character conventionally used by the operating system to separate search path components (as in PATH), such as colon for POSIX or semicolon for Windows. Also available via os.path.
Missing, reverse a string >>> 'hello world'[::-1] 'dlrow olleh'
I would add: with patterns and how to roll your own; list comprehensions; generators and methods of making them; decorators; getattr, hasattr, setattr. At least I ask people that say they really know python well about these things.
Thank you, Dave, for your generosity. This will be helpful in a course I'm taking on 'Programming Fundamentals with Python'.
Hi Dave, thanks for the opensource information. From a Newbie Pythionista wannabe
This is a great cheatsheet. I'd add a few things, though I don't know what I would remove. I'd add dictionary methods, built-in functions (http://docs.python.org/library/functions.html), and the regular expression functions, since they are odd and I can never remember which one returns what.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.