Python Cheat Sheet

ADVERTISEMENT

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

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go