Enabling Your Growth Through
www.GlowTouch.com Application Development & Support Services
Python Cheat Sheet
Python Class Special Methods
Python Date Formatting
__new__(cls) __lt__(self, other)
%a Abbreviated weekday (Sun)
__init__(self, args) __le__(self, other)
%A Weekday (Sunday)
__del__(self) __gt__(self, other)
%b Abbreviated month name (Jan)
__repr__(self) __ge__(self, other)
%B Month name (January)
__str__(self) __eq__(self, other)
%c Date and time
__cmp__(self, other) __ne__(self, other)
%d Day (leading zeros) (01 to 31)
__index__(self) __nonzero__(self)
%H 24 hour (leading zeros) (00 to 23)
__hash__(self)
%I 12 hour (leading zeros) (01 to 12)
__getattr__(self, name)
%j Day of year (001 to 366)
__getattribute__(self, name)
%m Month (01 to 12)
__setattr__(self, name, attr)
%M Minute (00 to 59)
__delattr__(self, name)
%p AM or PM
__call__(self, args, kwargs)
%S Second (00 to 61⁴)
%U Week number¹ (00 to 53)
Python Indexes and Slices
%w Weekday² (0 to 6)
len(a) 6
%W Week number³ (00 to 53)
a[0] 0
%x Date
a[5] 5
%X Time
a[-1] 5
%y Year without century (00 to 99)
a[-2] 4
%Y Year (2008)
a[1:] [1,2,3,4,5]
%Z Time zone (GMT)
a[:5] [0,1,2,3,4]
%% A literal "%" character (%)
a[:-2] [0,1,2,3]
¹ Sunday as start of week. All days in a new
a[1:3] [1,2]
year preceding the first Sunday are
a[1:-1] [1,2,3,4]
considered to be in week 0.
b=a[:] Shallow copy of a
² 0 is Sunday, 6 is Saturday.
Indexes and Slices of a=[0,1,2,3,4,5]
³ Monday as start of week. All days in a new
year preceding the first Monday are
Python Datetime Methods
considered to be in week 0.
today() fromordinal(ordinal)
⁴ This is not a mistake. Range takes account
now(timezoneinfo) combine(date, time)
of leap and double-leap seconds.
utcnow() strptime(date, format)
fromtimestamp(timestamp)
utcfromtimestamp(timestamp)
Python Time Methods
replace() utcoffset()
isoformat() dst()
__str__() tzname()
strftime(format)