\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{Theo666} \pdfinfo{ /Title (python-chap-2.pdf) /Creator (Cheatography) /Author (Theo666) /Subject (Python\_Chap\_2 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}{79B352} \definecolor{LightBackground}{HTML}{F6FAF4} \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\_Chap\_2 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Theo666} via \textcolor{DarkBackground}{\uline{cheatography.com/193738/cs/40328/}}} \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}Theo666 \\ \uline{cheatography.com/theo666} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 17th September, 2023.\\ Updated 25th September, 2023.\\ 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Naming}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{The name of variables in Python can consist of lowercase letters (a to z), uppercase letters (A to Z), numbers (0 to 9), or the underscore character (\_). Spaces aren't be allowed in a variable name. \newline % Row Count 4 (+ 4) Additionally, a variable name must not start with a digit, and it is not recommended to start it with the underscore character except in very specific cases, for example, `if \_\_name\_\_ == "\_\_main\_\_"` \newline % Row Count 8 (+ 4) Furthermore, it is essential to avoid using a built-in word in Python as a variable name (for example: print, range, for, from, etc.).% Row Count 11 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.08 cm} x{3.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Type of variables}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Integer}} & `int()` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{Float}} & `float()` \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} {\bf{String }}" " or ' ' or ''' ''' & `str()` \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} {\bf{List}} {[} ..., ...{]} & `list()` \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} {\bf{Dictionary}} \{key : value, ...\} & `\{key:value\}` `dict{[}key{]} = value` \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} {\bf{Tuple}} (..., ...) & `tuple()` \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} {\bf{Set}} \{..., ...\} & `set()` \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Boolean}} True\&False} \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} {\bf{Frozenset}} frozenset(\{..., ...\}) & `frozenset()` \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{- To check the type of variable, type(variable) \newline - Floats can be in scientific format, like 3e8 = 3*10\textasciicircum{}8. \newline - To convert float to scientific format, `"\%e"\%float`. It will return a string \newline - To use mathematical constant e, it should import module math. `math.e` \newline - To make a long number visible, using underscores "\_" to separate digits in the version 3.6+, like 380\_000} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.76 cm} x{6.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Conversion}} \tn % Row 0 \SetRowColor{LightBackground} FLoat \& Int & `float()` `int()` `round()` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} List to Str & `'separator'.join(list)` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Str to List & `list(string)` \seqsplit{`string.split('separator')`} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{- `int(float)` returns only the integer part of the float and `round(float, num)` is used to round a number to a specified number of decimal places. \newline - `'sep'.join()` cannot combine lists with full integers. {[}str(i) for i in list; separator by default is space} \tn \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}{Properties \& Common Functions}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{NUM}} & `int()` `round(value, decimal)` `abs()` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{STRING}} & iterable, indexable, immutable; len(); str + str, str * positive int; `str.replace(a,b)` `str.count(a)` `str.title()` `str.upper()` `str.lower()` `str.strip()` `str.rstrip()` `str.lstrip()` \tn % Row Count 9 (+ 7) % Row 2 \SetRowColor{LightBackground} {\bf{LIST}} & `list{[}start:stop:step{]}` `enumerate(list)` `max()` `min()` `sum()` `list.reverse()` `reversed(list)` list * int, list + list; `{[}i for i in list for \_ in range(){]}` `list.append()` `list.insert(item,pos)` `list.remove()` `list.pop()` `del list{[}{]}` `list.index(item)` `sorted(list)` `list.sort()` \tn % Row Count 19 (+ 10) % Row 3 \SetRowColor{white} {\bf{RANGE}} & `range(start, stop, step)` step could be negative; similar to lists, but immutable \tn % Row Count 22 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{DICT}} & iterable by key, ordered by key or value : `sorted(dic)` `sorted(dic, key=dico.get)` `dic.items()` `dic.keys()` `dic.values()` `dic{[}key{]}` or `dic.get(key)` `dic{[}key{]}=value` `del dic{[}key{]}` `dict.pop(key)`; len() \tn % Row Count 29 (+ 7) % Row 5 \SetRowColor{white} {\bf{TUPLE}} & len(), iterable, ordered, indexiable, immutable. Avoid containing mutable variables \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2 cm} x{6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Properties \& Common Functions (cont)}} \tn % Row 6 \SetRowColor{LightBackground} {\bf{SET}} & iterable, mutable, unordered, indexable; `set.add()` `set.remove()` \seqsplit{`set.updata()``set(list1)} \& set(list2)` sames; `set(list1) | set(list2)` union; `set(list1) - set(list2)` \tn % Row Count 6 (+ 6) % Row 7 \SetRowColor{white} {\bf{FROZENSET}} & `f1.union(f2)` `f1.intersection(f2)` \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{- If strings or lists are multiplied by a negative integer or a float, it will returns nothing but a null string/list or an error \newline - To duplicate a list, `list.copy()` or `list{[}:{]}`. It should exactly avoid using `lst2 = lst1`, this creates a reference to the original list with the same ID `id()` \newline - `list{[}1:n{]}` stop at n-1, even if negative index \newline - `set()` can use to remove duplicated elements in lists and to take keys of a dictionary \newline - sets cannot be applied operators like + or *} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{1.008 cm} x{1.656 cm} p{1.224 cm} x{3.312 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Arithmetic Operators}} \tn % Row 0 \SetRowColor{LightBackground} x + y & add & x - y & substract \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} x * y & multiply & x ** y & x\textasciicircum{}y \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} x / y & divide & x // y & integet division \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} x \% y & modulus & & \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{Assignment shortcuts: x op= y, for example, x += y is egal to x = x+y} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{4 cm} p{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Comparison Operators}} \tn % Row 0 \SetRowColor{LightBackground} x == y & x != y \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} x \textless{} y & x \textgreater{} y \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} x \textless{}= y & x \textgreater{}= y \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}