\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{atinfosec (KilGrave)} \pdfinfo{ /Title (python-functions.pdf) /Creator (Cheatography) /Author (atinfosec (KilGrave)) /Subject (Python Functions 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}{253285} \definecolor{LightBackground}{HTML}{F1F2F7} \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 Functions Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{atinfosec (KilGrave)} via \textcolor{DarkBackground}{\uline{cheatography.com/69386/cs/17507/}}} \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}atinfosec (KilGrave) \\ \uline{cheatography.com/kilgrave} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 18th October, 2018.\\ Updated 19th October, 2018.\\ 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*}{2} \begin{tabularx}{8.4cm}{x{2.08 cm} x{5.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Math Module Functions}} \tn % Row 0 \SetRowColor{LightBackground} ceil(x) & Returns the smallest integer greater than or equal to x \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{copysign(x}, y) & Returns x with the sign of y \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} fabs(x) & Returns the absolute value of x \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{factorial(x)} & Returns the factorial of x \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} floor(x) & Returns the largest integer less than or equal to x \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} fmod(x, y) & Returns the remainder when x is divided by y \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{isfinite(x)} & Returns True if x is neither an infinity nor a NaN (Not a Number) \tn % Row Count 15 (+ 3) % Row 7 \SetRowColor{white} isinf(x) & Returns True if x is a positive or negative infinity \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} isnan(x) & Returns True if x is a NaN \tn % Row Count 18 (+ 1) % Row 9 \SetRowColor{white} ldexp(x, i) & Returns x {\emph{ (2}}*i) \tn % Row Count 20 (+ 2) % Row 10 \SetRowColor{LightBackground} modf(x) & Returns the fractional and integer parts of x \tn % Row Count 22 (+ 2) % Row 11 \SetRowColor{white} exp(x) & Returns e**x \tn % Row Count 23 (+ 1) % Row 12 \SetRowColor{LightBackground} expm1(x) & Returns e**x – 1 \tn % Row Count 24 (+ 1) % Row 13 \SetRowColor{white} log(x{[}, base{]}) & Returns the logarithm of x to the base (defaults to e) \tn % Row Count 26 (+ 2) % Row 14 \SetRowColor{LightBackground} log2(x) & Returns the base-2 logarithm of x \tn % Row Count 28 (+ 2) % Row 15 \SetRowColor{white} log10(x) & Returns the base-10 logarithm of x \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.08 cm} x{5.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Math Module Functions (cont)}} \tn % Row 16 \SetRowColor{LightBackground} pow(x, y) & Returns x raised to the power y \tn % Row Count 2 (+ 2) % Row 17 \SetRowColor{white} sqrt(x) & Returns the square root of x \tn % Row Count 3 (+ 1) % Row 18 \SetRowColor{LightBackground} acos(x) & Returns the arc cosine of x \tn % Row Count 4 (+ 1) % Row 19 \SetRowColor{white} asin(x) & Returns the arc sine of x \tn % Row Count 5 (+ 1) % Row 20 \SetRowColor{LightBackground} atan(x) & Returns the arc tangent of x \tn % Row Count 6 (+ 1) % Row 21 \SetRowColor{white} atan2(y, x) & Returns atan(y / x) \tn % Row Count 8 (+ 2) % Row 22 \SetRowColor{LightBackground} cos(x) & Returns the cosine of x \tn % Row Count 9 (+ 1) % Row 23 \SetRowColor{white} hypot(x, y) & Returns the Euclidean norm, sqrt(x{\emph{x + y}}y) \tn % Row Count 11 (+ 2) % Row 24 \SetRowColor{LightBackground} sin(x) & Returns the sine of x \tn % Row Count 12 (+ 1) % Row 25 \SetRowColor{white} tan(x) & Returns the tangent of x \tn % Row Count 13 (+ 1) % Row 26 \SetRowColor{LightBackground} \seqsplit{degrees(x)} & Converts angle x from radians to degrees \tn % Row Count 15 (+ 2) % Row 27 \SetRowColor{white} \seqsplit{radians(x)} & Converts angle x from degrees to radians \tn % Row Count 17 (+ 2) % Row 28 \SetRowColor{LightBackground} gamma(x) & Returns the Gamma function at x \tn % Row Count 19 (+ 2) % Row 29 \SetRowColor{white} lgamma(x) & Returns the natural logarithm of the absolute value of the Gamma function at x \tn % Row Count 22 (+ 3) % Row 30 \SetRowColor{LightBackground} pi & Mathematical constant, the ratio of circumference of a circle to it's diameter (3.14159...) \tn % Row Count 26 (+ 4) % Row 31 \SetRowColor{white} e & mathematical constant e (2.71828...) \tn % Row Count 28 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{ * import math and use math.fun()}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.2 cm} x{4.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Sets Functions}} \tn % Row 0 \SetRowColor{LightBackground} S.add(e) & Adds the element {\bf{e}} to the set {\bf{S}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} S1.update(S2) & Adds the items specified in the set {\bf{S2}} to the set {\bf{S1}} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} S.remove(e) & Remove the element {\bf{e}} from the set {\bf{S}} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} S.pop() & Removes any element from the set {\bf{S}} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} S.clear() & Remove all element from the set {\bf{S}} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} S.copy() & Creates a copy of the set {\bf{S}} \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} S1.union(S2) & Returns a set containing elements from both {\bf{S1}} and {\bf{S2}} \tn % Row Count 16 (+ 3) % Row 7 \SetRowColor{white} \seqsplit{S1.intersection(S2)} & Returns a set containing elements common in set {\bf{S1}} and {\bf{S2}} \tn % Row Count 19 (+ 3) % Row 8 \SetRowColor{LightBackground} \seqsplit{S1.difference(S2)} & Returns a set containing elements in set {\bf{S1}} but not in {\bf{S2}} \tn % Row Count 22 (+ 3) % Row 9 \SetRowColor{white} \seqsplit{S1.symmetric\_difference(S2)} & Returns a set containing elements which are in one of the either sets {\bf{S1}} and {\bf{S2}}, but not in both \tn % Row Count 27 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.8 cm} x{5.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Functions}} \tn % Row 0 \SetRowColor{LightBackground} S.count(str) & Counts the number of times string {\bf{str}} occurs in string {\bf{S}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} S.find(str) & Returns index of first occurrence of string {\bf{str}} in string {\bf{S}}, and {\bf{-1}} if {\bf{str}} is not present int string {\bf{S}} \tn % Row Count 8 (+ 5) % Row 2 \SetRowColor{LightBackground} S.rfind(str) & Returns index of last occurrence of string {\bf{str}} in string {\bf{S}}, and {\bf{-1}} if {\bf{str}} is not present in string {\bf{S}} \tn % Row Count 13 (+ 5) % Row 3 \SetRowColor{white} \seqsplit{S.capitalize()} & Returns a string that has first letter of the string {\bf{S}} in uppercase and rest of the characters in lowercase \tn % Row Count 18 (+ 5) % Row 4 \SetRowColor{LightBackground} S.title() & Returns a string that has first letter of every word in the string {\bf{S}} in uppercase and rest of the characters in lowercase \tn % Row Count 23 (+ 5) % Row 5 \SetRowColor{white} S.lower() & Returns a string that has all uppercase characters in string {\bf{S}} converted into lowercase characters \tn % Row Count 27 (+ 4) % Row 6 \SetRowColor{LightBackground} S.upper() & Returns a string that has all lower characters in string {\bf{S}} converted into uppercase characters \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.8 cm} x{5.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Functions (cont)}} \tn % Row 7 \SetRowColor{LightBackground} S.swapcase() & Returns a string that has all lowercase characters in string {\bf{S}} converted into uppercase characters and vice versa \tn % Row Count 5 (+ 5) % Row 8 \SetRowColor{white} S.isupper() & Returns {\bf{True}} if all alphabets in string {\bf{S}} are in uppercase,else {\bf{False}} \tn % Row Count 9 (+ 4) % Row 9 \SetRowColor{LightBackground} S.islower() & Returns {\bf{True}} if all alphabets in string {\bf{S}} are in lowercase,else {\bf{False}} \tn % Row Count 13 (+ 4) % Row 10 \SetRowColor{white} S.istitle() & Returns {\bf{True}} if string {\bf{S}} is in titlecase \tn % Row Count 15 (+ 2) % Row 11 \SetRowColor{LightBackground} \seqsplit{S.replace(str1},str2) & Returns a string that has every occurrence of string {\bf{str1}} in {\bf{S}} replaced by with the occurrence of string {\bf{str2}} \tn % Row Count 20 (+ 5) % Row 12 \SetRowColor{white} S.strip() & Returns a string that has whitespaces in {\bf{S}} removed from start and end \tn % Row Count 23 (+ 3) % Row 13 \SetRowColor{LightBackground} S.lstrip() & Returns a string that has whitespaces in {\bf{S}} removed from start \tn % Row Count 26 (+ 3) % Row 14 \SetRowColor{white} S.rstrip() & Returns a string that has whitespaces in {\bf{S}} removed from end \tn % Row Count 29 (+ 3) % Row 15 \SetRowColor{LightBackground} \seqsplit{S.split(delimeter)} & Returns a list formed by splitting the string {\bf{S}} into various substring. The delimeter is used to mark the split points \tn % Row Count 34 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.8 cm} x{5.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Functions (cont)}} \tn % Row 16 \SetRowColor{LightBackground} \seqsplit{S.partition(delimeter)} & Partitions the string {\bf{S}} into two parts base on {\bf{delimeter}} and returns a tuple comprising of string before {\bf{delimeter}} \tn % Row Count 5 (+ 5) % Row 17 \SetRowColor{white} \seqsplit{S.join(sequence)} & Returns a string comprising of elements of the sequence separated by delimeter {\bf{S}} \tn % Row Count 9 (+ 4) % Row 18 \SetRowColor{LightBackground} S.isspace() & Returns {\bf{True}} if all characters in string {\bf{S}} comprise of whitespace characters only,i.e. {\bf{' '}}, {\bf{'\textbackslash{}n'}}, {\bf{'\textbackslash{}t'}} else {\bf{False}} \tn % Row Count 15 (+ 6) % Row 19 \SetRowColor{white} S.isalpha() & Returns {\bf{True}} if all characters in string {\bf{S}} comprise of alphabets only, else {\bf{False}} \tn % Row Count 19 (+ 4) % Row 20 \SetRowColor{LightBackground} S.isdigit() & Returns {\bf{True}} if all characters in string {\bf{S}} comprise of digits only, else {\bf{False}} \tn % Row Count 23 (+ 4) % Row 21 \SetRowColor{white} S.isalnum() & Returns {\bf{True}} if all characters in string {\bf{S}} comprise of alphabets and digits only, else {\bf{False}} \tn % Row Count 27 (+ 4) % Row 22 \SetRowColor{LightBackground} \seqsplit{S.startswith(str)} & Returns {\bf{True}} if string {\bf{S}} starts with string {\bf{str}},else {\bf{False}} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.8 cm} x{5.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String Functions (cont)}} \tn % Row 23 \SetRowColor{LightBackground} \seqsplit{S.endswith(str)} & Returns {\bf{True}} if string {\bf{S}} ends with string {\bf{str}},else {\bf{False}} \tn % Row Count 3 (+ 3) % Row 24 \SetRowColor{white} S.encode(str) & Returns {\bf{S}} in encoded format according to the given encoding scheme \tn % Row Count 6 (+ 3) % Row 25 \SetRowColor{LightBackground} S.decode(str) & Returns the decoded string {\bf{S}} according to the given encoding scheme \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2 cm} x{6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{List}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{L.append(e)} & Adds the element {\bf{e}} to the end of the list {\bf{L}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{L.extend(L2)} & Adds the items specified in the list {\bf{L2}} at the end of the list {\bf{L}} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{L.remove(e)} & Remove the element {\bf{e}} from the list {\bf{L}} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} L.pop(i) & Removes the element specified at index {\bf{i{\emph{ from the list }}L}}* \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{L.count(e)} & Returns count of occurrence of element {\bf{e}} in list {\bf{L}} \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{L.index(e)} & Returns index of element {\bf{e}} from list {\bf{L}} \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{L.insert(i},e) & Returns element {\bf{e}} at the index {\bf{i}} in list {\bf{L}} \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} L.sort() & Sorts the elements of the list {\bf{L}} \tn % Row Count 18 (+ 2) % Row 8 \SetRowColor{LightBackground} \seqsplit{L.reverse()} & Reverses the order elements in list {\bf{L}} \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.56 cm} x{5.44 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{File Handling}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{open(filename},mode) & Open a {\bf{file}} and store it as an object \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{file.close()} & Close a file which is opened \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} file.read() & Read whole data from {\bf{file}} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{file.readline()} & Read a line from {\bf{file}} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{file.readlines()} & Read all the lines in a list from {\bf{file}} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{file.write('data')} & Write data in a file \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{ * Mode can be 'r', 'w' and 'a' }}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}