\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{jui19788} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (jui19788) /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}{FF36F2} \definecolor{LightBackground}{HTML}{FFE5FD} \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}{jui19788} via \textcolor{DarkBackground}{\uline{cheatography.com/25833/cs/6967/}}} \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}jui19788 \\ \uline{cheatography.com/jui19788} \\ \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 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}{x{0.84609 cm} x{4.13091 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Vocabulary}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{variable} & holds a value and can be changed \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{string} & a list of characters such as numbers, letters, symbols \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{integer} & whole number/counting number. 100, -52 \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} float & the number in decimal. 100.01, -52.5 \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{boolean} & true/false \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{modulo} & find the remainder \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \seqsplit{syntax} & grammar/structure of language \tn % Row Count 12 (+ 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}{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) - \_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) - 3my= "hi" -{}- cannot start with number \newline % Row Count 13 (+ 1) - first name = "hi" -{}- no spaces allowed \newline % Row Count 14 (+ 1) - first-name -{}- dashes are not accepted% Row Count 15 (+ 1) } \tn \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 & addition (math) \tn % Row Count 3 (+ 1) \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}{Random Stuff}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import random \newline \newline intlist = {[}1,2,3,4,5{]} \newline random\_int = random.choice(intlist) \newline print (intlist,random\_int) \newline \newline fplist = {[}3.5,4.02,5.55,9.65,7.02{]} \newline random\_fp = random.choice(fplist) \newline print (fplist,random\_fp) \newline \newline strlist = {[}'dog','cat','monkey','elephant','squirrel'{]} \newline random\_list = random.choice(strlist) \newline print (strlist,random\_list) \newline \newline mylist = {[}1,3.2,'snow'{]} \newline random\_item = random.choice(mylist) \newline print (mylist,random\_item) \newline \newline myvar1 = 1 \newline myvar2 = 2 \newline myvar3 = 3 \newline varlist = {[}myvar1,myvar2,myvar3{]} \newline random\_var = random.choice(varlist) \newline print (varlist,random\_var)} \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}{Largest number in the list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def maxlist(list): \newline maxvalue = list{[}0{]} \newline \newline for item in list: \newline if item \textgreater{} maxvalue: \newline maxvalue = item \newline \newline return maxvalue \newline \newline mylist = {[}1,2,3,4,55,66,777,0,1{]} \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}{Largest of two numbers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def max2(num1, num2): \newline maxvalue = num1 \newline \newline if num2 \textgreater{} maxvalue: \newline maxvalue = num2 \newline \newline return maxvalue \newline \newline print(max2(4,5)) \newline print(max2(33,5))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.94103 cm} x{3.03597 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() & change number to be decimal number \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} str() & a list of number, letter and symbols \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} len() & the length of the string \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \# & comment, no effect \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} \seqsplit{"""................."""} & multiple line comment \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 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} != & 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} + & 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) \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} \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}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = ' ' \newline \newline while True : \newline binary\_string = ' ' \newline user\_number = input ("Enter a number to convert to binary") \newline number = int (user\_number) \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}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Print Name}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{name = "tim GIRARD" \newline \newline print (name.upper()) → TIM GIRARD \newline print (name.lower()) → tim girard \newline print (name.capitalize()) → Tim girard \newline print (name.title()) → Tim Girard} \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}}{def areaOfCircle (r): \newline \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 \newline user\_radius = float(input('Enter the radius:')) \newline \newline print("The area of the circle is", \seqsplit{areaOfCircle(user\_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}{Largest of three values}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def max3(num1, num2, num3): \newline maxvalue = num1 \newline if (num1 \textgreater{} num2) and (num1 \textgreater{} num3): \newline maxvalue = num1 \newline elif (num2 \textgreater{} num1) and (num2 \textgreater{} num3): \newline maxvalue = num2 \newline else: maxvalue = num3 \newline \newline return maxvalue \newline \newline print (max3(1,2,50)) \newline print (max3(51,2,50)) \newline print (max3(1,255,50))} \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}{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}{Number to Hex}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = input("please enter a number: ") \newline number = int(user\_number) \newline hex\_string = ' ' \newline while (number \textgreater{} 0): \newline remaider = number \% 16 \newline if remaider == 10: \newline remaider = 'A' \newline elif remaider == 11: \newline remaider = 'B' \newline elif remaider == 12: \newline remaider = 'C' \newline elif remaider == 13: \newline remaider = 'D' \newline elif remaider == 14: \newline remaider = 'E' \newline elif remaider == 15: \newline remaider = 'F' \newline \newline hex\_string = str(remaider) + str(hex\_string) \newline number = number // 16 \newline print ("Hexadecimal string is 0x", hex\_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}{Area of Circle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{""" \newline Python Intro Assignment \#2 \newline name \newline student number \newline """ \newline \newline \#Ask the user for a radius of a circle \newline user\_radius = input("What is a radius of a circle?") \newline \newline \#Convert the given radius to a floating point \newline radius = float(user\_radius) \newline \newline \#Make a variable called pi \newline pi = float(3.1415) \newline \newline \#Calculate the area of the circle using exponents \newline area = pi{\emph{(radius}}*2) \newline \newline \#Display the area of the circle to the user \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}{Sort fruit list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{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) \newline \newline \newline for fruit in fruits: \newline print ("Fruit: ", fruit)} \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}{Bracket codes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def myprint(text): \newline print ("{\bf{{\emph{" + str(text) + "}}}}") \newline \newline return \newline \newline myprint("1) \newline myprint("hi") \newline myprint("2.5") \newline \newline def myprintnew(text, decoration): \newline print(decoration + str(text) + decoration) \newline \newline return \newline \newline myprintnew(1, "+++") \newline myprintnew('hello','||||||||||||||||') \newline myprintnew(1, "{\emph{-}}-{\emph{-}}-{\emph{-}}-{\emph{-}}")} \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 Definition (Loop)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def printDefinitions(word): \newline if word == "variable": \newline print (""" \newline \newline A variable is the vaule that can changes \newline \newline """) \newline \newline elif word == "function": \newline \newline print (""" \newline \newline A function is a block of code that can reuse \newline \newline """) \newline \newline elif word == "string": \newline print (""" \newline \newline A String is a list of characters \newline \newline """) \newline elif word == "parameter": \newline print (""" \newline \newline A Parameter is something you give to the function \newline \newline """) \newline elif word == "argument": \newline print (""" \newline \newline An Argument is thing that give to function \newline \newline """) \newline elif word == "function call": \newline print (""" \newline \newline A Function call is to tell the function/code to run \newline \newline """) \newline else: \newline print("Unknown word") \newline while True: \newline user\_input = input("Enter words: ") \newline \seqsplit{printDefinitions(user\_input)}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}