Python Cheatsheet
·
481 words
·
3 minute read Python sys Variables đź”—
variable | meaning |
---|
argv | Command line args |
builtiÂn_mÂoduÂle_Ânames | Linked C modules |
byteorder | Native byte order |
check_ÂintÂerval | Signal check frequency |
exec_pÂrefix | Root directory |
executable | Name of executable |
exitfunc | Exit function name |
modules | Loaded modules |
path | Search path |
platform | Current platform |
stdin, stdout, stderr | File objects for I/O |
versioÂn_info | Python version info |
winver | Version number |
Python sys.argv đź”—
sys.argv for the command:
python foo.py bar -c qux --h
sys.argv | part 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 Variable | meaning |
---|
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 |
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]
.
slices | meaning |
---|
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)
Format | Meaning |
---|
%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 (%) |
That’s it. Hope it helps.