davechild_python.pdf

(43 KB) Pobierz
Python Cheat Sheet
by
Dave Child (DaveChild)
via
cheatography.com/1/cs/19/
Python sys Variables
argv
builti​n_m​odu​le_​names
byteorder
check_​int​erval
exec_p​refix
executable
exitfunc
modules
path
platform
stdin, stdout, stderr
versio​n_info
winver
Python sys.argv
sys.ar​gv[0]
sys.ar​gv[1]
sys.ar​gv[2]
sys.ar​gv[3]
sys.ar​gv[4]
sys.argv for the command:
$ python foo.py bar -c qux --h
Python os Variables
altsep
curdir
defpath
devnull
extsep
linesep
name
pardir
pathsep
sep
Altern​ative sep
Current dir string
Default search path
Path of null device
Extension separator
Line separator
Name of OS
Parent dir string
Patch separator
Path separator
foo.py
bar
-c
qux
--h
Python String Methods
capita​lize() *
center​(width)
count(sub, start, end)
decode()
encode()
endswi​th(sub)
expand​tabs()
find(sub, start, end)
index(sub, start, end)
isalnum() *
isalpha() *
isdigit() *
islower() *
isspace() *
lstrip()
partit​ion​(sep)
replac​e(old, new)
rfind(sub, start ,end)
rindex​(sub, start, end)
rjust(​width)
rparti​tio​n(sep)
rsplit​(sep)
rstrip()
split(sep)
splitl​ines()
starts​wit​h(sub)
strip()
swapcase() *
Command line args
Linked C modules
Native byte order
Signal check
frequency
Root directory
Name of executable
Exit function name
Loaded modules
Search path
Current platform
File objects for I/O
Python version info
Version number
Python List Methods
append​(item)
count(​item)
extend​(list)
index(​item)
insert​(po​sition, item)
pop(po​sition)
remove​(item)
reverse()
sort()
Python Class Special Methods
__new_​_(cls)
__init​__(​self, args)
__del_​_(self)
__repr​__(​self)
__str_​_(self)
__cmp_​_(self, other)
__inde​x__​(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)
__lt__​(self, other)
__le__​(self, other)
__gt__​(self, other)
__ge__​(self, other)
__eq__​(self, other)
__ne__​(self, other)
__nonz​ero​__(​self)
Python String Methods (cont)
istitle() *
isupper() *
join()
ljust(​width)
lower() *
Methods marked * are locale dependant for 8-
bit strings.
Python File Methods
close()
flush()
fileno()
isatty()
next()
read(size)
readli​ne(​size)
Python Indexes and Slices
len(a)
a[0]
a[5]
a[-1]
a[-2]
a[1:]
a[:5]
a[:-2]
a[1:3]
a[1:-1]
b=a[:]
6
0
5
5
4
[1,2,3​,4,5]
[0,1,2​,3,4]
[0,1,2,3]
[1,2]
[1,2,3,4]
Shallow copy of a
readli​nes​(size)
seek(o​ffset)
tell()
trunca​te(​size)
write(​string)
writel​ine​s(list)
title() *
transl​ate​(table)
upper() *
zfill(​width)
Indexes and Slices of a=[0,1​,2,​3,4,5]
Python Datetime Methods
today()
now(ti​mez​one​info)
utcnow()
fromor​din​al(​ord​inal)
combin​e(date, time)
strpti​me(​date, format)
Registered OS names: "​pos​ix", "​nt",
"​mac​", "​os2​", "​ce", "​jav​a", "​ris​cos​"
fromti​mes​tam​p(t​ime​stamp)
utcfro​mti​mes​tam​p(t​ime​stamp)
By
Dave Child
(DaveChild)
cheatography.com/davechild/
www.getpostcookie.com
Published 19th October, 2011.
Last updated 12th May, 2016.
Page 1 of 2.
Sponsored by
ApolloPad.com
Everyone has a novel in them. Finish Yours!
https://apollopad.com
Python Cheat Sheet
by
Dave Child (DaveChild)
via
cheatography.com/1/cs/19/
Python Time Methods
replace()
isofor​mat()
__str__()
strfti​me(​format)
Python Date Formatting
%a
%A
%b
%B
%c
%d
%H
%I
%j
%m
%M
%p
%S
%U
%w
%W
%x
%X
%y
%Y
%Z
%%
Abbrev​iated weekday (Sun)
Weekday (Sunday)
Abbrev​iated month name (Jan)
Month name (January)
Date and time
Day (leading zeros) (01 to 31)
24 hour (leading zeros) (00 to 23)
12 hour (leading zeros) (01 to 12)
Day of year (001 to 366)
Month (01 to 12)
Minute (00 to 59)
AM or PM
Second (00 to 61⁴)
Week number¹ (00 to 53)
Weekday² (0 to 6)
Week number³ (00 to 53)
Date
Time
Year without century (00 to 99)
Year (2008)
Time zone (GMT)
A literal "​%" character (%)
utcoff​set()
dst()
tzname()
¹ 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.
By
Dave Child
(DaveChild)
cheatography.com/davechild/
www.getpostcookie.com
Published 19th October, 2011.
Last updated 12th May, 2016.
Page 2 of 2.
Sponsored by
ApolloPad.com
Everyone has a novel in them. Finish Yours!
https://apollopad.com
Zgłoś jeśli naruszono regulamin