\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{Rawisara} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (Rawisara) /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}{79E5CB} \definecolor{LightBackground}{HTML}{EEFBF8} \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}{Rawisara} via \textcolor{DarkBackground}{\uline{cheatography.com/25834/cs/6960/}}} \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}Rawisara \\ \uline{cheatography.com/rawisara} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 11th 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.14471 cm} x{3.83229 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Vocabulary}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Modulo(\%)} & Finding the remainder \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Syntax & Grammar/ Structure of language \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} / & divide(float) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} // & divide(integer) \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.88206 cm} p{1.09494 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Comparing Values}} \tn % Row 0 \SetRowColor{LightBackground} True or anything & True \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} False and anything & False \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.63321 cm} p{1.34379 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Multiplication and Exponents}} \tn % Row 0 \SetRowColor{LightBackground} string* string & Fail!! \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string ** string & Fail!! \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} string ** number & Fail!! \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}{Random}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{import random \newline % Row Count 1 (+ 1) int/fp/strlist = {[}1,2,3,4,5,6,7,8,9,0{]} \newline % Row Count 2 (+ 1) random\_int/fp/str = \seqsplit{random.choice(int/fp/strlist)} \newline % Row Count 3 (+ 1) print(int/fp/strlist,random\_int/fp/str) \newline % Row Count 4 (+ 1) \#fp = float ,str = string \newline % Row Count 5 (+ 1) fplist = {[}4.6,3.2,7.7,6.2{]} \newline % Row Count 6 (+ 1) strlist = {[}'uik','lok','pki','roo'{]} \newline % Row Count 7 (+ 1) mylist = {[}589,56.3,'suay'{]} \newline % Row Count 8 (+ 1) random\_mylist = random.choice(mylist) \newline % Row Count 9 (+ 1) print(mylist,random\_mylist)% Row Count 10 (+ 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}{Palindrome function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_input = input("write down string") \newline print(input) \newline \newline letter\_num = 0 \newline reverse = "" \newline while letter\_num \textless{} len(user\_input): \newline reverse = user\_input{[}letter\_num{]} + reverse \newline letter\_num = letter\_num + 1 \newline \newline print ("Reverse: ",reverse) \newline \newline if reverse == user\_input: \newline print("This is palindrome") \newline else: \newline print("This 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}{Guessing Game}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import random \newline mylist = {[}'lion', 'cheetah', 'panther', 'cougar', 'leopard'{]} \newline random\_item = random.choice(mylist) \newline Chances = 5 \newline Score = 0 \newline \newline while Chances \textgreater{} 0: \newline print("Words:{[}'lion', 'cheetah', 'panther', 'cougar', 'leopard'{]}") \newline \newline \newline user\_guess = input("Guess a word: ") \newline if user\_guess == random\_item: \newline print("That's correct!") \newline Score = Score+100 \newline print("Chances remaining",'',Chances) \newline random\_item = random.choice(mylist) \newline print("Score is",'',Score) \newline else: \newline \newline if user\_guess in mylist: \newline print("Sorry, wrong choice!") \newline Chances = Chances - 1 \newline print("Chances remaining",'',Chances) \newline print("Score is",'',Score) \newline else: \newline print("Sorry, that is not even in the list!") \newline Chances = Chances - 1 \newline print("Chances remaining",'',Chances) \newline print("Score is",'', Score) \newline \newline if Chances == 0: \newline print("Gameover",'',"The word is",'',random\_item) \newline print("Final score is",'',Score)} \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}{Printing Value}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{print("hello", "there") \#displays hello there \newline % Row Count 1 (+ 1) print("hello" + "there") \#displays hellothere% Row Count 2 (+ 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}{Combining Strings (Concatenation)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{"hi" + "there" == "hithere" \newline % Row Count 1 (+ 1) "hi" * 5 == "hihihihihi"% Row Count 2 (+ 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}{List}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{mylist.append(5) \#add at the end of the list% Row Count 1 (+ 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}{Range}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{for num in range(100): \newline print (num) \# prints all numbers from 0 – 99 \newline for num in range(5, 50): \newline print(num) \#prints all numbers from 5 - 49} \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}{in}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{if user\_guess in mylist: \newline print("Sorry, tht's wrong") \newline else: \newline print("Sorry It is not in choice")} \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}{Comment}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\# comment \newline % Row Count 1 (+ 1) """ \newline % Row Count 2 (+ 1) Double quote \newline % Row Count 3 (+ 1) for 2 line comment \newline % Row Count 4 (+ 1) """ \newline % Row Count 5 (+ 1) ''' Single quote - Multi-line comment, '''% Row Count 6 (+ 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}{Find the area of prism}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def volumeofprism(b,h,l): \newline volume = areaofTriangle(b,h) * l \newline return volume \newline user\_length = float(input('Enter the length of the prism: ')) \newline print ("The volume of the prism is", volumeofprism(user\_base,user\_height,user\_length))} \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}{Print even number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{num = 2 \newline while 1 \textless{} num and num \textless{}101 : \newline print(num) \newline num = num + 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}{Function Largest Value}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def max3 (num1,num2,num3): \newline if num1\textgreater{}num2 and num1\textgreater{}num3: \newline largestvalue = num1 \newline elif num2\textgreater{}num3 and num2\textgreater{}num1: \newline largestvalue = num2 \newline else: \newline largestvalue = num3 \newline return largestvalue \newline \newline print (max3(9,100,25)) \newline print (max3(69,85,1)) \newline print (max3(75,9,33)) \newline \newline \newline \newline def maxlist (list): \newline largestvalue = list {[}0{]} \newline for item in list: \newline if item \textgreater{} largestvalue: \newline largestvalue = item \newline return largestvalue \newline mylist = {[}1,2,3,4,103,100,89,57{]} \newline print (maxlist(mylist))} \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}{Palindrome2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def isPalindrome(word): \newline letter\_num = 0 \newline reverse = "" \newline \newline while letter\_num \textless{} len(user\_word): \newline reverse = user\_word{[}letter\_num{]} + reverse \newline letter\_num = letter\_num + 1 \newline \newline \newline if word == reverse: \newline return True \newline else: \newline return False \newline \newline while True: \newline user\_word = input("Enter the word") \newline word = len(user\_word) \newline print(word) \newline if user\_word == "quit": \newline break \newline if \seqsplit{isPalindrome(user\_word):} \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}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Capital letter}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{name = "tim GIRARD" \newline % Row Count 1 (+ 1) print (name.upper()) → TIM GIRARD \newline % Row Count 2 (+ 1) print (name.lower()) → tim girard \newline % Row Count 3 (+ 1) print (name.capitalize()) → Tim girard \newline % Row Count 5 (+ 2) print (name.title()) → Tim Girard% Row Count 6 (+ 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}{Naming Conventions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Rule for giving name \newline % Row Count 1 (+ 1) - letter \newline % Row Count 2 (+ 1) - numbers \newline % Row Count 3 (+ 1) - underscore \_ \newline % Row Count 4 (+ 1) Valid name \newline % Row Count 5 (+ 1) - \_myStr \newline % Row Count 6 (+ 1) - my3 \newline % Row Count 7 (+ 1) - Hello\_there \newline % Row Count 8 (+ 1) Invalid name \newline % Row Count 9 (+ 1) - 3my="hi" -{}- cannot start with number \newline % Row Count 10 (+ 1) - first name="hi" \newline % Row Count 11 (+ 1) - first-name \newline % Row Count 12 (+ 1) - first+name% Row Count 13 (+ 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}{Countdown Number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = input("Please enter the 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}{Reverse Number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{word = input("enter the word") \newline letter\_num = 0 \newline reverse = "" \newline while letter\_num \textless{} len(word): \newline reverse = word{[}letter\_num{]} + reverse \newline letter\_num = letter\_num + 1 \newline \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}{Convert to Binary String}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = input ("Please enter the number") \newline number = int(user\_number) \newline binary\_string = "" \newline while (number\textgreater{}0): \newline remainder = number\%2 \newline binary\_string = str(remainder) + (binary\_string) \newline number = number//2 \newline print ("Binary string is", 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}{Sort word per line}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{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 \newline \newline H \newline e \newline l \newline l \newline o} \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}{Define: function 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*r**2 \newline return area \newline user\_radius = input ('Enter the radius') \newline r = float(user\_radius) \newline print("The area of circle is",areaofcircle(r))} \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}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def printdefinitions(word): \newline if word == "Variable": \newline print ('lo') \newline \newline elif word == "Function": \newline print ('fun') \newline \newline elif word == "Parameter" or word == "Argument": \newline print ('para') \newline \newline elif word == "Function call": \newline print ('call') \newline \newline elif word == "String": \newline print ("stri") \newline \newline else: \newline print ("Unknown Word") \newline return \newline \newline while True: \newline user\_input = input ("Enter the word:") \newline \seqsplit{printdefinitions(user\_input)}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}