\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{Anon123} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (Anon123) /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}{86DE2F} \definecolor{LightBackground}{HTML}{F7FCF2} \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}{Anon123} via \textcolor{DarkBackground}{\uline{cheatography.com/25787/cs/6921/}}} \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}Anon123 \\ \uline{cheatography.com/anon123} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 5th February, 2016.\\ Updated 13th May, 2016.\\ 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.4931 cm} x{3.4839 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Vocabulary}} \tn % Row 0 \SetRowColor{LightBackground} variable & something that can change \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string & a list of characters \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} integer number & pos/neg natural numbers and zero \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} floating point & decimal number \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} length & the length of the string \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} Modulo & Finds the remainder \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} Boolean & True/False \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} Syntax & Grammar/Structure of language \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} range (1,10) & the numbers 1-9 \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} range(10) & the numbers 0-10 \tn % Row Count 13 (+ 1) \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}{Conditionals}} \tn % Row 0 \SetRowColor{LightBackground} If..... :then..... else....... & If the statement is true then do command under then else do command under else \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} while...... & While {\bf{this}} is true loop the command under the conditional \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} While True & loops forever \tn % Row Count 9 (+ 1) % Row 3 \SetRowColor{white} for {\bf{each item}} in {\bf{name of list}} & For every item in the list repeat the command under the loop that many times. (a string is a list too) \tn % Row Count 15 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Naming Conventions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Rules for naming variables}}: \newline % Row Count 1 (+ 1) - letters \newline % Row Count 2 (+ 1) - numbers \newline % Row Count 3 (+ 1) - underscores (\_) \newline % Row Count 4 (+ 1) - can start with {\bf{letters}} or {\bf{underscores}} ONLY \newline % Row Count 6 (+ 2) - NO SPACES \newline % Row Count 7 (+ 1) {\bf{Valid names}}: \newline % Row Count 8 (+ 1) - {\bf{\_}}mystr \newline % Row Count 9 (+ 1) - my3 \newline % Row Count 10 (+ 1) Hello\_there \newline % Row Count 11 (+ 1) {\bf{Invalid names}}: \newline % Row Count 12 (+ 1) - {\bf{3}}my= "hi" -{}- cannot start with number \newline % Row Count 13 (+ 1) - first{\bf{ }} name = "hi" -{}- no spaces allowed \newline % Row Count 14 (+ 1) - first{\bf{-}}name -{}- dashes are not accepted% Row Count 15 (+ 1) } \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}{Lists}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#this is how you maek a list in python \newline \newline shoppinglist = {[}'coke zero', 'bacon', 'water', 'jelly', 'gummy bears'{]} \newline \newline print (shoppinglist) \newline \newline print (shoppinglist{[}0{]}) \newline \#prints the first item of the list \newline \newline list\_num = 0 \newline \newline while list\_num \textless{} len(shoppinglist): \newline print ("List:", shoppinglist{[}list\_num{]}) \newline list\_num =list\_num+1 \newline \newline \newline \#for loop-{}-\textgreater{} same as the above \newline \#For every item in that list we're going to print it. \newline \newline for item in shoppinglist: \newline print (item) \newline \newline \newline \newline numbers = range(1,5) \newline \#print up until less then the last number. \newline for item in numbers: \newline print (item) \newline \newline \# a string is a list of characters, letters, numbers, etc. \newline \newline mystr = "hello" \newline \newline for letter in mystr: \newline print (letter)} \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}{Adding strings number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mystring = "" \newline count = 0 \newline while count \textless{} 5: \newline mystring = mystring + str(count) \newline print (mystring) \newline count = count + 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.54747 cm} x{4.42953 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Symbols}} \tn % Row 0 \SetRowColor{LightBackground} == & equal to \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} != & not equal to \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textless{} & less than \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textless{}= & less than or equal to \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \textgreater{} & greater than \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \textgreater{}= & greater than or equal to \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} + & add \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} - & subtract \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} * & multiply \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} / & divide and quotient is float \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} // & divide and quotient is integer \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} ** & exponent \tn % Row Count 12 (+ 1) % Row 12 \SetRowColor{LightBackground} \% & modulo: the remainder \tn % Row Count 13 (+ 1) % Row 13 \SetRowColor{white} {[}...{]} & The position of the item in the list or the letter in a word \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.59264 cm} x{3.38436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Multiplication \& Exponents}} \tn % Row 0 \SetRowColor{LightBackground} string * string & CRASH!!! \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} string * number & combines the strings multiple time \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} number * number & math (multiply) \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} string ** number & CRASH!!! \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} number ** number & Exponent(Math) \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} string ** number & CRASH!!! \tn % Row Count 12 (+ 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}{Even/odd using counters}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{even\_value = 0 \newline odd\_value = 0 \newline while True: \newline user\_input = input("Enter a positive number: ") \newline number = int(user\_input) \newline if number \textless{} 0: \newline print ("There were ", even\_value ,"even numbers and there were " , odd\_value ,"odd numbers.") \newline break \newline if number \% 2 == 0: \newline even\_value = even\_value + 1 \newline else: \newline odd\_value = odd\_value +1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Addition}} \tn % Row 0 \SetRowColor{LightBackground} string + string & squishes them together \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string + number & crash \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} number + number & math(addition) \tn % Row Count 3 (+ 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}{Area of circle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def areaofCircle(r): \newline if r \textless{}= 0: \newline return "Error: invalid radius" \newline \newline pi = 3.1415 \newline area = pi{\emph{(r}}*2) \newline return (area) \newline user\_radius = input('Enter the radius: ') \newline radius = float(user\_radius) \newline print("The area of the circle is", areaofCircle(radius))} \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}{fibonacci}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{num1= 0 \newline num2=1 \newline mystr = '0' \newline \newline while num1 + num2 \textless{} 89: \newline Fibonacci = num1 +num2 \newline \newline num1= num2 \newline \newline num2=Fibonacci \newline mystr= mystr+"," + str(num1) \newline print (mystr)} \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}{Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def calc(num1, num2, operation): \newline \newline if operation == "sum": \newline return sum(num1, num2) \newline \newline elif operation == "product": \newline return product(num1, num2) \newline \newline elif operation == "diff": \newline return diff(num1, num2) \newline \newline elif operation == "div": \newline return div(num1, num2) \newline \newline \newline def sum(a, b): \newline return a+b \newline \newline def product(a, b): \newline return a*b \newline \newline \newline def diff (a, b): \newline return a-b \newline \newline \newline \newline def div(a, b): \newline if b == 0: \newline return ("Error") \newline elif b != 0: \newline return a/b \newline \newline \newline \newline \newline print (calc(1,2,"sum")) \newline print (calc(4,2, "diff")) \newline print (calc (9,0, "div")) \newline print (calc (2,12, "product")) \newline \newline calc(1, 2, "sum")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.59264 cm} x{3.38436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} print() & displays information on the screen \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} input() & receives info from the user \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} int() & converts the value into an integer \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} str() & converts the value to a string \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} float() & converts the value to a floating point \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} len() & The length of the string \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} \# & One line comment not include in code \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} ''' & Multi-line comment \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} def \seqsplit{name(variable):} & defines a block as in subbing the name for lines of commands. The variable in the parentheses can be replaced by inputing the desire value into those parentheses. \tn % Row Count 19 (+ 6) % Row 9 \SetRowColor{white} range (100) & range of numbers from 0 to one less then that. \tn % Row Count 21 (+ 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}{Spelling a string out in reverse code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{word = input("Type in an word: ") \newline reverse = "" \newline for letter in word: \newline reverse = letter + reverse \newline print ("Reverse: ", reverse)} \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}{This prints the true or false value using boolean}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{print(True) \newline print (2\textless{}3) \newline print (2 != 2)} \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}{Countdown Code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = input("Please enter a number: ") \newline \newline number = int(user\_number) \newline \newline countdown\_string = "" \newline \newline while number \textgreater{} 0: \newline countdown\_string = countdown\_string + " " + str(number) \newline number = number-1 \newline \newline print (countdown\_string)} \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}{palindrome and efficient loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def isPalindrome(word): \newline \newline \newline letter\_num =0 \newline while letter\_num \textless{} len(word)-1-letter\_num: \newline \newline if word{[}letter\_num{]} == word{[}len(word)-1-letter\_num{]}: \newline letter\_num = letter\_num +1 \newline \newline else: \newline return False \newline return True \newline \newline while True: \newline user\_input = input("Please type in a word: ") \newline if user\_input == "quit": \newline break \newline \#print \seqsplit{(isPalindrome(user\_input))} \newline myvalue = \seqsplit{isPalindrome(user\_input)} \newline if myvalue == True: \newline print (user\_input + " is a palindrome.") \newline elif myvalue == False: \newline print (user\_input + " is not a palindrome.")} \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 loops \#2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{word = input("Type in an word: ") \newline reverse = "" \newline for letter in word: \newline reverse = letter + reverse \newline """ \newline letter\_num = 0 \newline \newline reverse = '' \newline \newline while letter\_num \textless{} len(word): \newline reverse = (word{[}letter\_num{]} + reverse) \newline letter\_num = letter\_num + 1 \newline """ \newline \newline \newline \newline print ("Reverse: ", reverse)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}