\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{Mnasnan (mnasnan)} \pdfinfo{ /Title (nancy-s-python.pdf) /Creator (Cheatography) /Author (Mnasnan (mnasnan)) /Subject (Nancy's 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}{FF0000} \definecolor{LightBackground}{HTML}{FFEFEF} \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{Nancy's Python Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Mnasnan (mnasnan)} via \textcolor{DarkBackground}{\uline{cheatography.com/25754/cs/6873/}}} \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}Mnasnan (mnasnan) \\ \uline{cheatography.com/mnasnan} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 3rd February, 2016.\\ Updated 10th October, 2017.\\ 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 Convention}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Rule for giving name} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{- letter} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{- numbers} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{- underscore \_} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 4 (+ 0) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Valid name} \tn % Row Count 5 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{- \_myStr} \tn % Row Count 6 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{- my3} \tn % Row Count 7 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{- Hello\_​there Invalid name} \tn % Row Count 8 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{- 3my="hi" -{}- cannot start with number} \tn % Row Count 9 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{- first name="hi"} \tn % Row Count 10 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{- first-name} \tn % Row Count 11 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{- first+name} \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.88 cm} x{5.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Symbol}} \tn % Row 0 \SetRowColor{LightBackground} == & Equal \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} != & Not equal \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textgreater{}= & More than or equal \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textless{}= & Less than or equal \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \% (Madulo) & Find the remainder \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.68 cm} x{4.32 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Multiplication and Exponents}} \tn % Row 0 \SetRowColor{LightBackground} string * number & Combine that string \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string* string & CRASH! \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} number * number & Multiply (Math) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} string ** string & CRASH! \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} number ** number & Exponent (Math) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} string ** number & CRASH! \tn % Row Count 6 (+ 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}{Area of Triangle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def areaOfTriangle(base, hight) : \newline return 0.5 {\emph{ base }} hight \newline \newline user\_base = float(input("Enter the base of the triangle: ")) \newline user\_height = float(input("Enter the hight of the triangle: ")) \newline \newline print ("The area of the triangle is", \seqsplit{areaOfTriangle(user\_base}, user\_height)) \newline \newline \newline def volumeOfPrism(area, high) : \newline return area * high \newline \newline user\_prism\_high = float(input("Enter the hight of the prism: ")) \newline \newline print ("The volume of the prism is", \seqsplit{volumeOfPrism(areaOfTriangle(user\_base}, user\_height), user\_prism\_high))} \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}{Reverse Word}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{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}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Sort fruit list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{fruits = {[}{]} \#an empty list \newline \newline for number in range(5): \newline user\_fruit = input("Please enter a fruit") \newline \seqsplit{fruits.append(user\_fruit)} \newline \newline print ("Size of fruit list is", len(fruits)) \newline \newline fruits.sort() \newline \newline for fruit in fruits: \newline print ("Fruit: ", fruit)} \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}{Countdown Machine}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{number = int(input("What number do you want to count down? ")) \newline countdown\_string = ' ' \newline \newline while number \textgreater{} 0: \newline countdown\_number = 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}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Area of Circle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{user\_radius = input("What is a radius of a circle?") \newline \newline radius = float(user\_radius) \newline \newline pi = float(3.1415) \newline \newline area = pi{\emph{(radius}}*2) \newline \newline print ("The area of the circle is", area)} \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}{Create List}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{create a function name: createList \newline argument: quitword \newline return: a list \newline \newline def createList(quitword): \newline mylist = {[}{]} \#empty list \newline \newline while True: \#loop forever \newline user\_word = input("Please enter a list item: ") \newline \newline if user\_word == quitword: \newline return mylist \#returns the list and exits the function \newline \newline duplicateword = False \newline \newline for item in mylist: \newline if user\_word == item: \newline duplicateword = True \newline \newline if (duplicateword == True): \newline print ("Duplicate Word!") \newline else: \newline \seqsplit{mylist.append(user\_word)} \#Adds the user\_word to the end of the list \newline \newline userlist = createList('stop') \#Function Call \newline print (userlist)} \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}{Function (Ex.)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def myprint2(text, decoration): \#Text and decoration is a parameter \newline print (decoration + str(text) + decoration) \newline return \newline \newline myprint2("Hello", "+++++") \newline myprint2("Hello", "-=-=-=-=") \newline myprint2("Hello", "\textless{}\textless{}\textless{}\textless{}\textless{}")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{+++++Hello+++++ \newline -=-=-=-=Hello-=-=-=-= \newline \textless{}\textless{}\textless{}\textless{}\textless{}Hello\textless{}\textless{}\textless{}\textless{}\textless{}} \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}{Palindrome}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def isPalindrome(word): \newline \newline index = 0 \newline reverse = '' \newline \newline while int(index) \textless{} len(user\_word): \newline reverse = user\_word{[}index{]} + (reverse) \newline index = int(index) + 1 \newline \newline if user\_word == reverse: \newline return True \newline else: \newline return False \newline \newline while True: \newline user\_word = input("Please enter a word: ") \newline \newline if user\_word == "quit": \newline break \newline \newline print("Length of the", user\_word, "is", len(user\_word)) \newline \newline Palindrome = isPalindrome(user\_word) \newline \newline if Palindrome == True: \newline print (user\_word, "is a Palindrome!") \newline else: \newline print (user\_word, "is not a Palindrome")} \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}{Sort word per line}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{mystr = "Hello" \newline \newline letter\_num = 0 \newline \newline while letter\_num \textless{} len(mystr): \newline print (mystr{[}letter\_num{]}) \newline letter\_num = letter\_num + 1} \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}{Convert decimal to binary}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{user\_number = ' ' \newline \newline while user\_number != ' 0 ' : \newline user\_number = input ("Enter a number to convert to binary") \newline number = int(user\_number) \newline binary\_string = ' ' \newline \newline while (number \textgreater{} 0): \newline remainder = number\%2 \newline binary\_string = str(remainder)+ binary\_string \newline number = number//2 \newline \newline print ("Binary string is", binary\_string)} \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}{Area of Circle by Function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def areaOfCircle(r): \newline pi = 3.1415 \newline area = pi {\emph{ r }}* 2 \newline return area \newline \newline user\_radius = input("Enter the radius of the circle: ") \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}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Guess word game}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{import random \newline \newline guesslist = {[}'grape', 'orange', 'chloroplast', 'ribosome', 'lipstick'{]} \newline \newline chance = 3 \newline score = 0 \newline \newline print (guesslist) \newline \newline while chance != 0: \newline random\_item = random.choice(guesslist) \newline user\_input = input("Please guess a word: ") \newline if user\_input == random\_item: \newline print ("That's correct!") \newline score = score + 100 \newline print ("Score:", score) \newline else: \newline if user\_input not in guesslist: \newline print ("Sorry, that isn't even in the list!") \newline chance = chance - 1 \newline print ("Chance Remaining:", chance) \newline else: \newline print ("Sorry, wrong choice!") \newline chance = chance - 1 \newline print ("Chance Remaining:", chance) \newline \newline if chance == 0: \newline print ("The word was", random\_item) \newline print ("The score is", score)} \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}{Convert binary to decimal}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{num = str(int(input("Enter a binary to convert to decimal "))) \newline \newline dec = int(num, 2) \newline \newline print ("The decimal number is", dec)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}