\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{shadowG (grimeswong)} \pdfinfo{ /Title (python-baisc-usage-and-syntax.pdf) /Creator (Cheatography) /Author (shadowG (grimeswong)) /Subject (Python - Baisc Usage \& Syntax 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}{FC9732} \definecolor{LightBackground}{HTML}{FEF8F2} \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 - Baisc Usage \& Syntax Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{shadowG (grimeswong)} via \textcolor{DarkBackground}{\uline{cheatography.com/49785/cs/41384/}}} \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}shadowG (grimeswong) \\ \uline{cheatography.com/grimeswong} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 18th May, 2024.\\ 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{3.28 cm} x{4.72 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Comment}} \tn % Row 0 \SetRowColor{LightBackground} comment - single line & \# this is a single line comment \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} comment - multiline comments & """ this is multi lines comments """ \tn % Row Count 5 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Variables Declaration}} \tn % Row 0 \SetRowColor{LightBackground} integer & students\_count = 1000 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} decimal & rating = 4.99 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} boolean & is\_published = False \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} string - single line & course\_name = "Python" \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} string - multiple lines & course\_name = """ Multiple Lines """ \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} string concatenation & full\_name = first\_name + " " + last\_name \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} formatted string & full\_name = f"\{first\_name\} \{last\_name\}" \tn % Row Count 11 (+ 2) % Row 7 \SetRowColor{white} assign multiple variables & x, y, z = "John", "Alex", "May" \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} one value to multiple variables & x = y = z = "Value" \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} uppack & animals = {[}"monkey", "cat", "dog"{]} x, y, z = animals \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{boolean - capitalise the first letter \newline single line - can use either single or double quote \newline multiple lines - use triple double quote to wrap the entire strings \newline formatted string - use 'f' and curly braces with the double quotes wrap with the separated strings} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.44 cm} x{4.56 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Output Variables}} \tn % Row 0 \SetRowColor{LightBackground} text output & print("Hello World") \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} variable output & print(x + y + x) \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Global Variable}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def myfunc(): \newline global x \newline x = " John" \newline myfunc() \newline print("Hello " + x)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data Types}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{text Type: str \newline % Row Count 1 (+ 1) numeric Types: int, float, complex \newline % Row Count 2 (+ 1) sequence Types: list, tuple, range \newline % Row Count 3 (+ 1) mapping Type: dict \newline % Row Count 4 (+ 1) set Types: set, frozenset \newline % Row Count 5 (+ 1) boolean Type: bool \newline % Row Count 6 (+ 1) binary Types: bytes, bytearray, memoryview \newline % Row Count 7 (+ 1) none Type: NoneType% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Collection Types}} \tn % Row 0 \SetRowColor{LightBackground} List - like an array & fruits = {[}'apple', 'orange', 'banana'{]} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Dictionary - key value pair & fruit = \{'name'='apple, 'colour: 'red'\} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Tuples - order is unchangable & fruit = ('apple', 'orange', 'banana') \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Set - set items are unchangable & fruit = \{'apple', 'orange', ;banana'\} \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{If - else}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{while loop}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{for loop}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{function}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.728 cm} x{2.808 cm} x{1.296 cm} x{1.368 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Built-in function}} \tn % Row 0 \SetRowColor{LightBackground} check type of variable & x = 2.8 & \seqsplit{print(type(x))} & \textless{}class 'float'\textgreater{} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} casting & int(), float(), str() & x = int(3) & x will be 3 \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} input & username = input("Enter your name") & user type "John" & \seqsplit{username} = "John" \tn % Row Count 8 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.448 cm} x{2.16 cm} x{1.512 cm} x{1.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Strings}} \tn % Row 0 \SetRowColor{LightBackground} slicing strings & a = "Hello World" & print(a{[}2:7{]}) & 'llo W' \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} from index to the end & & print(a{[}2:{]}) & 'llo \seqsplit{World'} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} from start to index & & print(a{[}:8{]}) & \seqsplit{'Hello} Wo' \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} from negative to negative & -2 is not included 'd' & print(a{[}-7:-2{]}) & 'o Wor' \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.736 cm} x{2.964 cm} x{1.9 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{String Methods}} \tn % Row 0 \SetRowColor{LightBackground} upper case & print("Hello \seqsplit{World".upper())} & "HELLO WORLD" \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} lower case & print("Hello World".lower() & "hello world" \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} trim white space (leading \& trailing) & print(" No Space ".strip & "No Space" \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} replace string & print("Hello \seqsplit{John".replace("John"},"Alex")) & "Hello Alex" \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} split string & print("Hello, World".split(",") & return {[}"Hello", "World"{]} \tn % Row Count 13 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.964 cm} x{3.116 cm} x{1.52 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{String Concatenation}} \tn % Row 0 \SetRowColor{LightBackground} use '+' operator & `"Hello " + "World"` & "Hello World" \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} use join() & `"".join({[}"Hello "," World{]}"` & "Hello World" \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} use format() - convert number to text & `age = 18" text = "I am \{\} years old" \seqsplit{print(text.format(age))`} & "I am 18 years old" \tn % Row Count 8 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{"+' operator cannot combine string and number \newline \{\} is a placeholder} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Formatted String}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\# formatted string \newline first = "Mosh" \newline last = "Hamedani" \newline full = f"\{first\} \{last\}" \newline print(full)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{There are three types of number: integer, float \& complex number \newline x = 1 \# integer \newline x = 1.1 \# float \newline x = 1 + 2j \# a + bi \# complex number \newline \newline print(10 + 3) \# addition \newline print(10 - 3) \# subtraction \newline print(10 {\emph{ 3) \# multiplication \newline print(10 / 3) \# division outcome with float \newline print(10 // 3) \# division outcome with integer \newline print(10 \% 3) \# modulus, the remainder of a division \newline print(10 }}* 3) \# exponent, the power} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}