Python Cheat Sheet
by Dave Child (DaveChild) via [Link]/1/cs/19/
Python sys Variables Python Class Special Methods Python String Methods (cont)
argv Command line args __new__(cls) __lt__(self, other) istitle() * title() *
builtin_module_‐ Linked C modules __init__(self, args) __le__(self, other) isupper() * translate(table)
names __del__(self) __gt__(self, other) join() upper() *
byteorder Native byte order __repr__(self) __ge__(self, other) ljust(width) zfill(width)
check_interval Signal check __str__(self) __eq__(self, other) lower() *
frequency
__cmp__(self, other) __ne__(self, other) Methods marked * are locale dependant for
exec_prefix Root directory
__index__(self) __nonzero__(self) 8-bit strings.
executable Name of executable
__hash__(self)
exitfunc Exit function name Python File Methods
__getattr__(self, name)
modules Loaded modules close() readlines(size)
__getattribute__(self, name)
path Search path flush() seek(offset)
__setattr__(self, name, attr)
platform Current platform fileno() tell()
__delattr__(self, name)
stdin, stdout, stderr File objects for I/O isatty() truncate(size)
__call__(self, args, kwargs)
version_info Python version info next() write(string)
winver Version number Python List Methods read(size) writelines(list)
append(item) pop(position) readline(size)
Python [Link]
count(item) remove(item)
[Link]gv[0] [Link] Python Indexes and Slices
extend(list) reverse()
[Link]gv[1] bar len(a) 6
index(item) sort()
[Link]gv[2] -c a[0] 0
insert(position, item)
[Link]gv[3] qux a[5] 5
[Link]gv[4] --h Python String Methods a[-1] 5
[Link] for the command: capitalize() * lstrip() a[-2] 4
$ python [Link] bar -c qux --h center(width) partition(sep) a[1:] [1,2,3,4,5]
count(sub, start, replace(old, new) a[:5] [0,1,2,3,4]
Python os Variables
end) a[:-2] [0,1,2,3]
altsep Alternative sep
decode() rfind(sub, start ,end) a[1:3] [1,2]
curdir Current dir string
encode() rindex(sub, start, a[1:-1] [1,2,3,4]
defpath Default search path end)
a[::-1] [5,4,3,2,1]
devnull Path of null device endswith(sub) rjust(width)
a[::-2] [5,3,1]
extsep Extension separator expandtabs() rpartition(sep)
b=a[:] Shallow copy of a
linesep Line separator find(sub, start, end) rsplit(sep)
Indexes and Slices of a=[0,1,2,3,4,5]
name Name of OS index(sub, start, rstrip()
pardir Parent dir string end) Python Datetime Methods
pathsep Patch separator isalnum() * split(sep)
today() fromordinal(ordinal)
sep Path separator isalpha() * splitlines()
now(timezoneinfo) combine(date, time)
Registered OS names: "posix", "nt", isdigit() * startswith(sub)
utcnow() strptime(date, format)
"mac", "os2", "ce", "java", "riscos" islower() * strip()
fromtimestamp(timestamp)
isspace() * swapcase() *
utcfromtimestamp(timestamp)
By Dave Child (DaveChild) Published 19th October, 2011. Sponsored by [Link]
[Link]/davechild/ Last updated 21st November, 2022. Everyone has a novel in them. Finish
[Link] Page 1 of 2. Yours!
[Link]
Python Cheat Sheet
by Dave Child (DaveChild) via [Link]/1/cs/19/
Python Time Methods
replace() utcoffset()
isoformat() dst()
__str__() tzname()
strftime(format)
Python Date Formatting
%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 (%)
¹ 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.
By Dave Child (DaveChild) Published 19th October, 2011. Sponsored by [Link]
[Link]/davechild/ Last updated 21st November, 2022. Everyone has a novel in them. Finish
[Link] Page 2 of 2. Yours!
[Link]