\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{yunshu (xys)} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (yunshu (xys)) /Subject (Python Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{D40000} \definecolor{LightBackground}{HTML}{FCEFEF} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Python Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{yunshu (xys)} via \textcolor{DarkBackground}{\uline{cheatography.com/130138/cs/25798/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}yunshu (xys) \\ \uline{cheatography.com/xys} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 20th December, 2020.\\ Updated 20th December, 2020.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basics}} \tn % Row 0 \SetRowColor{LightBackground} abs() & absolute value \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} hash() & hash value \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} set() & creat a Set object \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} all() & True if all elements are true \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} any() & True if any element is true \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} min() & return minimum \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} max() & return maximum \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} divmod(a,b) & return (a//b,a\%b) \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} hex() & hexadecimal \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} oct() & octal \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} bin() & binary \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} dir() & return all attributes of obj \tn % Row Count 15 (+ 2) % Row 12 \SetRowColor{LightBackground} sorted({\emph{iter}}) & return a new sorted list from iter \tn % Row Count 17 (+ 2) % Row 13 \SetRowColor{white} open(path,mode) & open a file \tn % Row Count 19 (+ 2) % Row 14 \SetRowColor{LightBackground} int() & creat an Int object \tn % Row Count 20 (+ 1) % Row 15 \SetRowColor{white} str() & return the string form \tn % Row Count 21 (+ 1) % Row 16 \SetRowColor{LightBackground} float() & creat a float obj \tn % Row Count 22 (+ 1) % Row 17 \SetRowColor{white} list() & creat a List obj \tn % Row Count 23 (+ 1) % Row 18 \SetRowColor{LightBackground} \seqsplit{isinstance(obj},class) & check if obj belongs to class \tn % Row Count 25 (+ 2) % Row 19 \SetRowColor{white} ord(c) & return ASCII of c \tn % Row Count 26 (+ 1) % Row 20 \SetRowColor{LightBackground} chr(n) & return char of ASCII \tn % Row Count 27 (+ 1) % Row 21 \SetRowColor{white} sum(iter) & return sum of iter \tn % Row Count 28 (+ 1) % Row 22 \SetRowColor{LightBackground} filter(pred,iter) & return list of elements meeting pred \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basics (cont)}} \tn % Row 23 \SetRowColor{LightBackground} pow(a,b) & return a\textasciicircum{}b \tn % Row Count 1 (+ 1) % Row 24 \SetRowColor{white} callable(obj) & True if obj is callable \tn % Row Count 2 (+ 1) % Row 25 \SetRowColor{LightBackground} type() & return type of obj \tn % Row Count 3 (+ 1) % Row 26 \SetRowColor{white} zip() & zip('ab','12') -\textgreater{} a1,b2 \tn % Row Count 5 (+ 2) % Row 27 \SetRowColor{LightBackground} map(f,xs) & return ys = f(xs) \tn % Row Count 6 (+ 1) % Row 28 \SetRowColor{white} round() & rounded number \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{thread}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import threading} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} t = \seqsplit{threading.Thread(target=fun}, args = iter,name=thread name) & creat a thread \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} t.start() & start thread t \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} t.join() & join thread t, other threads waiting until t finishes \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} lock = threading.Lock() & create a lock \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} lock.acquire() & current thread acquires the lock \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} lock.release() & current thead release lock \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{str-library}} \tn % Row 0 \SetRowColor{LightBackground} s1+s2 & string concatenation \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} s*5 & repeating 5 times of s \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} {[}0{]} or {[}:{]} & subscription and slice \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} in/not in & member test \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} r/R & no escape: r'\textbackslash{}n' = '\textbackslash{}n' (no new line) \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} \% & string formatting (\%d \textless{}=\textgreater{} integer) ) \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} s.capitalize() & capitalize the first char in s \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} s.count(x,beg=0,end=len(s))) & count the number of occurence of x in s \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} s.endswith(suffix,beg=0,end=len(s)) & check if s ends with suffix (within the given area) \tn % Row Count 17 (+ 3) % Row 9 \SetRowColor{white} \seqsplit{s.startswith(prefix},beg=0,end=len(s)) & check if s starts with x \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} \seqsplit{s.expandtabs(tabsize=8)} & expand the "tab" in s to space \tn % Row Count 21 (+ 2) % Row 11 \SetRowColor{white} s.find(x,beg=0,end=len(s)) & return start index of x in s if x is in s, else -1 \tn % Row Count 24 (+ 3) % Row 12 \SetRowColor{LightBackground} s.index(x,beg=0,end=len(s)) & similar to find, but raises an exception if x is not in s \tn % Row Count 27 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{s.rindex()} \tn % Row Count 28 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{s.rfind()} \tn % Row Count 29 (+ 1) % Row 15 \SetRowColor{white} s.isalnum() & True if every char(\textgreater{}=1) in s is number or letter \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{str-library (cont)}} \tn % Row 16 \SetRowColor{LightBackground} s.isalpha() & True if every char(\textgreater{}=1) in s is letter \tn % Row Count 2 (+ 2) % Row 17 \SetRowColor{white} s.isdigit() & True if every char(\textgreater{}=1) in s is number \tn % Row Count 4 (+ 2) % Row 18 \SetRowColor{LightBackground} s.isnumeric() & True if all characters in the string are numeric(\textgreater{}=1) \tn % Row Count 7 (+ 3) % Row 19 \SetRowColor{white} s.isdecimal() & Return True if the string is a decimal string(\textgreater{}=1), False otherwise. \tn % Row Count 11 (+ 4) % Row 20 \SetRowColor{LightBackground} s.isspace() & True if s only contains space \tn % Row Count 13 (+ 2) % Row 21 \SetRowColor{white} s.join() & Concatenate any number of strings using s as delimiter \tn % Row Count 16 (+ 3) % Row 22 \SetRowColor{LightBackground} s.upper() & all to uppercase \tn % Row Count 17 (+ 1) % Row 23 \SetRowColor{white} s.isupper() & True if all cased chars are supercase(\textgreater{}=1) \tn % Row Count 20 (+ 3) % Row 24 \SetRowColor{LightBackground} s.lower() & all to lowercase \tn % Row Count 21 (+ 1) % Row 25 \SetRowColor{white} s.islower() & True if all cased chars are lowercase(\textgreater{}=1) \tn % Row Count 24 (+ 3) % Row 26 \SetRowColor{LightBackground} s.lstrip() & return a new string leading whitespace removed \tn % Row Count 27 (+ 3) % Row 27 \SetRowColor{white} s.strip() & Return a copy of the string with leading and trailing whitespace removed \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{str-library (cont)}} \tn % Row 28 \SetRowColor{LightBackground} s.rstrip() & Return a copy of the string with trailing whitespace removed. \tn % Row Count 4 (+ 4) % Row 29 \SetRowColor{white} s.split(del,maxsplit = s.count(del)) & Return a list of the words in the string, using del as the delimiter string \tn % Row Count 8 (+ 4) % Row 30 \SetRowColor{LightBackground} \seqsplit{s.splitlines(keepends)} & Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true. \tn % Row Count 16 (+ 8) % Row 31 \SetRowColor{white} s.swapcase() & lower \textless{}-\textgreater{} upper \tn % Row Count 17 (+ 1) % Row 32 \SetRowColor{LightBackground} s.titile() & titilization: all words are capitalized \tn % Row Count 19 (+ 2) % Row 33 \SetRowColor{white} s.replace(old,new,{\emph{max}}) & Return a copy with all occurrences of substring old replaced by new \tn % Row Count 23 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{list}} \tn % Row 0 \SetRowColor{LightBackground} {[}1,2,3{]}+{[}4,5,6{]} & {[}1,2,3,4,5,6{]} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} arr = {[}0{]}*10 & Array arr = new Array{[}10{]} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} l.append(obj) & append obj at end of l \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} l.count(obj) & count occurence number of obj in l \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} l.extend({\emph{iter}}) & Extend list by appending elements from the iterable \tn % Row Count 9 (+ 3) % Row 5 \SetRowColor{white} l.index(obj,beg=0,end=len(l)) & Return first index of value. Raises ValueError if the value is not present \tn % Row Count 13 (+ 4) % Row 6 \SetRowColor{LightBackground} l.remove(obj) & Remove first occurrence of value. Raises ValueError if the value is not present \tn % Row Count 17 (+ 4) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{l.sort(cmp=None,key=None,reverse=False)} \tn % Row Count 18 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{tuple}} \tn % Row 0 \SetRowColor{LightBackground} (1,2)+(3,4) & (1,2,3,4) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} (0)*10 & (0,0,0,0,0,0,0,0,0,0) \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{dict (hashtable)}} \tn % Row 0 \SetRowColor{LightBackground} d = \{'age':20\} & create a dict \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} d{[}'age'{]} = 30 & add/update value \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} d.pop(key) & deleting key and value \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} d.clear() & create a dict \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} d.get(key,default=None) & get value by key, or default if key not exists \tn % Row Count 6 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{d.has\_key(key)} & True if d has key \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} d.items() & a list of (key,value) of d \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} d.update(d2) & updating (k,v) of d2 to d1 \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} d.pop(key) & delete and return the value pointed by the key \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{white} d.popitem() & delete and return a pair of (k,v) randomly \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{dict features: \newline 1. fast for searching and inserting, which won't be affected by the number of keys \newline 2. occupy a lot of memory} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.9908 cm} x{2.9862 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{set}} \tn % Row 0 \SetRowColor{LightBackground} s = set({[}1,2,3{]}) & creat a set \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} s.add(4) & adding element \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} s.remove(4) & deleting element \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} s1 \& s2 & intersection of sets \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} s1 | s2 & union of sets \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} s.clear() & clear the set \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} s.pop() & remove one element randomly \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\seqsplit{s1.symmetric\_difference(s2)}} \tn % Row Count 10 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.83689 cm} x{2.14011 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{copy}} \tn % Row 0 \SetRowColor{LightBackground} a = li & a: new pointer to li \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} a = li{[}:{]} & first level copy \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} a = list(li) & first level copy \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} a = copy.copy(li) & first level copy \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} a = copy.deepcopy(li) & recursive copy \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import copy \newline li = {[}1,2,3,{[}4,5{]}{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{list generation expression}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{[}a+b for a in list1 for b in list2{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{@property}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Student(object): \newline @property \newline def {\bf{score}}(self): return 100 \newline \newline @{\bf{score}}.setter \newline def {\bf{score}}(self,value): pass} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{the three names (score) should be consistent} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{regular expression}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import re} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} re.match(pattern,string,{\emph{flags}}) & Try to apply the pattern at the start of the string, returning a Match object, or None if no match was found. \tn % Row Count 6 (+ 5) % Row 2 \SetRowColor{LightBackground} \seqsplit{re.search(pattern},string,{\emph{flag}}) & Scan through string looking for a match to the pattern, returning a Match object, or None if no match was found. \tn % Row Count 12 (+ 6) % Row 3 \SetRowColor{white} \seqsplit{matchObject.span()} & return (a,b) where a is the start inex and b is the end index of the matching \tn % Row Count 16 (+ 4) % Row 4 \SetRowColor{LightBackground} \seqsplit{re.compile(pattern},{\emph{flag}}) & Compile a regular expression pattern, returning a Pattern object, which can be used in re.match/re.search \tn % Row Count 21 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.39356 cm} x{3.58344 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{parameters}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{func(*args)} & accepting any parameters \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} func(**kw) & accepting only key word parameters \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{closure}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def \seqsplit{create\_myFunc\_at\_runtime(*runtime\_para):} \newline def myFunc(x): \newline (return x + runtime\_para) \newline pass \newline return myFunc} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Build A Class: Test}} \tn % Row 0 \SetRowColor{LightBackground} \_\_slots\_\_ = ('name','age') & this class have only 2 attributes now: name \& age \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \_\_eq\_\_(self,obj) & override "==" operator \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \_\_ne\_\_(self,obj) & != \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \_\_le\_\_(self,o) & \textless{}= \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} \_\_ge\_\_(self,o) & \textgreater{}= \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} \_\_lt\_\_(self,o) & \textless{} \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} \_\_gt\_\_(self,o) & \textgreater{} \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} \_\_str\_\_(self) & override str() \tn % Row Count 12 (+ 1) % Row 8 \SetRowColor{LightBackground} \_\_repr\_\_(self) & repr() \tn % Row Count 13 (+ 1) % Row 9 \SetRowColor{white} \_\_len\_\_(self) & len() \tn % Row Count 14 (+ 1) % Row 10 \SetRowColor{LightBackground} \seqsplit{\_\_getitem\_\_(self},n) & subscritable and slice-able \tn % Row Count 16 (+ 2) % Row 11 \SetRowColor{white} \seqsplit{\_\_setitem\_\_(self},key,value) & supporting item assignment \tn % Row Count 19 (+ 3) % Row 12 \SetRowColor{LightBackground} \_\_call\_\_(self) & -\textgreater{} callable \tn % Row Count 20 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{inheritance}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{overriding \_\_init\_\_: \newline super(child class,self).\_\_init\_\_(*para)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.73735 cm} x{2.23965 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{datetime}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{from datetime import datetime} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} dt = datetime(2015,4,19,12,20) & 2015-04-19 12:20:00 \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} datetime.now() & current date and time \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{datetime.strptime('2015-6-1} 18:19:59','\%Y-\%m-\%d \%H:\%M:\%S') & str -\textgreater{} datetime \tn % Row Count 8 (+ 3) % Row 4 \SetRowColor{LightBackground} dt.strftime('\%a,\%b \%d \%H \%M') & datetime -\textgreater{} str \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} from datetime import timedelta & datetime addition and subtraction \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{now + timedelta(hours = 10)} \tn % Row Count 13 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{now + timedelta(days=1)} \tn % Row Count 14 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{JSON}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import json} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{js=json.dumps(py)} & convet from python obj to json \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} py = json.loads(js) & convert from json to python obj \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}