\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{Salisa Stamp} \pdfinfo{ /Title (python-sheet.pdf) /Creator (Cheatography) /Author (Salisa Stamp) /Subject (Python Sheet 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}{FF675C} \definecolor{LightBackground}{HTML}{FFF5F4} \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 Sheet Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Salisa Stamp} via \textcolor{DarkBackground}{\uline{cheatography.com/25742/cs/6860/}}} \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}Salisa Stamp \\ \uline{cheatography.com/salisa-stamp} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 15th February, 2016.\\ Updated 12th 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}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Operators}} \tn % Row 0 \SetRowColor{LightBackground} + & addition \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} - & subtraction \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} * & multiplication \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} / & division \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} // & division(floor division) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} ** & exponent \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \% & module \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} == & equal to \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} != & unequal to \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} \textless{} & lesser than \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} \textless{}= & lesser than or equal to \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} \textgreater{} & greater than \tn % Row Count 12 (+ 1) % Row 12 \SetRowColor{LightBackground} \textgreater{}= & greater than or equal to \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}{Addition}} \tn % Row 0 \SetRowColor{LightBackground} string + string & combine 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{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Subtraction}} \tn % Row 0 \SetRowColor{LightBackground} String - String & crash \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string-number & crash \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} number-number & math-subtraction \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Multiplication}} \tn % Row 0 \SetRowColor{LightBackground} String * String & crash \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{String*number} & print string for number times \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{number*number} & math-multiplication \tn % Row Count 5 (+ 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}{using a while loop to print each item in list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{wlist = {[}2,4,5,6,7,8{]} \newline index = 0 \newline while index \textless{} len(wlist): \newline print (wlist{[}index{]}) \newline index = index +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}{Area of Circle Code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{while True: \newline user\_radius = input("What is the radius?") \newline radius = float(user\_radius) \newline pi = 3.1415 \newline area= pi radius * 2 \newline print ("The area of the circle is", area)} \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}{Area of a Triangle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def areaOfTriangle (base,height): \newline return base heigh 0.5 \newline base = float(input('Enter the base of the triangle')) \newline height = float(input(input('Enter the height of the triangle: ')) \newline print('The area of the triangle is',areaOfTriangle(base,height)) \newline def volumeOfPrism (area,height): \newline return areaOfPrism* height \newline base = float(input('Enter the area of the prism')) \newline height = float(input('Enter the height of the prism: '))} \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}{Definition program code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def printDefinitions(word): \newline if word == "variable": \newline print( 'A variable is things that able to change') \newline elif word == "function": \newline print( "A function is to help to use a code") \newline elif word == "variable": \newline print( 'A variable is the things that help you to change') \newline elif word == "return variable": \newline print( 'A return variable is something that return the function back to you') \newline elif word == "argument": \newline print('A argument is something that give the function to you') \newline elif word == "parameter": \newline print('A parameter is something that give function') \newline elif word == "string": \newline print('A string is the text, number or anything that is list the characters') \newline else: \newline print('unknown word') \newline user\_word = input( "Enter a word to define: ") \newline print(Definitions(user\_word))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Function}} \tn % Row 0 \SetRowColor{LightBackground} print( ) & displays information on the screen \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} input( ) & receives information from the user \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} int( ) & converts a value to an integer \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} float( ) & converts a value to a decimal number \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} str( ) & converts a value to a string \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} while... : & loop statement \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} if ... : & if statement used as a condition or loop in python \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} else : & another condition used after if statement \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} """ & multi-line comment \tn % Row Count 16 (+ 1) % Row 9 \SetRowColor{white} \#... & a line comment \tn % Row Count 17 (+ 1) % Row 10 \SetRowColor{LightBackground} for ... in ... & a list \tn % Row Count 19 (+ 2) % Row 11 \SetRowColor{white} True & a condition in a loop \tn % Row Count 20 (+ 1) % Row 12 \SetRowColor{LightBackground} False & a condition in a loop \tn % Row Count 21 (+ 1) % Row 13 \SetRowColor{white} len( ) & length of the string \tn % Row Count 22 (+ 1) % Row 14 \SetRowColor{LightBackground} ... {[} x {]} & the x'th letter of the string \tn % Row Count 24 (+ 2) % Row 15 \SetRowColor{white} import ... & import a codeor something like formula in python \tn % Row Count 26 (+ 2) % Row 16 \SetRowColor{LightBackground} \seqsplit{random.choice(...)} & to random item from the list \tn % Row Count 28 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Exponent}} \tn % Row 0 \SetRowColor{LightBackground} string**string & crash \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string**number & crash \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} number**number & math-exponent \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.68758 cm} x{2.28942 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Division}} \tn % Row 0 \SetRowColor{LightBackground} String / String & crash \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} String/number & crash \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Number/number & math-division \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}{Palindrome}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{while True: \newline def palindrome(word): \newline reverse = "" \newline myresult = "" \newline for letters in word: \newline reverse = letters + reverse \newline if word == reverse : \newline return True \newline else: \newline return False \newline reverse = "" \newline word = input("please enter a word: ") \newline if word == "quit": \newline break \newline theresult = palindrome(word) \newline print("This word has",len(word),"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}{Random code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import random \newline mylist = {[}'Dog','Fish', 'Cat', 'Bear'{]} \newline counter = 0 \newline while counter \textless{} 10: \newline random\_item = random.choice(mylist) \newline print (random\_item) \newline counter = counter + 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}{Guessing Game code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import random \newline mylist = {[}'beagle','pomeranian','pug'{]} \newline score = 0 \newline chances = 3 \newline start\_over = 0 \newline random\_item = \newline random.choice(mylist) \newline while start\_over \textless{} 1: \newline print \seqsplit{("-=-=-=-=-=-=-=-=-=-=-=-")} \newline print ("Guessing Game") \newline print \seqsplit{("-=-=-=-=-=-=-=-=-=-=-=-")} \newline print("words:", mylist) \newline guess = input("Guess a word: ") \newline if (guess in mylist): \newline if(guess == random\_item ): \newline print("That's correct!") \newline score = score + 100 \newline print("Score:", score) \newline start\_over = 2 \newline else: \newline print("Sorry, wrong choice! ") \newline chances = int(chances) -1 \newline else: \newline print("Sorry, that is not even in the list") \newline chances = int(chances) -1 \newline if(chances \textgreater{} 0): \newline print("Chances remaining:",chances) \newline else: \newline start\_over = 2 \newline print("Game Over! The word was ", random\_item) \newline print("Chance remaining:", chances) \newline print("Final score:", score)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.34379 cm} x{3.63321 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Vocabulary}} \tn % Row 0 \SetRowColor{LightBackground} variable & holds a value and can be changed \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} string & a list of characters such as numbers, letters, symbols \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} floating number & number with a decimal point \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} integer & number with no decimal point \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} input & something that the user types in \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} syntax & grammar or rules on programming \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} loop & the condition used in python \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} operator & the signs used for mathematics condition \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} module & text for storing the python code \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Change the text}} \tn % Row 0 \SetRowColor{LightBackground} ( ... . upper( ) ) & change the text to upper case \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} ( ... .lower( ) ) & change the text to lower case \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} ( ... . capitalize( ) ) & change the first letter of the text to upper case and convert other letters to lower case \tn % Row Count 8 (+ 4) % Row 3 \SetRowColor{white} ( ... . title( ) ) & change the first letter of each word from the text to upper case and convert other letter to lower case \tn % Row Count 13 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{print number in separate line in list mylist}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mylist = {[}1,2,3,4,5{]} \newline for number in mylist: \newline print (number)} \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}{number to binary code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = "" \newline while user\_number != "0": \newline user\_number = input ( "enter a number" ) \newline number = int(user\_number) \newline binary\_string = "" \newline while (number \textgreater{} 0 ):\#the number is greater than 0 \newline remainder = number \% 2 \newline binary\_string = str( remainder)+ binary\_string \newline number = number//2 \newline print (number) \newline print ( "binary string is ", \newline binary\_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}{Count down code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number= input("enter number") \newline number = int(user\_number) \newline countdown\_string = "" \newline while number \textgreater{} 0: \newline countdown\_string = countdown\_string + str(number) + "" \newline number = number-1 \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}{Number printing( for loop)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{for number in range(5): \newline print (number) \newline \newline \# the output will be 0-4 in separate lines} \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}{Quit word (def code)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# create a function that allows a user to create a list \newline \#function name: word \newline \#paramater: word \newline \#return the list \newline def createList (quitword): \newline mylist = {[} {]} \#create an empty list \newline while True: \newline \#get the item from the user \newline item = input('Please enter a list item') \newline \# when the user enters an item that is equal to quitword \newline if item == quitword: \newline return mylist \newline \# check if the list already in the list \newline duplicateword = False \newline \# figure out if the word isalready in the list \newline for word in mylist: \newline if item == word: \newline duplicateword = True \newline if duplicateword == True: \newline print ('Duplicate word!') \newline else: \newline \# add this item to the end of the list \newline mylist.append(item) \newline \#function call \newline mylist = creatList("stop") \newline print(mylist)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}