\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{Apisara1999} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (Apisara1999) /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}{A31E0A} \definecolor{LightBackground}{HTML}{FCF7F7} \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}{Apisara1999} via \textcolor{DarkBackground}{\uline{cheatography.com/25844/cs/6965/}}} \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}Apisara1999 \\ \uline{cheatography.com/apisara1999} \\ \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.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Vocabulary}} \tn % Row 0 \SetRowColor{LightBackground} Variable & Hold a value, can be change \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} string & A list of number/letter/symbols \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Float number & Whole number/counting number \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} integer number & The number in decimal \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} boolean & True/False \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} Modulo & Fine the remainder \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} syntax & Grammar/structure of lauguage \tn % Row Count 11 (+ 2) % Row 7 \SetRowColor{white} length & the length of the string \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.74655 cm} x{4.23045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Function}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{print()} & Show information that you want on the screen \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{input()} & Gain information from user \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{Float()} & Change to the decimal number \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} int() & change to the number integer \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} str() & A list of number/letter/symbols \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} len() & The length of the string \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} \# & To note, no effect \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} "" & Multi-line comment \tn % Row Count 11 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.14011 cm} x{2.83689 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Conditionals}} \tn % Row 0 \SetRowColor{LightBackground} If..... :then..... else....... & If the statement is true then do command under then else do command under else \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} while...... & While this is true loop the command under the conditional \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} While True & loops forever \tn % Row Count 8 (+ 1) % Row 3 \SetRowColor{white} for each item in name of list & For every item in the list repeat the command under the loop that many times. (a string is a list too) \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}{Naming Conventions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Rules for naming variable:}} \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 letters or 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Example8-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 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}{Example13- def printDefinitions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def printDefinitions(word): \newline \newline \#variable \newline \newline \newline if word == "variable": \newline print (""" \newline A varible is value that can be change \newline """) \newline \newline \newline \#function \newline \newline elif word == "function": \newline print (""" \newline A function is block of quote can be reused \newline """) \newline \newline \newline \#parameter \newline \newline elif word == "parameter": \newline print (""" \newline A parameter is value that inside the blacket of the function \newline """) \newline \newline \newline \#argument \newline \newline elif word == "argument": \newline print (""" \newline A argument is value that inside the blacket of the function \newline """) \newline \newline \newline \# function call \newline \newline elif word == "function call": \newline print (""" \newline A function call is something that make the function run \newline """) \newline \newline \newline \newline \#string \newline \newline elif word == "string": \newline print (""" \newline A string is list of character \newline """) \newline \newline \newline else: \newline print("unknown word") \newline \newline return \newline \newline user\_input=input("Enter word") \newline \newline printDefinitions(user\_input)} \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}{Example17-The largest value}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#write a function that returns the largest of two values \newline \#name : max2 \newline \#agruments: num1, num2 \newline \# return: largest value \newline \newline \# write a functrion that returns the largest of three values \newline \# name : max3 \newline \#agrument: num1, num2, num3 \newline \# return: largest value \newline \newline def max2(num1,num2): \newline if num1 \textgreater{}= num2: \newline max\_value = (num1) \newline if num2 \textgreater{} num1: \newline max\_value = (num2) \newline return max\_value \newline \newline num1 = input('Enter the the first value') \newline num2 = input('Enter the the second value') \newline print (max2(num1,num2)) \newline \newline \newline \newline \newline \newline def max3(num1,num2,num3): \newline if num1 \textgreater{}= num2 and num1 \textgreater{}= num3: \newline max\_value = (num1) \newline if num2 \textgreater{} num1 and num2 \textgreater{}= num3: \newline max\_value = (num2) \newline if num3 \textgreater{}= num2 and num3 \textgreater{}= num1: \newline max\_value = (num3) \newline \newline return max\_value \newline \newline num3 = input('Enter the the third value') \newline print (max3(num1,num2,num3))} \tn \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} != & not equal to \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textless{} & less than \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textless{}= & less than or equal to \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \textgreater{} & greater than \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \textgreater{}= & greater 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{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} number * number & math (multiply) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} string * number & combines the strings multiple time \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) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Addition}} \tn % Row 0 \SetRowColor{LightBackground} string + string & squishes them 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.63781 cm} x{2.33919 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\emph{Important}}}} \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) % Row 2 \SetRowColor{LightBackground} range(5) = & {[}0,1,2,3,4{]} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} print("hello", "there") & \#displays hello there \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} print("hello" + "there") & \#displays hellothere \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} "hi" + "there" & == "hithere" \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} "hi" * 5 & == "hihihihihi" \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} while True: & \# forever \tn % Row Count 10 (+ 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}{While Loop with List:}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{thelist = {[}4, 3, 2, 1, 0{]} \newline index = 0 \# start at the first item \newline while index \textless{} len(thelist): \newline print (thelist{[}index{]}) \#prints each item \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}{For-Loop with List:}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{forlist = {[}3, 4, 5, 2, 1{]} \newline for item in forlist: \newline print(item)} \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}{Example5-print out each item in list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mystr = "hello123" \newline \newline numbers = {[}1,2,3,4,5,6{]} \newline print (numbers) \newline \newline shoppinglist = {[}'shoes','bags','pants','shirts'{]} \newline print (shoppinglist) \newline \newline mixed = {[}1, 'hello', 2.5, True, False{]} \newline print (mixed) \newline \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 for myletterisawesome in mystr: \newline print(myletterisawesome) \newline \newline for tientien in shoppinglist: \newline print(tiemtiem) \newline \newline out = 0 \newline \newline for mrtim in shoppinglist: \newline out = out + 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}{Example10-Def / function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def myprintnew(text, decoration): \newline print(decoration + str(text) + decoration) \newline return \newline \newline myprintnew(1, "+++") \newline myprintnew('hello', '-=-=-=-=-=-=-=-=') \newline myprintnew(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}{Example12-Circle area}} \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 \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}{Example11 - doubleit}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def doubleit(number): \newline \newline return number * 2 \newline \newline print (doubleit(3)) \newline \newline print (doubleit(doubleit(4))) \newline \newline myvar = 12 \newline myvar = doubleit(myvar) \newline myvar = doubleit(myvar) \newline print (myvar)} \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}{Example13.5- def printDefinitions + loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def printDefinitions(word): \newline \newline \#variable \newline \newline \newline if word == "variable": \newline print (""" \newline A varible is value that can be change \newline """) \newline \newline \newline \#function \newline \newline elif word == "function": \newline print (""" \newline A function is block of quote can be reused \newline """) \newline \newline \newline \#parameter \newline \newline elif word == "parameter": \newline print (""" \newline A parameter is value that inside the blacket of the function \newline """) \newline \newline \newline \#argument \newline \newline elif word == "argument": \newline print (""" \newline A argument is value that inside the blacket of the function \newline """) \newline \newline \newline \# function call \newline \newline elif word == "function call": \newline print (""" \newline A function call is something that make the function run \newline """) \newline \newline \newline \newline \#string \newline \newline elif word == "string": \newline print (""" \newline A string is list of character \newline """) \newline \newline \newline else: \newline print("unknown word") \newline \newline return \newline \newline \newline \newline {\bf{while True:}} \newline \newline {\bf{user\_input=input("Enter word")}} \newline \newline {\bf{printDefinitions(user\_input)}}} \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}{Example17.5-The largest value from list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#write the function that returns the largest number in a list \newline \#name: maxlist \newline \#argument:list \newline \#returns the largest value in the list \newline \newline \newline def maxlist(list): \newline maxvalue = list{[}0{]} \newline for item in list: \newline if item \textgreater{} maxvalue: \newline maxvalue = item \newline \newline return maxvalue \newline \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}{Example18- Palindrome+loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{while True : \newline user\_word = input("Enter your word") \newline \newline if user\_word != ("quit"): \newline print(len(user\_word)) \newline \newline if user\_word == ("quit"): \newline break \newline \newline \newline reverse = "" \newline letter\_num = 0 \newline \newline while letter\_num \textless{} len(user\_word): \newline reverse = user\_word{[}letter\_num{]} + reverse \newline letter\_num = letter\_num + 1 \newline if user\_word == reverse: \newline print (user\_word, "is palindrome") \newline else: \newline print (user\_word, "is not 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}{Example1-Spelling a string out in reverse code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{word = input("Type in an word: ") \newline reverse = "" \newline for letter in word: \newline reverse = letter + reverse \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}{Example2-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}{Example3-Countdown Code}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = input("Please enter a number: ") \newline \newline number = int(user\_number) \newline \newline countdown\_string = "" \newline \newline while number \textgreater{} 0: \newline countdown\_string = 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}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Example4-Print Name}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{name = {\emph{jaja YOOYUEN}} \newline % Row Count 1 (+ 1) print (name.upper()) -{}-{}- JAJA YOOYUEN \newline % Row Count 2 (+ 1) print (name.lower()) -{}-{}- jaja yooyuen \newline % Row Count 3 (+ 1) print (name.capitalize()) -{}-{}- Jaja yooyuen \newline % Row Count 4 (+ 1) print (name.title()) -{}-{}- Jaja Yooyuen% Row Count 5 (+ 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}{Example6}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{word = input("What is the word ?") \newline reverse = "" \newline \newline """ \newline letter\_num = 0 \newline \newline \newline while letter\_num \textless{} len(word): \newline reverse = word{[}letter\_num{]} + reverse \newline letter\_num = letter\_num + 1 \newline \newline """ \newline \newline for letter in word: \newline reverse = letter + reverse \newline \newline print ("Reverse: ",reverse) \newline \newline out = 0 \newline \newline for letter in word: \newline out = out + 1 \newline print(out)} \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}{Example7-Convert to binary}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = input("Please enter a number") \newline \newline number = int(user\_number) \newline \newline binary\_string ='' \newline while (number \textgreater{} 0): \newline remainder= number\%2 \newline binary\_string = str(remainder) + binary\_string \newline number= number//2 \newline \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}{Example9-Random and other}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import random \newline \newline inlist = (1,2,3,4,5,6,7) \newline random\_int = random.choice(inlist) \newline \newline print (inlist, '', random\_int) \newline \newline \newline \newline \newline \newline fplist = (1.1,2.1,3.1,4.1,5.1,6.1,7.1) \newline random\_fp = random.choice(fplist) \newline print (fplist, '', random\_fp) \newline \newline \newline strlist = ('love','captain','verymuch') \newline random\_str = random.choice(strlist) \newline print (strlist, '', random\_str) \newline \newline \newline mylist = (1,2,3,4,5,6,7,1.1,2.1,3.1,4.1,5.1,6.1,7.1,'love','captain','verymuch') \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}{Example14-reverse}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{reverse = "" \newline letter\_num = 0 \newline \newline word = input('type in a word: ') \newline \newline while letter\_num \textless{} len(word): \newline reverse = word{[}letter\_num{]} + reverse \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}{Example15-palindrome}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#create a function that will ask user for a string \newline \#and then say if that string is palindrome or not \newline \newline reverse = "" \newline letter\_num = 0 \newline \newline word = input('type in a word: ') \newline \newline while letter\_num \textless{} len(word): \newline reverse = word{[}letter\_num{]} + reverse \newline letter\_num = letter\_num + 1 \newline if word == reverse: \newline print ("It is palindrome") \newline else: \newline print ("It is not 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}{Example16- Area(Triangle) and volume (Prism)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#write a function that computes the area of triangle \newline \newline \#name: areOfTriangle \newline \#parameter: b, h \newline \#return: area \newline \newline \newline def areaOfTriangle(b, h): \newline if user\_base \textless{}= 0: \newline return "Error: invarid radius" \newline if user\_height \textless{}= 0: \newline return "Error: invarid radius" \newline \newline area = b * h / 2 \newline return area \newline \newline user\_base = float(input('Enter the base of the triangle: ')) \newline user\_height = float(input('Enter the height of the triangle: ')) \newline \newline \newline print ('The area of triangle is', \seqsplit{areaOfTriangle(user\_base}, user\_height)) \newline \newline \newline \newline \newline \#write a function that computes the volume of a prism \newline \#name: volumeOfPrism \newline \#return: volume \newline \newline def volumeOfPrism(b, h, l): \newline if user\_length \textless{}= 0: \newline return "Error: invarid radius" \newline \newline volume = b * h * l / 2 \newline return volume \newline \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}{Example18.5}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{''' \newline Apisara Yooyuen Jaja 5861004, 1005 \newline ''' \newline \newline def isPalindrome(word): \newline reverse = "" \newline letter\_num = 0 \newline \newline while letter\_num \textless{} len(user\_word): \newline reverse = user\_word{[}letter\_num{]} + reverse \newline letter\_num = letter\_num + 1 \newline \newline if word == reverse: \newline return True \newline else: \newline return False \newline \newline \newline \newline while True : \newline user\_word = input("Enter your word: ") \newline word = len(user\_word) \newline \newline \newline if user\_word == ("quit"): \newline break \newline \newline if \seqsplit{isPalindrome(user\_word):} \newline print(word) \newline print (user\_word, "is palindrome") \newline \newline else: \newline print(word) \newline print (user\_word, "is not palindrome")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}