\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{guyvk} \pdfinfo{ /Title (python-cheat-sheet.pdf) /Creator (Cheatography) /Author (guyvk) /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}{FA1D05} \definecolor{LightBackground}{HTML}{FEF0EF} \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}{guyvk} via \textcolor{DarkBackground}{\uline{cheatography.com/25801/cs/6919/}}} \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}guyvk \\ \uline{cheatography.com/guyvk} \\ \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 11th 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*}{4} \begin{tabularx}{3.833cm}{x{0.89258 cm} x{2.54042 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{volcabulary}} \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 & whole number/counting number \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} float number & The number in decimal \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} syntax & grammar/structure of language \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} modulo & find the remainder \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} boolean & true/false \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} argument & A value passed to a function (or method) when calling the function. \tn % Row Count 12 (+ 3) % Row 8 \SetRowColor{LightBackground} parameter & A named entity in a function definition that specifies an argument that the function can accept. \tn % Row Count 16 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.51495 cm} x{2.91805 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Function}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{print()} & displays information on the screen \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{input()} & receive info from the user \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} len() & show the number of word in the string \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} int() & convert string or decimal number into integer number \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{float()} & convert string or integer number into decimal number \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} str() & convert value to word(s) \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \# & comment, no effect \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} \seqsplit{range()} & the list of number between two numbers \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} def & define function \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} while & create loop with some condition. \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} for & create loop with no condition \tn % Row Count 17 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{0 01 0123 01234}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{mystring = "" \newline for items in range(5): \newline mystring = mystring + str(items) \newline print(mystring) \newline \newline \#or \newline \newline mystring = "" \newline index = 0 \newline while index \textless{} 5: \newline mystring = mystring + str(index) \newline print (mystring) \newline index = index + 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} x{3.0897 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Math}} \tn % Row 0 \SetRowColor{LightBackground} == & equal to \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} != & no equal to \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textless{} & less than \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textgreater{} & more than \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \textless{}= & less than or equal to \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \textgreater{}= & more than or equal to \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \% & Modulo, find the remainder \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} / & divide with answer as a float. e.g. 5/2=2.5 \tn % Row Count 9 (+ 2) % Row 8 \SetRowColor{LightBackground} // & divide with answer as an integer. e.g. 5//2=2 \tn % Row Count 11 (+ 2) % Row 9 \SetRowColor{white} ** & exponent \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{0.89258 cm} x{2.54042 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Naming Convention}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{Invalid name} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} -3my="hi" & -{}-cannot start with number \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{-first name="hi"} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{3.833cm}}{-first-name} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Basic Calculator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{def calc(num1, num2, operation): \newline if operation=="sum": \newline return sum(num1, num2) \newline elif operation=="product": \newline return product(num1, num2) \newline elif operation=="diff": \newline return diff(num1, num2) \newline elif operation=="div": \newline return div(num1, num2) \newline else : \newline print ("No operation") \newline \newline def sum(a, b): \newline myvar1 = a+b \newline return myvar1 \newline \newline def product(a, b): \newline myvar2 = a*b \newline return myvar2 \newline \newline def diff(a, b): \newline myvar3 = a-b \newline return myvar3 \newline \newline def div(a, b): \newline if b!=0 : \newline return (a//b) \newline else: \newline print ("Error") \newline \newline print (calc(10,0,"div")) \newline print (calc(1,2,"sum")) \newline print (calc(4,2,"diff")) \newline print (calc(9, 3,"div")) \newline print (calc(2, 12,"product"))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Fibonacci}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{index = 0 \newline index2 = 0 \newline Fibo = "" \newline while index \textless{} 50: \newline if index == 0: \newline Fibo = Fibo + str(index) \newline index = index + 1 \newline elif index \textgreater{} 0: \newline Fibo = Fibo + ", " + str(index) \newline index = index + index2 \newline index2 = index-index2 \newline print (Fibo)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Reverse Word}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{while True: \newline word = input("Please enter a word") \newline index = 0 \newline reverse = "" \newline \newline while int(index) \textless{} len(word): \newline reverse = word{[}index{]} + (reverse) \newline index = int(index) + 1 \newline \newline print("Reverse: ",reverse)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{even and odd}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{even = 0 \newline odd = 0 \newline while True: \newline mynum = int(input("Enter the positive number: ")) \newline if mynum \textgreater{} 0: \newline if mynum\%2 == 0: \newline even = even + 1 \newline elif mynum\%2 != 0: \newline odd = odd+1 \newline elif mynum \textless{} 0: \newline break \newline print (even, (" of them are even.")) \newline print (odd, (" of them are odd."))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Guessing Game}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{import random \newline \newline mylist = {[}'happy', 'love' , 'flower' , 'golden' , 'valentine', 'rainbow'{]} \newline score = 0 \newline chance = 5 \newline print (mylist) \newline \newline random\_item = random.choice(mylist) \newline \newline while chance \textgreater{} 0 : \newline user\_guess = input("Guess a word: ") \newline if user\_guess == random\_item: \newline print ("That's correct!") \newline score = score + 100 \newline print ("Score: ",score) \newline random\_item = random.choice(mylist) \newline else: \newline chance = chance - 1 \newline print ("Chance Remaining: ",chance) \newline if user\_guess in mylist: \newline print ("Sorry, wrong choices") \newline else: \newline print ("Sorry, that is not even in the list !") \newline print ("Final Score: ",score) \newline print ("Game Over!The word was",random\_item)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{While loop list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{whileList = {[}'Guy','Pop','Pat','Kim','Cliff','Anon'{]} \newline index = 0 \newline while index \textless{} len(whileList): \newline print (whileList{[}index{]}) \newline index = index + 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Max value in list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{\#Example 1 \newline def max2(num1, num2): \newline maxvalue = num1 \newline \newline if num2 \textgreater{} maxvalue: \newline maxvalue = num2 \newline return maxvalue \newline print (max2(3,5)) \newline \newline \newline \#Example 2 \newline def max3(num1, num2, num3): \newline maxvalue = num1 \newline \newline if num2 \textgreater{} num3 \textgreater{} maxvalue: \newline maxvalue = num2 \newline \newline elif num3 \textgreater{} num2\textgreater{} maxvalue: \newline maxvalue = num3 \newline return maxvalue \newline print (max3(3,5,9)) \newline \newline \newline \#Example 3 \newline def maxlist(list): \newline maxvalue = list{[}0{]} \newline for num in list: \newline if maxvalue \textless{} num: \newline maxvalue = num \newline return maxvalue \newline print (maxlist(range(0,56)))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{area of circle calculation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{def areaOfCircle(r): \newline if r \textless{}= 0: \newline return "Error" \newline pi = 3.1415 \newline area = pi{\emph{r}}*2 \newline return area \newline user\_radius = float(input("Enter the radius: ")) \newline radius = float(user\_radius) \newline \newline print ("the area of the circle is",areaOfCircle(radius))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{palindrome}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{def isPalindrome(word): \newline index = 0 \newline while index \textless{} 1/2*len(word): \newline if word{[}index{]} == word{[}len(word)-1-index{]} : \newline index = index+1 \newline else : \newline return False \newline return True \newline \newline while True: \newline word = input("Enter your word: ") \newline if word == str("quit"): \newline break \newline else: \newline if isPalindrome(word) == True : \newline palindrome = (word + str(" is a palindrome.")) \newline else : \newline palindrome = (word + str(" is not a palindrome.")) \newline \newline \newline \newline print(palindrome)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}