You are on page 1of 2

Python Cheat Sheet

by Runnko via cheatography.com/24696/cs/6190/

List Methods os lib Variables (cont) Class Special Methods String Methods (cont)

append​(item) sep Path separator __new_​_(cls) endswi​th(sub)

count(​item) Registered OS names: "​​po​s​i​x", __init​__(​self, args) expand​tabs()

extend​(list) "​​nt​", "​​ma​c​", "​​os​2​", "​​ce​", "​​ja​v​a​", __del_​_(self) find(sub, start, end)
"​​ri​s​c​os​​"
index(​item) __repr​__(​self) isalnum()*

insert​(po​sition, item) __str_​_(self) isalpha()*


Operations on Dicts
pop(po​sition) __cmp_​_(self, other) isdigit()*
d.upda​te(d2)
remove​(item) __inde​x__​(self) islower()*
d.keys()
reverse() __hash​__(​self) isspace()*
d.values()
sort() __geta​ttr​__(​self, name) istitle()*
d.items()
__seta​ttr​__(​self, name, attr) isupper()*
List Slices and Indexes d.pop(​key​[,d​efa​ult])
__lt__​(self, other) join()
d.popi​tem()
len(a) 6 __le__​​(self, other) ljust(​width)
d.get(​key​[,d​efa​ult])
a[0] 0 __gt__​​(self, other) lower()*
d.setd​efa​ult​(ke​y[,​def​ault])
a[5] 5 __ge__​​(self, other) lstrip()
d.clear()
a[-1] 5 __eq__​​(self, other) partit​ion​(sep)
del d[key]
a[-2] 4 __ne__​​(self, other) replac​e(old, new)
d[key] = value
a[1:] [1,2,3​,4,5] __nonz​​er​o​_​_(​​self) rfind(sub, start, end)
a[:5] [0,1,2​,3,4] __dela​ttr​__(​self, name) rindex​(sub, start, end)
Datetime Methods
a[:-2] [0,1,2,3] __call​__(​self, args, kwargs) rjust(​width)
today()
a[1:3] [1,2] rparti​tio​n(sep)
now(ti​mez​one​info)
a[1:-1] [1,2,3,4] Opetaions on Sets
rsplit​(sep)
utcnow()
b=a[:] Shallow copy of a | union rstrip()
fromti​mes​tam​p(t​ime​stamp)
Indexes and Slices of a. & inters​ection split(sep)
utcfro​mti​mes​tam​p(t​ime​stamp)
a=[0,1​​,2​,​3​,4,5] -^ différ​enc​e/s​ymetric splitl​ines()
fromor​din​al(​ord​inal)
diff
starts​wit​h(sub)
os lib Variables combin​e(date, time)
< <= > >= inclusion relations
strip()
strpti​me(​date, format)
altsep Altern​​ative sep s.upda​te(s2 s.add(key)
swapca​se()*
curdir Current dir string )
Time Methods title()*
defpath Default search path s.copy() s.disc​ard​(key)
replace() transl​ate​(table)
devnull Path of null device s.pop() s.clear()
isofor​mat() upper()*
extsep Extension separator
__str__() String Methods zfill(​width)
linesep Line separator
strfti​me(​format) capita​lize()* Methods marked * are locale
name Name of OS
utcoff​set() dependant for 8-bit strings.
center​(width)
pardir Parent dir string
dst() countr​(sub, start, end)
pathsep Patch separator
tzname() decode()

encode()

By Runnko Published 26th November, 2015. Sponsored by Readability-Score.com


cheatography.com/runnko/ Last updated 26th November, 2015. Measure your website readability!
Page 1 of 2. https://readability-score.com
Python Cheat Sheet
by Runnko via cheatography.com/24696/cs/6190/

File Methods Date Formating (cont) sys lib Variables and sys.args
(cont)
close() %y Year without century (00 to

flush() 99) versio​n_info Python version info

%Y Year (2008) winver Version number


fileno()

isatty() %Z Time zone (GMT) sys.ar​gv[0] foo.py

%% A literal "​​%" character (%) sys.ar​gv[1] bar


next()
¹ Sunday as start of week. All days sys.ar​gv[2] -c
read(size)
in a new year preceding the first sys.ar​gv[3] qux
readli​ne(​size)
Sunday are considered to be in
sys.ar​gv[4] --h
readli​nes​(size) week 0.
seek(o​ffset) ² 0 is Sunday, 6 is Saturday. sys.argv for the command:
³ Monday as start of week. All days $ python foo.py bar -c qux --h
tell()
in a new year preceding the first
trunca​te(​size) Monday are considered to be in
write(​string) week 0.

writel​ine​s(list) ⁴ This is not a mistake. Range


takes account of leap and double​​-
leap seconds.
Date Formating

%a Abbrev​​iated weekday (Sun) sys lib Variables and sys.args


%A Weekday (Sunday)
argv Command line
%b Abbrev​​iated month name args
(Jan)
builti​n_m​odu​le_​ Linked C
%B Month name (January) names modules

%c Date and time byteorder Native byte


%d Day (leading zeros) (01 to order
31) check_​int​erval Signal check
%H 24 hour (leading zeros) (00 frequency
to 23) exec_p​refix Root directory

%I 12 hour (leading zeros) (01 executable Name of


to 12) executable

%j Day of year (001 to 366) exitfunc Exit function


%m Month (01 to 12) name

%M Minute (00 to 59) modules Loaded modules

%p AM or PM path Search path

%S Second (00 to 61⁴) platform Current platform

%U Week number¹ (00 to 53) stdin, stdout, File objects for


stderr I/O
%w Weekday² (0 to 6)

%W Week number³ (00 to 53)

%x Date

%X Time

By Runnko Published 26th November, 2015. Sponsored by Readability-Score.com


cheatography.com/runnko/ Last updated 26th November, 2015. Measure your website readability!
Page 2 of 2. https://readability-score.com

You might also like