zl程序教程

您现在的位置是:首页 >  后端

当前栏目

CheatSheet:Python快速参考

Python 快速 参考
2023-09-14 09:07:13 时间

Python sys Variables

变量说明
argvCommand line args
builti­n_m­odu­le_­namesLinked C modules
byteorderNative byte order
exec_p­refixRoot directory
executableName of executable
modulesLoaded modules
pathSearch path
platformCurrent platform
stdin, stdout, stderrFile objects for I/O
versio­n_infoPython version info
versionVersion number

Python sys.argv

sys.argv for the command:

$ python foo.py bar -c qux --h
变量
sys.ar­gv[0]foo.py
sys.ar­gv[1]bar
sys.ar­gv[2]-c
sys.ar­gv[3]qux
sys.ar­gv[4]–h

Python os Variables

方法说明
altsepAltern­ative sep
curdirCurrent dir string
defpathDefault search path
devnullPath of null device
extsepExtension separator
linesepLine separator
nameName of OS
pardirParent dir string
pathsepPatch separator
sepPath separator

Registered OS names: “­pos­ix”, “­nt”, “­mac­”, “­os2­”, “­ce”, “­jav­a”, “­ris­cos­”

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)
inde­x­(self)
nonz­ero­(­self)
hash­(­self)
geta­ttr­(­self, name)
_geta­ttr­ibu­te­_(self, name)
seta­ttr­(­self, name, attr)
dela­ttr­(­self, name)
call­(­self, args, kwargs)

Python List Methods

方法说明
append­(item)
pop(po­sition)
count(­item)
remove­(item)
extend­(list)
reverse()
index(­item)
sort()
insert­(po­sition, 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)
starts­wit­h(sub)
endswi­th(sub)
center­(width)
rjust(­width)
ljust(­width)
zfill(­width)
expand­tabs()
strip()
lstrip()
rstrip()
split(sep)
rsplit­(sep)
splitl­ines()
partit­ion­(sep)
rparti­tio­n(sep)
join()
swapcase() *
capita­lize() *
title() *
transl­ate­(table)
lower() *
upper() *
replac­e(old, new)
isdigit() *
isalnum() *
isspace() *
istitle() *
islower() *
isupper() *
isalpha() *

Methods marked * are locale dependant for 8-bit strings.

Python File Methods

方法说明
close()
readli­nes­(size)
flush()
seek(o­ffset)
fileno()
tell()
isatty()
trunca­te(­size)
next()
write(­string)
read(size)
writel­ine­s(list)
readli­ne(­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()
fromor­din­al(­ord­inal)
now(ti­mez­one­info)
combin­e(date, time)
utcnow()
strpti­me(­date, format)
fromti­mes­tam­p(t­ime­stamp)
utcfro­mti­mes­tam­p(t­ime­stamp)

Python Time Methods

操作结果
replace()
utcoff­set()
isofor­mat()
dst()
str()
tzname()
strfti­me(­format)

Python Date Formatting

字符说明
%aAbbrev­iated weekday (Sun)
%AWeekday (Sunday)
%bAbbrev­iated month name (Jan)
%BMonth name (January)
%cDate and time
%dDay (leading zeros) (01 to 31)
%H24 hour (leading zeros) (00 to 23)
%I12 hour (leading zeros) (01 to 12)
%jDay of year (001 to 366)
%mMonth (01 to 12)
%MMinute (00 to 59)
%pAM or PM
%SSecond (00 to 61⁴)
%UWeek number¹ (00 to 53)
%wWeekday² (0 to 6)
%WWeek number³ (00 to 53)
%xDate
%XTime
%yYear without century (00 to 99)
%YYear (2008)
%ZTime zone (GMT)
%%A literal “­%” character (%)

¹ Sunday as start of week. All days in a new year preceding the first Sunday are considered to be in week 0.
² 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.
⁴ This is not a mistake. Range takes account of leap and double­-leap seconds.

pdf下载

参考

  1. 原文 - Python Cheat Sheet by DaveChild
  2. Python 速查表