python sheet
Python sys Variables
变量 |
说明 |
argv |
Command line args |
builtin_module_names |
Linked C modules |
byteorder |
Native byte order |
exec_prefix |
Root directory |
executable |
Name of executable |
modules |
Loaded modules |
path |
Search path |
platform |
Current platform |
stdin, stdout, stderr |
File objects for I/O |
version_info |
Python version info |
version |
Version number |
Python sys.argv
sys.argv for the command:
$ python foo.py bar -c qux –h
变量 |
说明 |
sys.argv[0] |
foo.py |
sys.argv[1] |
bar |
sys.argv[2] |
-c |
sys.argv[3] |
qux |
sys.argv[4] |
--h |
Python os Variables
方法 |
说明 |
altsep |
Alternative sep |
curdir |
Current dir string |
defpath |
Default search path |
devnull |
Path of null device |
extsep |
Extension separator |
linesep |
Line separator |
name |
Name of OS |
pardir |
Parent dir string |
pathsep |
Patch separator |
sep |
Path separator |
Registered OS names: “posix”, “nt”, “mac”, “os2”, “ce”, “java”, “riscos”
Python Class Special Methods
方法 |
说明 |
__new__(cls) |
|
init(self, args) |
|
__del__(self) |
|
__str__(self) |
|
repr(self) |
|
lt(self, other) |
|
le(self, other) |
|
gt(self, other) |
|
ge(self, other) |
|
eq(self, other) |
|
ne(self, other) |
|
__cmp__(self, other) |
|
index(self) |
|
nonzero(self) |
|
hash(self) |
|
getattr(self, name) |
|
__getattribute__(self, name) |
|
setattr(self, name, attr) |
|
delattr(self, name) |
|
call(self, args, kwargs) |
|
Python List Methods
方法 |
说明 |
append(item) |
|
pop(position) |
|
count(item) |
|
remove(item) |
|
extend(list) |
|
reverse() |
|
index(item) |
|
sort() |
|
insert(position, item) |
|
Python String Methods
方法 |
说明 |
decode() |
|
encode() |
|
count(sub, start, end) |
|
index(sub, start, end) |
|
rindex(sub, start, end) |
|
find(sub, start, end) |
|
rfind(sub, start ,end) |
|
startswith(sub) |
|
endswith(sub) |
|
center(width) |
|
rjust(width) |
|
ljust(width) |
|
zfill(width) |
|
expandtabs() |
|
strip() |
|
lstrip() |
|
rstrip() |
|
split(sep) |
|
rsplit(sep) |
|
splitlines() |
|
partition(sep) |
|
rpartition(sep) |
|
join() |
|
swapcase() * |
|
capitalize() * |
|
title() * |
|
translate(table) |
|
lower() * |
|
upper() * |
|
replace(old, new) |
|
isdigit() * |
|
isalnum() * |
|
isspace() * |
|
istitle() * |
|
islower() * |
|
isupper() * |
|
isalpha() * |
|
Methods marked * are locale dependant for 8-bit strings.
Python File Methods
方法 |
说明 |
close() |
|
readlines(size) |
|
flush() |
|
seek(offset) |
|
fileno() |
|
tell() |
|
isatty() |
|
truncate(size) |
|
next() |
|
write(string) |
|
read(size) |
|
writelines(list) |
|
readline(size) |
|
Python Indexes and Slices
Indexes and Slices of a=[0,1,2,3,4,5]
操作 |
结果 |
len(a) |
6 |
a[0] |
0 |
a[5] |
5 |
a[-1] |
5 |
a[-2] |
4 |
a[1:] |
[1,2,3,4,5] |
a[:5] |
[0,1,2,3,4] |
a[:-2] |
[0,1,2,3] |
a[1:3] |
[1,2] |
a[1:-1] |
[1,2,3,4] |
b=a[:] |
Shallow copy of a |
Python Datetime Methods
操作 |
结果 |
today() |
|
fromordinal(ordinal) |
|
now(timezoneinfo) |
|
combine(date, time) |
|
utcnow() |
|
strptime(date, format) |
|
fromtimestamp(timestamp) |
|
utcfromtimestamp(timestamp) |
|
Python Time Methods
操作 |
结果 |
replace() |
|
utcoffset() |
|
isoformat() |
|
dst() |
|
str() |
|
tzname() |
|
strftime(format) |
|