Python Cheatsheet

· 481 words · 3 minute read

Python sys Variables đź”—

variablemeaning
argvCommand line args
builti­n_m­odu­le_­namesLinked C modules
byteorderNative byte order
check_­int­ervalSignal check frequency
exec_p­refixRoot directory
executableName of executable
exitfuncExit function name
modulesLoaded modules
pathSearch path
platformCurrent platform
stdin, stdout, stderrFile objects for I/O
versio­n_infoPython version info
winverVersion number

Python sys.argv đź”—

sys.argv for the command:

python foo.py bar -c qux --h
sys.argvpart of the command
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 đź”—

OS Variablemeaning
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

Python Class Special Methods đź”—

  • __new_­_(cls)
  • __lt__­(self, other)
  • __init­__(­self, args)
  • __le__­(self, other)
  • __del_­_(self)
  • __gt__­(self, other)
  • __repr­__(­self)
  • __ge__­(self, other)
  • __str_­_(self)
  • __eq__­(self, other)
  • __cmp_­_(self, other)
  • __ne__­(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 đź”—

  • capita­lize() : for 8-bit strings
  • lstrip()
  • center­(width)
  • partit­ion­(sep)
  • count(sub, start, end)
  • replac­e(old, new)
  • decode()
  • rfind(sub, start ,end)
  • encode()
  • rindex­(sub, start, end)
  • endswi­th(sub)
  • rjust(­width)
  • expand­tabs()
  • rparti­tio­n(sep)
  • find(sub, start, end)
  • rsplit­(sep)
  • index(sub, start, end)
  • rstrip()
  • isalnum() : for 8-bit strings
  • split(sep)
  • isalpha() : for 8-bit strings
  • splitl­ines()
  • isdigit() : for 8-bit strings
  • starts­wit­h(sub)
  • islower() : for 8-bit strings
  • strip()
  • isspace() : for 8-bit strings
  • swapcase() : for 8-bit strings
  • istitle() : for 8-bit strings
  • title() : for 8-bit strings
  • isupper() : for 8-bit strings
  • transl­ate­(table)
  • join()
  • upper() : for 8-bit strings
  • ljust(­width)
  • zfill(­width)
  • lower() : 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].

slicesmeaning
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 đź”—

FormatMeaning
%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 (%)

That’s it. Hope it helps.

Share:
waffarx cash back