\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{Chutima Rakyu} \pdfinfo{ /Title (python-nae.pdf) /Creator (Cheatography) /Author (Chutima Rakyu) /Subject (Python Nae 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}{8B14A3} \definecolor{LightBackground}{HTML}{F7F0F9} \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 Nae Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Chutima Rakyu} via \textcolor{DarkBackground}{\uline{cheatography.com/25741/cs/7574/}}} \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}Chutima Rakyu \\ \uline{cheatography.com/chutima-rakyu} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 20th March, 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{User as float, print half of the that number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = float(input('enter the number:')) \newline print (user\_number/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}{list,print all element from the list using loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# for loop solution \newline mylist = {[}1,2,3,4,5{]} \newline for number in mylist: \newline print(number) \newline \newline \# while loop solution \newline mylist = {[}1,2,3,4,5{]} \newline num = 0 \newline while number \textless{} len(mylist): \newline print(mylist(number)) \newline number = number+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}{print Fibonacci series between 0 to 50 using loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{num1 = 0 \newline num2 = 1 \newline fibonacci = num1 + num2 \newline myoutput = " 0,1" \newline while fibonacci \textless{}50: \newline myoutput = myoutput + "," + str(fibonacci) \newline num1 =num2 \newline num2 =fibonacci \newline fibonacci = num1 + num2 \newline print(myoutput)} \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}{The areaOfEllipse using pir2r1 ( two parameter)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def areaOfEllipse(radius1,radius2): \newline pi = 3.1415 \newline area = pi{\emph{radius1}}radius2 \newline return area \newline area1= areaOfEllipse(2,3) \newline print(area1)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{User\_input}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{user\_input = input("Enter a value: ")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Comparing Values:}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{When you compare two values, the result is a Boolean \newline (True or False) E.g. 2 == 3 is False \newline  == is equal to \newline  != is not equal to \newline  \textless{} less than \newline  \textless{}= less than or equal to \newline  \textgreater{} greater than \newline  \textgreater{}= greater than or equal to \newline  and \newline  or \newline  not \newline True or anything is always True \newline False and anything is always False} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Combining Strings (Concatenation)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{"hi" + "there" == "hithere" \newline "hi" * 5 == "hihihihihi"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Converting between different data types:}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{word = str(3) \#converts 3 to a string "3" \newline num = int("3.5") \#converts "3.5" to an integer 3 \newline num = float("3") \#converts "3" to a float 3.0} \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}{The output of follwing code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Y = True \newline print (not y or 2 \textless{} 3) output = False} \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 alleven numbers from 1 100 using while loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{number = 0 \newline while number \textless{} 100: \newline number =number + 2 \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}{use a for loop to print the following}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{0 \newline 01 \newline 012 \newline 0123 \newline 01234 \newline mystring = "" \newline for number in range(5):0 \newline mystring = mystring + str(number) \newline print (mystring) \newline \# range(2) = 01} \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}{receive input from user, convert integer,print , 5}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{number = int(input("Enter a number:")) \newline print(num*5)} \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}{output of the program}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mystring = "" \newline count = 0 \newline while count \textless{} 5: \newline mystring = mystring + str(count) \newline print(mystring) \newline count = count + 1 \newline 0 \newline 01 \newline 012 \newline 0123 \newline 01234} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Data types}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{String - a list of characters e.g. "abc123\$\%\textasciicircum{}", or \newline empty string "" \newline Integer - whole numbers, and negative numbers e.g. -5, \newline 0, 2 \newline Floating Point - decimal numbers e.g. 1.5, 2.0, -2.99 \newline Boolean - True or False} \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 values:}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{print("hello", "there") \#displays hello there \newline print("hello" + "there") \#displays hellothere} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Comments}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\# hashtag – everything after \# is a comment not code \newline """ \newline Double quote - Multi-line comment, everything in \newline between three double quotes is a comments \newline """ \newline ''' Single quote - Multi-line comment, everything in \newline between three single quotes is a comments '''} \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}{the loop doesn't go forever}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{gameover = 0 \newline while (gameover ==0): \newline \newline print("hello") \newline gameover = 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}{Receive number from user, number is determine by 3}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = int(input("Enter the number:")) \newline remainder = user\_number\%3 \newline if remainder == 0: \newline print (user\_number, "is divisible by 3") \newline else: \newline print (user\_number, "is not visible by 3")} \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}{MultiplicationTable}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def multiplicationTable(): \newline user\_number = input( "Enter the number:") \newline number = int(user\_number) \newline count = 1 \newline \newline While count \textless{}=10: \newline print(number, "{\emph{",count,"=",num}}count) \newline count = count + 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}{Making number is negative, Zero or positive}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = int(input("Enter a number")) \newline if user\_number \textgreater{} 0: \newline print(user\_number,"is positive") \newline elif user\_number \textless{} 0: \newline print(user\_number,"is negative") \newline else: \newline print(user\_number,"is equal to zero")} \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 all even number from -100 to -1 (while loop)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number= -100 \newline while user\_number\textless{} -1: \newline print(user\_number) \newline user\_number= user\_number + 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}{positive integer,negative print HM odd, even}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{evencount = 0 \newline oddcount = 0 \newline while True: \newline number = int(input("Enter a number")) \newline if number \textless{} 0: \newline print ("Even number",evencount) \newline print ("odd number", oddcount) \newline break \newline else: \newline if (number\%2) == 0: \newline evencount = evencount + 1 \newline else: \newline oddcount = oddcount + 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}{Basic Math Operations:}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{+ addition, - subtraction \newline / divide with answer as a float. E.g. 5/2 == 2.5 \newline // divide with answer as an integer. E.g. 5//2 == 2 \newline * multiply \newline {\bf{ exponent. E.g. 2 power 3 == 2 }} 3 \newline \% modulo. Gives the remainder when dividing \newline e.g. 33 \% 10 == 3 \newline All math operations use the sa} \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 out fifth character from the variable}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{myword = "hellothere" \newline print(hellothere{[}4{]})} \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 integer and multiply by 10 loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{while True: \newline user\_number = int(input("enter a number") \newline number= int(user\_number)*10 \newline print(number)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}