Python Cheat Sheet v1
Python Cheat Sheet v1
Python Cheat Sheet v1
winver Command line args Native byte order Signal check frequency Root directory Name of executable Exit function name Loaded modules Search path Current platform File objects for I/O Python version info Version number
Datetime Methods lstrip() partition(sep) replace(old, new) rfind(sub, start ,end) rindex(sub, start, end) rjust(width) rpartition(sep) rstrip() split(sep) splitlines() startswith(sub) strip() swapcase() * title() * translate(table) upper() * zfill(width) Date Formatting (strftime and strptime) %a Abbreviated weekday (Sun) %A Weekday (Sunday) %b Abbreviated month name (Jan) %B Month name (January) %c Date and time Time Methods replace() isoformat() __str__() strftime(format) utcoffset() dst() tzname() today() now(timezoneinfo) utcnow() fromordinal(ordinal) combine(date, time) strptime(date, format)
fromtimestamp(timestamp) utcfromtimestamp(timestamp)
find(sub, start, end) rsplit(sep) isalnum() * isalpha() * isdigit() * islower() * isspace() * istitle() * isupper() * join() ljust(width) lower() *
Methods marked * are locale dependant for 8-bit strings.
sys.argv for $ python foo.py bar -c qux --h sys.argv[0] sys.argv[1] sys.argv[2] sys.argv[3] sys.argv[4] foo.py bar -c qux --h
Note
%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)
os Variables altsep curdir defpath devnull extsep linesep name pardir pathsep sep Alternative sep Current dir string Default search path Path of null device Extension separator Line separator Name of OS Parent dir string Patch separator Path separator
Registered OS names: posix, nt, mac, os2, ce, java, riscos
List Methods append(item) count(item) extend(list) index(item) insert(position, item) File Methods close() flush() fileno() isatty() next() read(size) readline(size) readlines(size) seek(offset) tell() truncate(size) write(string) writelines(list) pop(position) remove(item) reverse() sort()
%m Month (01 to 12) %M Minute (00 to 59) %p AM or PM %S Second (00 to 61 4) %U Week number %w Weekday %x Date %X Time %y Year without century (00 to 99) %Y Year (2008) %Z Time zone (GMT) %% A literal "%" character (%) 1. Sunday as start of week. All days in a new year preceding the first Sunday are considered to be in week 0.
2 1
(0 to 6)
3
%W Week number
Note
Class Special Methods __new__(cls) __init__(self, args) __del__(self) __repr__(self) __str__(self) __index__(self) __hash__(self) __getattr__(self, name) __getattribute__(self, name) __setattr__(self, name, attr) __delattr__(self, name) __call__(self, args, kwargs) __lt__(self, other) __le__(self, other) __gt__(self, other) __ge__(self, other) __eq__(self, other) __nonzero__(self)
Indexes and Slices (of a=[0,1,2,3,4,5]) len(a) a[0] a[5] a[-1] a[-2] a[1:] a[:5] a[:-2] a[1:3] a[1:-1] b=a[:] 6 0 5 5 4 [1,2,3,4,5] [0,1,2,3,4] [0,1,2,3] [1,2] [1,2,3,4] Shallow copy of a
2. 3.
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.
4.
This is not a mistake. Range takes account of leap and double-leap seconds.