\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{Pear Tan} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (Pear Tan) /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}{03EBDB} \definecolor{LightBackground}{HTML}{EFFDFC} \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}{Pear Tan} via \textcolor{DarkBackground}{\uline{cheatography.com/25842/cs/6964/}}} \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}Pear Tan \\ \uline{cheatography.com/pear-tan} \\ \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{0.89586 cm} x{4.08114 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Symbol}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{float()} & number with decimal point \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} str() & string \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} int() & integer \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} len() & the length of the word or string \tn % Row Count 4 (+ 1) \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}{Multiply}} \tn % Row 0 \SetRowColor{LightBackground} string * number & Repeat those thing for the number of time \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} string * string & Crash! \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} number * number & Multiply like in math \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Exponents}} \tn % Row 0 \SetRowColor{LightBackground} string ** number & Crash! \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} number ** number & Exponent in Math \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} number ** string & Crash! \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}{Rule for naming the variable}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Rule for naming variables}} \newline % Row Count 1 (+ 1) \# letters \newline % Row Count 2 (+ 1) \# numbers \newline % Row Count 3 (+ 1) \# underscore (\_) \newline % Row Count 4 (+ 1) \# can either start with letter or underscores ONLY \newline % Row Count 6 (+ 2) \# no space \newline % Row Count 7 (+ 1) {\bf{Example}} \newline % Row Count 8 (+ 1) Hello\_there \newline % Row Count 9 (+ 1) me2 \newline % Row Count 10 (+ 1) \_mynumber \newline % Row Count 11 (+ 1) {\bf{Invalid names}} \newline % Row Count 12 (+ 1) \# 3my =cannot start with number \newline % Row Count 13 (+ 1) \# last name = no spaces allowed \newline % Row Count 14 (+ 1) \# last-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}{Define Function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{varl = 1 \newline \_varl = 3 \newline \_varl + 100 \newline \newline print(\_varl) \newline \newline def bacon(): \# defines a functio named bacon \newline print ("hello it's bacon") \newline print ("line2") \newline print ("line3") \newline print ("line4") \newline print ("line5") \newline print ("line6") \newline print ("line7") \newline return \#exit the fuction \newline \newline bacon() \newline bacon() \newline bacon() \newline \newline def myprint (text): \newline print ("***"+ str(text) +"***") \newline return \newline \newline myprint(1) \newline myprint("hello") \newline myprint(2.5) \newline \newline def myprintnew (text, decoration): \newline print (decoration + str(text) + decoration) \newline return \newline \newline myprintnew (1,"+++") \newline myprintnew ('hello','-=-=-=-=-=-=-=-=') \newline myprintnew (1,"@@@@@@@") \newline \newline def doubleit(number): \newline return number {\emph{ 2 \newline \newline print (doubleit(3)) \newline \newline print (doubleit(doubleit(4))) \newline myvar = 12 \newline myvar = doubleit(myvar) \newline myvar = doubleit(myvar) \newline print(myvar) \newline {\bf{Result}} \newline 3 \newline hello it's bacon \newline line2 \newline line3 \newline line4 \newline line5 \newline line6 \newline line7 \newline hello it's bacon \newline line2 \newline line3 \newline line4 \newline line5 \newline line6 \newline line7 \newline hello it's bacon \newline line2 \newline line3 \newline line4 \newline line5 \newline line6 \newline line7 \newline {\bf{}}1}}{\emph{ \newline {\bf{}}hello}}{\emph{ \newline {\bf{}}2.5}}* \newline +++1+++ \newline -=-=-=-=-=-=-=-=hello-=-=-=-=-=-=-=-= \newline @@@@@@@1@@@@@@@ \newline 6 \newline 16 \newline 48} \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}{Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{firstname = input("What is your fisrt name?") \newline lastname = input("What is your last name?") \newline \newline fullname = firstname + " " + lastname \newline print(fullname) \newline \newline letternumber = input("What is the letter of number?") \newline \newline letternumber = int(letternumber) \newline \newline if letternumber\textgreater{}len(fullname): \newline print("Invalid letter number, try again") \newline else: \newline print(fullname{[}letternumber{]}) \newline \newline times = input("How many times to print the letter?") \newline times = int(times) \newline \newline if times\textgreater{}100: \newline print("Too many letters to print") \newline else: \newline print(fullname{[}letternumber{]}*times) \newline {\bf{Result}} \newline What is your fisrt name? Pear \newline What is your last name? Tan \newline Pear Tan \newline What is the letter of number? 4 \newline r \newline How many times to print the letter? 12 \newline rrrrrrrrrrrr} \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}{Example}} \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 \newline {\bf{Result}} \newline {[}1, 2, 3, 4, 5, 6{]} \newline {[}'shoe', 'bags', 'pants', 'shirt'{]} \newline {[}1, 'Hello', 2.5, True, 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}{word per line}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mystr = input(" Please enter your word") \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 {\bf{Result}} \newline Please enter your word1,2,3, \newline 1 \newline , \newline 2 \newline , \newline 3 \newline ,} \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 area of circle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{while True: \newline user\_radius = input("Enter the radius of the circles") \newline \newline radius = float(user\_radius) \newline \newline pi = 3.1415 \newline \newline \# \newline answer = pi * (radius**2) \newline \newline print("The area of the circle is",answer) \newline {\bf{Result}} \newline Enter the radius of the circles3 \newline The area of the circle is 28.273500000000002} \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}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{word = input("Please enter a string: ") \newline letter\_num = 0 \newline \newline reverse = "" \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}{Group work}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{""" \newline Group Members: Earn, Pop, Pear \newline \newline Class: 1005 \newline """ \newline import random \newline \newline \# create a list \newline \newline mylist = {[}'earn', 'pear', 'pop', 'jaja', 'roong'{]} \newline \newline random\_item = random.choice(mylist) \newline \newline \newline \#print \newline print("mylist:",mylist) \newline \newline \# ask the user to input the word \newline \newline chance = 5 \newline score = 0 \newline \newline \newline while chance!= 0: \newline user\_guess = input("Guess a word: ") \newline \newline if user\_guess == random\_item: \newline score = score + 100 \newline print("That's correct!","Score",score) \newline random\_item = random.choice(mylist) \newline \newline else: \newline chance = chance - 1 \newline print("Chances Remaining:",chance) \newline \newline \#check if that word is in the list \newline if user\_guess in mylist: \newline print ("Sorry, wrong choice") \newline else: \newline print("Sorry, that is not even in the list!") \newline \newline if chance == 0: \newline print(random\_item) \newline print("Final score:",score) \newline {\bf{Result}} \newline mylist: {[}'earn', 'pear', 'pop', 'jaja', 'roong'{]} \newline Guess a word: pear \newline Chances Remaining: 4 \newline Sorry, wrong choice \newline Guess a word: earn \newline Chances Remaining: 3 \newline Sorry, wrong choice \newline Guess a word: pop \newline Chances Remaining: 2 \newline Sorry, wrong choice \newline Guess a word: jaja \newline Chances Remaining: 1 \newline Sorry, wrong choice \newline Guess a word: roong \newline That's correct! Score 100 \newline Guess a word: jaja \newline Chances Remaining: 0 \newline Sorry, wrong choice \newline pear \newline Final score: 100} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.84609 cm} x{4.13091 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Function}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{input()} & information that receive from user \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{print()} & show information in the screen \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Addition or Plus}} \tn % Row 0 \SetRowColor{LightBackground} string + string & combine those strings together \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} string + number & program will be crash \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} number + number & add together like doing math \tn % Row Count 6 (+ 2) \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}{Symbol}} \tn % Row 0 \SetRowColor{LightBackground} + & plus or add \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} - & subtract \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} * & multiply \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} ** & exponent \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} / & divide and quotient (result) is float \tn % Row Count 6 (+ 2) % Row 5 \SetRowColor{white} // & divide and quotient (result) is integer \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} \% & remainder (modulo) \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} == & equal to \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} != & not equal to \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} \textless{}= & less than or eqaul to \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} \textless{} & less than \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} \textgreater{} & more than \tn % Row Count 14 (+ 1) % Row 12 \SetRowColor{LightBackground} \textgreater{}= & more than or equal to \tn % Row Count 15 (+ 1) % Row 13 \SetRowColor{white} \# & one line comment that will not included in the code \tn % Row Count 17 (+ 2) % Row 14 \SetRowColor{LightBackground} """ & Multi-line comment \tn % Row Count 18 (+ 1) % Row 15 \SetRowColor{white} True or anything & Always true \tn % Row Count 20 (+ 2) % Row 16 \SetRowColor{LightBackground} False and anything & False \tn % Row Count 22 (+ 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}{The area of circle 2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def areaOfCircle (user\_radius): \newline \newline if user\_radius\textless{}=0: \newline return "Error: invalid radius" \newline \newline pi = 3.1415 \newline area = pi*(user\_radius{\emph{*2) \newline return area \newline user\_radius = float(input("Enter the radius: ")) \newline print('The area of the circle is', \seqsplit{areaOfCircle(user\_radius)} \newline \newline }}Enter the radius: 3{\emph{ \newline }}The area of the circle is 28.273500000000002*} \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}{Meaning of the word}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def printDefinition(word): \newline \# write a definition in your own words for the folllowing words: \newline \# use multi-line strings to print the definition \newline \#variable \newline if word == "variable": \newline print(""" \newline \newline A variable is thing that can be changed \newline \newline """) \newline elif word == "function": \newline \newline \#function \newline print (""" \newline \newline A function is a thing that reuse block or quote. \newline \newline """) \newline elif word == "parameter": \newline \newline \#parameter \newline print(""" \newline \newline A parameter is thing inside blacket of function \newline \newline """) \newline elif word == "agument": \newline \newline \#argument \newline print(""" \newline \newline A argument is the same thing as parameter. It is thinfg inside blacket f function \newline \newline """) \newline elif word == "function call": \newline \#function call \newline print(""" \newline \newline Function is the thing make fuction run. \newline \newline """) \newline elif word == "string": \newline \newline \#string \newline print(""" \newline \newline A string is a list of character \newline \newline """) \newline else: \newline print("unknown word") \newline while True: \newline word = input ("Enter the word") \newline printDefinition(word) \newline \newline {\bf{Result}} \newline Enter the wordvariable \newline \newline A variable is thing that can be changed \newline \newline Enter the wordfunction \newline \newline A function is a thing that reuse block or quote. \newline \newline Enter the wordagument \newline \newline A argument is the same thing as parameter. It is thinfg inside blacket f function \newline \newline Enter the wordfunction call \newline \newline Function is the thing make fuction run. \newline \newline Enter the wordstring \newline A string is a list of character \newline \newline Enter the wordpear \newline unknown word} \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}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_number = input("What is the number?" ) \newline \newline number = int(user\_number) \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) \newline \newline {\bf{Result}} \newline What is the number? 5 \newline 54321} \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{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import random \newline \newline \# Create a list of integers \newline intlist = {[}1,2,3,4{]} \newline random\_int = random.choice(intlist) \newline print(intlist,random\_int)\#print the entire list and the random item \newline \newline \newline \# Create a list of floating point numbers \newline fplist={[}1.1,2.2,3.3,4.4{]} \newline random\_fp = random.choice(fplist) \newline print(fplist,random\_fp) \newline \newline \# Create a list of strings \newline strlist={[}'phone','pencil','computer'{]} \newline random\_str = random.choice(strlist) \newline print(strlist,random\_str) \newline \newline mylist = {[}1,2,3,4,1.1,2.2,3.3,4.4,'phone','pencil','computer'{]} \newline random\_item = random.choice(mylist) \newline print(mylist,random\_item) \newline \newline \#create a list of follwing veraibles \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) \newline {\bf{Result}} \newline {[}1, 2, 3, 4{]} 3 \newline {[}1.1, 2.2, 3.3, 4.4{]} 3.3 \newline {[}'phone', 'pencil', 'computer'{]} phone \newline {[}1, 2, 3, 4, 1.1, 2.2, 3.3, 4.4, 'phone', 'pencil', 'computer'{]} pencil \newline {[}1, 2, 3{]} 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}{Guess game with random}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import random \newline \newline \# create a list \newline \newline mylist = {[}'lion','cheetah','panther','cougar','leopard'{]} \newline \newline random\_item = random.choice(mylist) \newline print(random\_item) \newline \newline \#print \newline print(mylist{[}0{]}) \newline \newline \# ask the user to input the word \newline user\_guess = input("Guess a word: ") \newline \newline if user\_guess == random\_item: \newline print("Correct") \newline else: \newline \newline \#check if that word is in the list \newline if user\_guess in mylist: \newline print("Yes, it is in the list") \newline else: \newline print("No, it is not in the list")} \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{LightBackground} \mymulticolumn{1}{x{5.377cm}}{myself= "hello123" \newline \newline numbers = {[}1,2,3,4,5,6{]} \newline print(numbers) \newline \newline shoppinglist = {[}'shoe','bags','pants','shirt'{]} \newline print(shoppinglist) \newline \newline mixed={[}1,'Hello',2.5, True, False{]} \newline print(mixed) \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(opal) \newline shoppinglist. append('ties') \newline print(shoppinglist) \newline \newline \newline out = 0 \newline \newline for mrtim in shoppinglist: \newline out=out + 1 \newline print(mrtim) \newline print (out) \newline \newline largelist = range(100) \newline \newline for num in largelist: \newline print(num)} \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}{Decision making}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{f 3 \textless{} 2: \#if statement must compare two Booleans \newline print ('3 is less than2 ') \newline elif 4 \textless{} 2: \#can have 0 or more elif statements \newline print ('4 is less than 2') \newline elif 5 \textless{} 2: \newline print ('5 is less than 2') \newline else: \#can have 0 or 1 else statement at the end \newline print ('none of the above are True')} \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}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def nameOfFunction(): \newline print ('This function has no parameters') \newline print ('This function has no return value') \newline return \# no value, just exits the function \newline \newline \#function call \newline nameOfFunction() \newline \newline \#function with 1 parameter/argument \newline def testFunction(param): \newline print ('This function has 1 parameter') \newline print (param) \newline \newline \#function call \newline testFunction ("this is the parameter value") \newline \newline \#function with 2 parameters and a return value \newline def function3(param1, param2): \newline print('This function has 2 parameters') \newline return param1 + param2 \# return value \newline \newline \#function call and store the result in a variable \newline returnValue = function3(2, 3) \newline print (returnValue)} \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}{Determine zero positive and negative}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{num = int (input("Enter a number") \newline if num\textgreater{}0: \newline print (num,"is positive") \newline elif num\textless{}0: \newline print (num,"is negative") \newline else: \newline print (num,"is 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}{Divisible by 3}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{num = int(input(" Enter a word")) \newline remainder = num\%3 \newline if remainder ==o: \newline print(num,"is divisible by 3") \newline else: \newline print(num,"is not divisible 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}{Fibonacci fron o to 50}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{num1 = 0 \newline num2 = 0 \newline fibonacci = num1+num2 \newline myoutput = "0,1" \newline while fibonacci \textless{} 50: \newline myoutput = myouput + "," + str(fibonacci) \newline num1=num2 \newline num2 = fibonacci \newline fibonacci = num1+ num2 \newline print(my output) \newline {\emph{0,1,1,2,3,5,8,13,...}}} \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}{Sample}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def test() : \newline while True: \newline user\_input = input("Please enter a word: ") \newline if user\_input == 'quit': \newline break \newline \newline return \newline test() \newline {\emph{keep asking word till input quit}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \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} & A value or thing that can be changed \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{string} & A list of character such as letter or symbol \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{boolean} & True False \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{modulo} & Find the remainder \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{syntax} & There are error such as grammar or structure of language \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} float & A number with decimal point \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} \seqsplit{integer} & Rounded number which do not have decimal point \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Condition}} \tn % Row 0 \SetRowColor{LightBackground} while... & While this is true loop the command under the conditional \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} while True & Forever loop \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} 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 9 (+ 5) % Row 3 \SetRowColor{white} If...: then... else & If the statement in 'if ' is true, it will follow the statement in 'then'. If it is not, it will follow statement under 'else'. \tn % Row Count 15 (+ 6) \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 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 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)) \newline \newline \# write a function that returns the largest value \newline \# name: maxlist \newline \#argument : list \newline \#returns the largest value in the list \newline \newline def maxlist(list): \newline maxvalue = list(0) \newline \newline for item in list: \newline if item \textgreater{} maxvalue: \newline value = item \newline \newline return maxvalue \newline \newline mylist = {[}1,2,3,4,55,66,777,0,1{]} \newline print (maxlist(list))} \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 Triangle \& Volume of prism}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# write a function that computes the area of triangle \newline \newline \# name : areaOfTriangle \newline \# parameters :b,h \newline \# return : area \newline \newline def areaOfTriangle(b,h): \newline if user\_base\textless{}=0: \newline return "Error: invalid radius" \newline if user\_height\textless{}=0: \newline return "Error: invalid radius" \newline area = 0.5{\emph{ b}} h \newline return area \newline \newline \newline user\_base =float(input('Enter the base of the triangle:')) \newline user\_height = float(input('Enter the height of the triangle: ')) \newline print ('The area of the triangle is',areaOfTriangle(user\_base,user\_height)) \newline \newline \#write a function that computes the volume of a prism \newline \# name: volumeOfPrism \newline \# parameters :b,h,l \newline \# return : volume \newline \newline def volumeOfPrism(b,h,l): \newline volume = b{\emph{h}}l \newline return volume \newline user\_length = float(input('Enter the length of the prism:')) \newline \newline print('The volume of the prism is', volumeOfPrism(user\_base,user\_height,user\_length)) \newline \newline \newline {\bf{Result}} \newline Enter the base of the triangle:12 \newline Enter the height of the triangle: 6 \newline The area of the triangle is 36.0 \newline Enter the length of the prism:3 \newline The volume of the prism is 216.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}{Useful Function for Name}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mystr = "hello THERE" \newline \newline print (mystr.upper()) \textgreater{} HELLO THERE \newline print (mystr.lower()) \textgreater{} hello there \newline print (mystr.capitalize()) \textgreater{} Hello there \newline print (mystr.title()) \textgreater{} Hello There} \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}{How to 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) \newline {\bf{Result}} \newline Please enter a number 36 \newline Binary string is 100100} \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}{Guess a word}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# create a list \newline \newline mylist = {[}'lion','cheetah','panther','cougar','leopard'{]} \newline \newline \#print \newline print(mylist{[}0{]}) \newline \newline \# ask the user to input the word \newline user\_guess = input("Guess a word: ") \newline \newline \#check if that word is in the list \newline if user\_guess in mylist: \newline print("Yes, it is in the list") \newline else: \newline print("No, it is not in the list")} \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}{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}{Reverse}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{word = input("Please enter a word to reverse: ") \newline \newline letter\_num = 0 \newline \newline reverse = "" \newline \newline while letter\_num \textless{}len(word): \newline reverse = word{[}letter\_num{]} + reverse \newline letter\_num = letter\_num + 1 \newline \newline \newline print("Reverse: ",reverse) \newline {\bf{Result}} \newline Please enter a word to reverse: 0123456 \newline Reverse: 6543210} \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}}{\#creates a list of numbers from 0 to the specified \newline number \newline numberlist = range(5) \newline \# is the same as creating the following list \newline numberlist2 = {[}0, 1, 2, 3, 4{]} \newline 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}{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) \newline \newline print all items in the list} \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}{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}{Lists}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mylist = {[}2,3,4,5{]} \# create a list \newline \newline \#select an item from a list \newline print (mylist{[}0{]}) \#selects first item and displays 2 \newline \newline \# len() determines the length of the list \newline print (len(mylist)) \# displays 4 \newline \newline mylist.append(5) \# adds an item to the end of the list} \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}{Condition while loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{count = 0 \# start at zero \newline while count \textless{} 10: \# loop while count is less than 10 \newline print(count) \#will print numbers 0 - 9 \newline count = count + 1 \# must increase count} \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}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def palindrome(word): \newline letter\_num = 0 \newline reverse = "" \newline for letter\_num in word: \newline reverse = letter\_num + reverse \newline \newline if word == reverse: \newline return True \newline else: \newline return False \newline \newline while True: \newline user\_word = input("Please enter a word: ") \newline if user\_word != "quit": \newline print("This word has",len(user\_word),"letters") \newline if user\_word == "quit": \newline break \newline if palindrome(user\_word) == True: \newline print(user\_word,"is palindrome") \newline else: \newline print(user\_word,"is not palindrome") \newline {\bf{Result}} \newline Please enter a word: 321 \newline This word has 3 letters \newline 321 is not palindrome \newline Please enter a word: 212 \newline This word has 3 letters \newline 212 is palindrome \newline Please enter a word: quit} \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}{while loop with counting number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{num = -100 \newline while num\textless{} -1: \newline print(num) \newline num = num + 2 \newline \newline num = 0 \newline while num\textless{} 100: \newline num = num + 2 \newline print(num)} \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}{Example from sheet}} \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 \newline mystring = " " \newline for num in range(5): \newline mystring = mystring+ str(count) \newline print (mystring)g \newline {\bf{Result}} \newline 0 \newline 01 \newline 012 \newline 0123 \newline 01234} \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 count}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{evencount = 0 \newline oddcount = 0 \newline while True: \newline num = int (input("Enter a positive integer")) \newline if num \textless{} 0: \newline print ("Even numbers:",evencount) \newline print ("Odd numbers:",odd count) \newline break \newline else: \newline if (num\%2) == 0: \newline evencount = evencount + 1 \newline else: \newline oddcount = oddcount + 1 \newline \newline program that repeatedly recieve positive integers from the user. When the user enters a negative integer, exit the loop and print how many of the numbers were odd and even} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}