Python Cheat Sheet

ADVERTISEMENT

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

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2