Show Menu

Python Cheat Sheet by

Python Cheat Sheet
python

List Methods

append­(item)
count(­item)
extend­(list)
index(­item)
insert­(po­sition, item)
pop(po­sition)
remove­(item)
reverse()
sort()

List Slices and Indexes

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]
b=a[:]
Shallow copy of a
Indexes and Slices of a.
a=[0,1­­,2­,­3­,4,5]

os lib Variables

altsep
Altern­­ative 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
Registered OS names: "­­po­s­i­x",
"­­nt­", "­­ma­c­", "­­os­2­", "­­ce­", "­­ja­v­a­",
"­­ri­s­c­os­­"

Operations on Dicts

d.upda­te(d2)
d.keys()
d.values()
d.items()
d.pop(­key­[,d­efa­ult])
d.popi­tem()
d.get(­key­[,d­efa­ult])
d.setd­efa­ult­(ke­y[,­def­ault])
d.clear()
del d[key]
d[key] = value
 

Datetime Methods

today()
now(ti­mez­one­info)
utcnow()
fromti­mes­tam­p(t­ime­stamp)
utcfro­mti­mes­tam­p(t­ime­stamp)
fromor­din­al(­ord­inal)
combin­e(date, time)
strpti­me(­date, format)

Time Methods

replace()
isofor­mat()
__str__()
strfti­me(­format)
utcoff­set()
dst()
tzname()

Class Special Methods

__new_­_(cls)
__init­__(­self, args)
__del_­_(self)
__repr­__(­self)
__str_­_(self)
__cmp_­_(self, other)
__inde­x__­(self)
__hash­__(­self)
__geta­ttr­__(­self, name)
__seta­ttr­__(­self, name, attr)
__lt__­(self, other)
__le__­­(self, other)
__gt__­­(self, other)
__ge__­­(self, other)
__eq__­­(self, other)
__ne__­­(self, other)
__nonz­­er­o­_­_(­­self)
__dela­ttr­__(­self, name)
__call­__(­self, args, kwargs)

Opetaions on Sets

|
union
&
inters­ection
- ^
différ­enc­e/s­ymetric diff
< <= > >=
inclusion relations
s.upda­te(s2)
s.add(key)
s.copy()
s.disc­ard­(key)
s.pop()
s.clear()
 

String Methods

capita­lize()*
center­(width)
countr­(sub, start, end)
decode()
encode()
endswi­th(sub)
expand­tabs()
find(sub, start, end)
isalnum()*
isalpha()*
isdigit()*
islower()*
isspace()*
istitle()*
isupper()*
join()
ljust(­width)
lower()*
lstrip()
partit­ion­(sep)
replac­e(old, new)
rfind(sub, start, end)
rindex­(sub, start, end)
rjust(­width)
rparti­tio­n(sep)
rsplit­(sep)
rstrip()
split(sep)
splitl­ines()
starts­wit­h(sub)
strip()
swapca­se()*
title()*
transl­ate­(table)
upper()*
zfill(­width)
Methods marked * are locale dependant for 8-bit strings.

File Methods

close()
flush()
fileno()
isatty()
next()
read(size)
readli­ne(­size)
readli­nes­(size)
seek(o­ffset)
tell()
trunca­te(­size)
write(­string)
writel­ine­s(list)
 

Date Formating

%a
Abbrev­­iated weekday (Sun)
%A
Weekday (Sunday)
%b
Abbrev­­iated 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 (%)
¹ Sunday as start of week. All days in a new year preceding the first Sunday are considered to be in week 0.
² 0 is Sunday, 6 is Saturday.
³ Monday as start of week. All days in a new year preceding the first Monday are considered to be in week 0.
⁴ This is not a mistake. Range takes account of leap and double­­-leap seconds.

sys lib Variables and sys.args

argv
Command line args
builti­n_m­odu­le_­names
Linked C modules
byteorder
Native byte order
check_­int­erval
Signal check frequency
exec_p­refix
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
versio­n_info
Python version info
winver
Version number
sys.ar­gv[0]
foo.py
sys.ar­gv[1]
bar
sys.ar­gv[2]
-c
sys.ar­gv[3]
qux
sys.ar­gv[4]
--h
sys.argv for the command:
$ python foo.py bar -c qux --h

Download the Python Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/runnko_python.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.