欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CheatSheet:Python快速参考

程序员文章站 2022-05-26 17:52:09
...

Python sys Variables

变量 说明
argv Command line args
builti­n_m­odu­le_­names Linked C modules
byteorder Native byte order
exec_p­refix Root directory
executable Name of executable
modules Loaded modules
path Search path
platform Current platform
stdin, stdout, stderr File objects for I/O
versio­n_info Python version info
version Version 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

方法 说明
altsep Altern­ative 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: “­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

字符 说明
%a Abbrev­iated weekday (Sun)
%A Weekday (Sunday)
%b Abbrev­iated month name (Jan)
%B Month name (January)
%c Date and time
%d Day (leading zeros) (01 to 31)
%H 24 hour (leading zeros) (00 to 23)
%I 12 hour (leading zeros) (01 to 12)
%j Day of year (001 to 366)
%m Month (01 to 12)
%M Minute (00 to 59)
%p AM or PM
%S Second (00 to 61⁴)
%U Week number¹ (00 to 53)
%w Weekday² (0 to 6)
%W Week number³ (00 to 53)
%x Date
%X Time
%y Year without century (00 to 99)
%Y Year (2008)
%Z Time 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 速查表