\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{sura1234} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (sura1234) /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}{FFBD08} \definecolor{LightBackground}{HTML}{FFFAEF} \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}{sura1234} via \textcolor{DarkBackground}{\uline{cheatography.com/25868/cs/6990/}}} \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}sura1234 \\ \uline{cheatography.com/sura1234} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 12th 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Loop Positive Integer}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{even = 0 \newline % Row Count 1 (+ 1) odd = 0 \newline % Row Count 2 (+ 1) while True: \newline % Row Count 3 (+ 1) user\_input = int(input("Enter a number :")) \newline % Row Count 4 (+ 1) user = user\_input \% 2 \newline % Row Count 5 (+ 1) if user\_input \textgreater{} 0: \newline % Row Count 6 (+ 1) if user == 0: \newline % Row Count 7 (+ 1) even = even + 1 \newline % Row Count 8 (+ 1) elif user != 0: \newline % Row Count 9 (+ 1) odd = odd + 1 \newline % Row Count 10 (+ 1) print(user\_input) \newline % Row Count 11 (+ 1) else: \newline % Row Count 12 (+ 1) print ("Even number = ", even) \newline % Row Count 13 (+ 1) print ("Odd number = ", odd) \newline % Row Count 14 (+ 1) break% 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}{Fibonacci}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{num1 = 0 \newline % Row Count 1 (+ 1) num2 = 1 \newline % Row Count 2 (+ 1) fibonacci = num1 + num2 \newline % Row Count 3 (+ 1) output = "0,1" \newline % Row Count 4 (+ 1) while fibonacci \textless{} 50: \newline % Row Count 5 (+ 1) output = output + "," + str(fibonacci) \newline % Row Count 6 (+ 1) num1 = num2 \newline % Row Count 7 (+ 1) num2 = fibonacci \newline % Row Count 8 (+ 1) fibonacci = num1 + num2 \newline % Row Count 9 (+ 1) print (output)% Row Count 10 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Fibonacci}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{num1 = 0 \newline % Row Count 1 (+ 1) num2 = 1 \newline % Row Count 2 (+ 1) fibonacci = num1 + num2 \newline % Row Count 3 (+ 1) output = "0,1" \newline % Row Count 4 (+ 1) while fibonacci \textless{} 50: \newline % Row Count 5 (+ 1) output = output + "," + str(fibonacci) \newline % Row Count 6 (+ 1) num1 = num2 \newline % Row Count 7 (+ 1) num2 = fibonacci \newline % Row Count 8 (+ 1) fibonacci = num1 + num2 \newline % Row Count 9 (+ 1) print (output)% Row Count 10 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Variable Name}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Good Variable Name \newline % Row Count 1 (+ 1) my\_string = "123" \newline % Row Count 2 (+ 1) \_hello = "1" \newline % Row Count 3 (+ 1) mystring = 1 \newline % Row Count 4 (+ 1) value1 = 1 \newline % Row Count 5 (+ 1) \#can have integers, lowercase/uppercase, underscores \newline % Row Count 7 (+ 2) \#the first character must be a lowercase/uppercase or an underscore \newline % Row Count 9 (+ 2) Bad Variable Name \newline % Row Count 10 (+ 1) email@ = 2 \newline % Row Count 11 (+ 1) 1value = 2% Row Count 12 (+ 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}{Upper Lower List}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{mystr = "hello THERE" \newline % Row Count 1 (+ 1) print (mystr.upper()) \#Upper case all the letter in a word \newline % Row Count 3 (+ 2) print (mystr.lower()) \#Lower case all the letter in a word \newline % Row Count 5 (+ 2) print (mystr.capitalize()) \#Capital only first letter of first word \newline % Row Count 7 (+ 2) print (mystr.title()) \#Capital first letter of every word. \newline % Row Count 9 (+ 2) \#List in python \newline % Row Count 10 (+ 1) shoppinglist = {[}'Dogs', 'Cats', 'Mouses', 'Giraffe'{]} \newline % Row Count 12 (+ 2) print(shoppinglist{[}2{]}) \#Will print 'Mouses' \newline % Row Count 13 (+ 1) \#while loop \newline % Row Count 14 (+ 1) item\_number = 0 \newline % Row Count 15 (+ 1) while item\_number \textless{} len(shoppinglist): \newline % Row Count 16 (+ 1) print ("list item:", shoppinglist{[}item\_number{]}) \newline % Row Count 18 (+ 2) item\_number = item\_number + 1 \newline % Row Count 19 (+ 1) \#for loop \newline % Row Count 20 (+ 1) other = 0 \newline % Row Count 21 (+ 1) for cat in shoppinglist: \newline % Row Count 22 (+ 1) other = other + 1 \newline % Row Count 23 (+ 1) \# print ("List item:", cat) \newline % Row Count 24 (+ 1) print (other)% Row Count 25 (+ 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}{Calculator}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{def calc(num1, num2, operation): \newline % Row Count 1 (+ 1) if operation == "sum": \newline % Row Count 2 (+ 1) return sum(num1, num2) \newline % Row Count 3 (+ 1) elif operation == "product": \newline % Row Count 4 (+ 1) return product(num1, num2) \newline % Row Count 5 (+ 1) elif operation == "diff": \newline % Row Count 6 (+ 1) return diff(num1, num2) \newline % Row Count 7 (+ 1) elif operation == "div": \newline % Row Count 8 (+ 1) return div(num1, num2) \newline % Row Count 9 (+ 1) else: \newline % Row Count 10 (+ 1) print ("Unknown Operation") \newline % Row Count 11 (+ 1) def sum(a, b): \newline % Row Count 12 (+ 1) return a + b \newline % Row Count 13 (+ 1) def product(a, b): \newline % Row Count 14 (+ 1) return a * b \newline % Row Count 15 (+ 1) def diff(a, b): \newline % Row Count 16 (+ 1) return a - b \newline % Row Count 17 (+ 1) def div(a, b): \newline % Row Count 18 (+ 1) if b == 0: \newline % Row Count 19 (+ 1) return ("Error: Undefined value") \newline % Row Count 20 (+ 1) else: \newline % Row Count 21 (+ 1) return a // b \newline % Row Count 22 (+ 1) print(calc(12, 12, "sum")) \newline % Row Count 23 (+ 1) print(calc(9, 18, "diff")) \newline % Row Count 24 (+ 1) print(calc(20, 10, "product")) \newline % Row Count 25 (+ 1) print(calc(12, 4, "div"))% Row Count 26 (+ 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}{Guessing Game}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\#PWTK 1002 \newline % Row Count 1 (+ 1) scores = 0 \newline % Row Count 2 (+ 1) chances = 3 \newline % Row Count 3 (+ 1) while chances \textgreater{} 0: \newline % Row Count 4 (+ 1) print \seqsplit{("-=-=-=-=-=-=-=-=-=-=-Guessing} Game-=-=-=-=-=-=-=-=-{}-=-=-=-") \newline % Row Count 6 (+ 2) import random \newline % Row Count 7 (+ 1) mylist = {[}'apple', 'banana', 'papaya', 'melon', 'orange', 'grape', 'mango'{]} \newline % Row Count 9 (+ 2) print (mylist) \newline % Row Count 10 (+ 1) random\_item = random.choice(mylist) \newline % Row Count 11 (+ 1) user\_guess = input("Guess a word: ") \newline % Row Count 12 (+ 1) if user\_guess == random\_item: \newline % Row Count 13 (+ 1) print ("That's correct") \newline % Row Count 14 (+ 1) scores = scores + 100 \newline % Row Count 15 (+ 1) print ("Scores =", scores) \newline % Row Count 16 (+ 1) else: \newline % Row Count 17 (+ 1) chances = chances - 1 \newline % Row Count 18 (+ 1) print ("Chances left: ", chances) \newline % Row Count 19 (+ 1) if user\_guess in mylist: \newline % Row Count 20 (+ 1) print ("That's incorrect") \newline % Row Count 21 (+ 1) else: \newline % Row Count 22 (+ 1) print ("Sorry, that is not even in the list!") \newline % Row Count 24 (+ 2) if chances == 0: \newline % Row Count 25 (+ 1) print ("The word was: ", random\_item) \newline % Row Count 26 (+ 1) print ("Final score =", scores) \newline % Row Count 27 (+ 1) print ("GAME OVER!!!!!!!!")% Row Count 28 (+ 1) } \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{String} & A list of characters such as numbers, letters, symbols \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{Variable} & Holds a value and can be changed \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{Syntax} & The set of rules that defines the combinations of symbols \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{Boolean} & Identified True or False (true is not the same as True, false is not the same as False) \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{Modulo} & Finds the remainder after division of one number by another \tn % Row Count 11 (+ 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}{Radius of a Circle}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{while True: \newline % Row Count 1 (+ 1) \#Ask the user for a radius of a circle \newline % Row Count 2 (+ 1) user\_radius = input("What is the radius of the circle ") \newline % Row Count 4 (+ 2) \#Convert the given radius to a floating point \newline % Row Count 5 (+ 1) radius = (float(user\_radius)) \newline % Row Count 6 (+ 1) \#Make a variable called pi \newline % Row Count 7 (+ 1) pi = 3.1415 \newline % Row Count 8 (+ 1) \#Calculate the area of the circle using exponents \newline % Row Count 10 (+ 2) area = (pi {\emph{ (radius }}* 2)) \newline % Row Count 11 (+ 1) \#Display the area of the circle to the user \newline % Row Count 12 (+ 1) print("The area of the circle is", area)% Row Count 13 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Basic Info}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Basic Python Programming Language \newline % Row Count 1 (+ 1) :(colon) = syntax \newline % Row Count 2 (+ 1) Syntax (=) Grammar \newline % Row Count 3 (+ 1) Variable = Something that cahnges(numbers, words) \newline % Row Count 4 (+ 1) Number vs Strings: my var = 1 + 2 \newline % Row Count 5 (+ 1) print my var = 3 \newline % Row Count 6 (+ 1) my var 2 = "1" + "2" \newline % Row Count 7 (+ 1) print my var "12" \newline % Row Count 8 (+ 1) hello= "hello" + "It's me" \newline % Row Count 9 (+ 1) print hello = "helloIt'sme" \newline % Row Count 10 (+ 1) If 1==2: \newline % Row Count 11 (+ 1) When you do division in programming the program will add decimal even if it doesn't have the decimal EX: 10.0 \newline % Row Count 14 (+ 3) my var = "yourname"{[}0{]} (the first letter in programming is 0 not 1) \newline % Row Count 16 (+ 2) == equal to \newline % Row Count 17 (+ 1) != not equal to \newline % Row Count 18 (+ 1) \textgreater{} Greater than \newline % Row Count 19 (+ 1) \textgreater{}= Greater than or equal to \newline % Row Count 20 (+ 1) \textless{}= Less than or equal to \newline % Row Count 21 (+ 1) \textless{} Less than \newline % Row Count 22 (+ 1) print (len(fullname)) \newline % Row Count 23 (+ 1) if 1 == 2: \newline % Row Count 24 (+ 1) print ("true") \newline % Row Count 25 (+ 1) else: \newline % Row Count 26 (+ 1) print ("false") \newline % Row Count 27 (+ 1) if 2 == 2: \newline % Row Count 28 (+ 1) print ("true") \newline % Row Count 29 (+ 1) else: \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Basic Info (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{ print ("flase") \newline % Row Count 1 (+ 1) print ("false2")% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Palindrome}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{def isPalindrome(word): \newline % Row Count 1 (+ 1) reverse = "" \newline % Row Count 2 (+ 1) for item in user\_word: \newline % Row Count 3 (+ 1) reverse = item + reverse \newline % Row Count 4 (+ 1) reverse\_item = reverse \newline % Row Count 5 (+ 1) if reverse\_item == user\_word: \newline % Row Count 6 (+ 1) return True\#(reverse\_item, ("is a palindrome")) \newline % Row Count 8 (+ 2) else: \newline % Row Count 9 (+ 1) return False\#(reverse\_item, ("is not a palindrome")) \newline % Row Count 11 (+ 2) while True: \newline % Row Count 12 (+ 1) user\_word = input("Enter a word: ") \newline % Row Count 13 (+ 1) length = len(user\_word) \newline % Row Count 14 (+ 1) if user\_word == 'quit': \newline % Row Count 15 (+ 1) break \newline % Row Count 16 (+ 1) else: \newline % Row Count 17 (+ 1) print (length) \newline % Row Count 18 (+ 1) numlen = 0 \newline % Row Count 19 (+ 1) while numlen \textless{} length / 2 + 1: \newline % Row Count 20 (+ 1) if user\_word{[}numlen{]} != user\_word{[}-numlen-1{]}: \newline % Row Count 22 (+ 2) print (user\_word,"is not a palindrom") \newline % Row Count 24 (+ 2) break \newline % Row Count 25 (+ 1) numlen += 1 \newline % Row Count 26 (+ 1) else: \newline % Row Count 27 (+ 1) print (user\_word,"is a palindrome")% Row Count 28 (+ 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}{Return Max Number}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{def max2(num1, num2): \newline % Row Count 1 (+ 1) maxvalue = num1 \newline % Row Count 2 (+ 1) if num2 \textgreater{} maxvalue: \newline % Row Count 3 (+ 1) masvalue = num2 \newline % Row Count 4 (+ 1) return maxvalue \newline % Row Count 5 (+ 1) def max3(num1, num2, num3): \newline % Row Count 6 (+ 1) maxvalue = num1 \newline % Row Count 7 (+ 1) if num2 \textgreater{} maxvalue: \newline % Row Count 8 (+ 1) maxvalue = num2 \newline % Row Count 9 (+ 1) if num3 \textgreater{} maxvalue: \newline % Row Count 10 (+ 1) maxvalue = num3 \newline % Row Count 11 (+ 1) return maxvalue \newline % Row Count 12 (+ 1) def maxlist(list): \newline % Row Count 13 (+ 1) maxvalue = list{[}0{]} \newline % Row Count 14 (+ 1) for num in list: \newline % Row Count 15 (+ 1) if num \textgreater{} maxvalue: \newline % Row Count 16 (+ 1) maxvalue = num \newline % Row Count 17 (+ 1) return maxvalue \newline % Row Count 18 (+ 1) print (maxlist({[}1,2,3,4,5{]}))% Row Count 19 (+ 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}{Binary}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{while True: \newline % Row Count 1 (+ 1) user\_number = input("Put the number: ") \newline % Row Count 2 (+ 1) number = int(user\_number) \newline % Row Count 3 (+ 1) binary\_string = '' \newline % Row Count 4 (+ 1) while (number \textgreater{} 0): \newline % Row Count 5 (+ 1) remainder = (number \% 2) \newline % Row Count 6 (+ 1) binary\_string = str(remainder) + binary\_string \newline % Row Count 8 (+ 2) number = (number // 2) \newline % Row Count 9 (+ 1) print ("binary string is", binary\_string)% Row Count 10 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Loop Range}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\#Creating List \newline % Row Count 1 (+ 1) mylist = {[}1,2,3,4,5,6{]} \newline % Row Count 2 (+ 1) mylist2 = {[}'hi', 'hello', 'anything'{]} \newline % Row Count 3 (+ 1) mylist3 = {[}1, 'hello', 2.5{]} \newline % Row Count 4 (+ 1) print (mylist) \newline % Row Count 5 (+ 1) print (mylist2) \newline % Row Count 6 (+ 1) print (mylist3) \newline % Row Count 7 (+ 1) \#How to make a list with all numbers from 0-10 \newline % Row Count 8 (+ 1) mynumbers = range(11) \#0-10 (Number starts with 0) \newline % Row Count 10 (+ 2) for num in mynumbers: \newline % Row Count 11 (+ 1) print (num) \newline % Row Count 12 (+ 1) mylist2.append('another item') \#Adding item in a list \newline % Row Count 14 (+ 2) print (mylist2)% 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}{Reverse}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{word = input("Input a word: ") \newline % Row Count 1 (+ 1) reverse = "" \newline % Row Count 2 (+ 1) letter\_num = 0 \newline % Row Count 3 (+ 1) ''' \newline % Row Count 4 (+ 1) while letter\_num \textless{} len(word): \newline % Row Count 5 (+ 1) reverse = word{[}letter\_num{]} + reverse \newline % Row Count 6 (+ 1) letter\_num = letter\_num + 1 \newline % Row Count 7 (+ 1) ''' \newline % Row Count 8 (+ 1) for item in word: \newline % Row Count 9 (+ 1) reverse = item + reverse \newline % Row Count 10 (+ 1) print ("Reverse: ",reverse)% Row Count 11 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.9154 cm} x{1.60195 cm} x{2.05965 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Command}} \tn % Row 0 \SetRowColor{LightBackground} \# Hashtag & Add comment & \# CAN WRITE ANYTHING HEREEE \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} ''' (3 \seqsplit{Apostrophe)} & Long comment & ''' ALSO HEREEEEE ''' \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} print & To display something & print (var) \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} '' '' & Assign something in a variable & mystr = ("George") \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} int() & Set the number to interger & integer = int(20) \#with no decimal \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} str() & Convert a variable to string & String = str(integer) \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} input() & Gain information from the user & Name = input(" Put your name here: " \tn % Row Count 17 (+ 3) % Row 7 \SetRowColor{white} float() & Convert the number with decimal & Num = float(2) \#the answer will be 2.0 \tn % Row Count 20 (+ 3) % Row 8 \SetRowColor{LightBackground} len() & Find the length of the string & num1 = ("George"),, num2 = len(num1) \#Answer will be 6 \tn % Row Count 24 (+ 4) \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 RandomChoice}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{import random \newline % Row Count 1 (+ 1) intlist = {[}1, 2, 3, 4, 5{]} \newline % Row Count 2 (+ 1) random\_int = random.choice(intlist) \newline % Row Count 3 (+ 1) print (intlist, random\_int) \newline % Row Count 4 (+ 1) fplist = {[}2.2, 3.5, 4.8, 6.2, 7.9{]} \newline % Row Count 5 (+ 1) random\_fp = random.choice(fplist) \newline % Row Count 6 (+ 1) print (fplist, random\_fp) \newline % Row Count 7 (+ 1) strlist = {[}'burger', 'cheese', 'ham', 'bacon', 'sandwich', 'pizza'{]} \newline % Row Count 9 (+ 2) random\_str = random.choice(strlist) \newline % Row Count 10 (+ 1) print (strlist, random\_str) \newline % Row Count 11 (+ 1) mylist = {[}4, 6, 8, 11.4, 12.8, 17.6,'coco', 'latte', 'mocha'{]} \newline % Row Count 13 (+ 2) random\_item = random.choice(mylist) \newline % Row Count 14 (+ 1) print (mylist, random\_item) \newline % Row Count 15 (+ 1) myvar1 = 1 \newline % Row Count 16 (+ 1) myvar2 = 2 \newline % Row Count 17 (+ 1) myvar3 = 3 \newline % Row Count 18 (+ 1) varlist = {[}myvar1, myvar2, myvar3{]} \newline % Row Count 19 (+ 1) random\_var = random.choice(varlist) \newline % Row Count 20 (+ 1) print (varlist, random\_var)% Row Count 21 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Countdown}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{user\_number = input("Please enter a number: ") \newline % Row Count 1 (+ 1) number = int(user\_number) \newline % Row Count 2 (+ 1) countdown\_string = "" \newline % Row Count 3 (+ 1) while number \textgreater{} 0: \newline % Row Count 4 (+ 1) countdown\_string = countdown\_string + str(number) \newline % Row Count 6 (+ 2) number = number - 1 \newline % Row Count 7 (+ 1) \#add the number to the string \newline % Row Count 8 (+ 1) \#subtract 1 from the number \newline % Row Count 9 (+ 1) print (countdown\_string)% Row Count 10 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Area of Triangle}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\# Pom Wintakorn 1002 \newline % Row Count 1 (+ 1) def areaofTriangle(base, height): \newline % Row Count 2 (+ 1) return 0.5 {\emph{ base }} height \newline % Row Count 3 (+ 1) user\_base = float(input("Enter the base of the triangle: ")) \newline % Row Count 5 (+ 2) user\_height = float(input("Enter the height of the triangle: ")) \newline % Row Count 7 (+ 2) print ("The area of the triangle is", \seqsplit{areaofTriangle(user\_base}, user\_height)) \newline % Row Count 9 (+ 2) area = \seqsplit{areaofTriangle(user\_base}, user\_height) \newline % Row Count 10 (+ 1) def volumeofPrism(area, height): \newline % Row Count 11 (+ 1) return area * height \newline % Row Count 12 (+ 1) user\_height2 = float(input("Enter the second height of the triangle: ")) \newline % Row Count 14 (+ 2) print ("The volume of the triangular prism is", volumeofPrism(area, user\_height2))% Row Count 16 (+ 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}{Hexadecimal}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{while True: \newline % Row Count 1 (+ 1) user\_number = input("Put the number: ") \newline % Row Count 2 (+ 1) number = int(user\_number) \newline % Row Count 3 (+ 1) \#Loop the command \newline % Row Count 4 (+ 1) hex\_string = '' \newline % Row Count 5 (+ 1) while (number \textgreater{} 0): \newline % Row Count 6 (+ 1) remainder = number \% 16 \newline % Row Count 7 (+ 1) if remainder == 10: \newline % Row Count 8 (+ 1) remainder = 'A' \newline % Row Count 9 (+ 1) elif remainder == 11: \newline % Row Count 10 (+ 1) remainder = 'B' \newline % Row Count 11 (+ 1) elif remainder == 12: \newline % Row Count 12 (+ 1) remainder = 'C' \newline % Row Count 13 (+ 1) elif remainder == 13: \newline % Row Count 14 (+ 1) remainder = 'D' \newline % Row Count 15 (+ 1) elif remainder == 14: \newline % Row Count 16 (+ 1) remainder = 'E' \newline % Row Count 17 (+ 1) elif remainder == 15: \newline % Row Count 18 (+ 1) remainder = 'F' \newline % Row Count 19 (+ 1) hex\_string = str(remainder) + hex\_string \newline % Row Count 20 (+ 1) number = number // 16 \newline % Row Count 21 (+ 1) print ("hexadecimal string is 0x"+ hex\_string)% Row Count 22 (+ 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}{Loop Review}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\#While loop \newline % Row Count 1 (+ 1) mylist = {[}1,2,3{]} \newline % Row Count 2 (+ 1) index = 0 \#set to 0 because that is the first item in the list \newline % Row Count 4 (+ 2) while index \textless{} len(mylist): \newline % Row Count 5 (+ 1) print (mylist{[}index{]}) \newline % Row Count 6 (+ 1) index = index + 1 \newline % Row Count 7 (+ 1) \#For loop \newline % Row Count 8 (+ 1) for item in mylist: \newline % Row Count 9 (+ 1) print(item)% Row Count 10 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{List Practice}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{import random \newline % Row Count 1 (+ 1) intlist = {[}1, 2, 3, 4, 5{]} \newline % Row Count 2 (+ 1) random\_int = random.choice(intlist) \newline % Row Count 3 (+ 1) print (intlist, random\_int) \newline % Row Count 4 (+ 1) fplist = {[}2.2, 3.5, 4.8, 6.2, 7.9{]} \newline % Row Count 5 (+ 1) random\_fp = random.choice(fplist) \newline % Row Count 6 (+ 1) print (fplist, random\_fp) \newline % Row Count 7 (+ 1) strlist = {[}'burger', 'cheese', 'ham', 'bacon', 'sandwich', 'pizza'{]} \newline % Row Count 9 (+ 2) random\_str = random.choice(strlist) \newline % Row Count 10 (+ 1) print (strlist, random\_str) \newline % Row Count 11 (+ 1) mylist = {[}4, 6, 8, 11.4, 12.8, 17.6,'coco', 'latte', 'mocha'{]} \newline % Row Count 13 (+ 2) random\_item = random.choice(mylist) \newline % Row Count 14 (+ 1) print (mylist, random\_item) \newline % Row Count 15 (+ 1) myvar1 = 1 \newline % Row Count 16 (+ 1) myvar2 = 2 \newline % Row Count 17 (+ 1) myvar3 = 3 \newline % Row Count 18 (+ 1) varlist = {[}myvar1, myvar2, myvar3{]} \newline % Row Count 19 (+ 1) random\_var = random.choice(varlist) \newline % Row Count 20 (+ 1) print (varlist, random\_var)% Row Count 21 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.74195 cm} x{3.23505 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Number and String}} \tn % Row 0 \SetRowColor{LightBackground} "String" + "String" & Put both string together \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Number + "String" & CRASH! \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Number + Number & Addition(Math) \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} "String" * "String" & CRASH! \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} "String" * Number & Print that string that number times \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} Number * Number & Multiplication(Math) \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} String ** String & CRASH! \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} String ** Number & CRASH! \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} Number ** Number & Exponent(Math) \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} x{4.4793 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Mathematics}} \tn % Row 0 \SetRowColor{LightBackground} + & Addition \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} - & Subtraction \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} * & Multiplication \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} / & Division (Result with floating point) \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} // & Division \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} ** & Exponent \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} \% & Modulo (Find remainder) \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} == & Equal to \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} \textgreater{}= & Greater than or equal to \tn % Row Count 10 (+ 1) % Row 9 \SetRowColor{white} \textless{}= & Less than or equal to \tn % Row Count 11 (+ 1) % Row 10 \SetRowColor{LightBackground} != & Not equal to \tn % Row Count 12 (+ 1) % Row 11 \SetRowColor{white} \textless{} & Less than \tn % Row Count 13 (+ 1) % Row 12 \SetRowColor{LightBackground} \textgreater{} & More than \tn % Row Count 14 (+ 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}{Multiplication Table}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{def multiplicationTable(): \newline % Row Count 1 (+ 1) innum = int(input("Enter a number: ")) \newline % Row Count 2 (+ 1) for i in range(1,11): \newline % Row Count 3 (+ 1) output = innum*i \newline % Row Count 4 (+ 1) print (str(innum) + "*" + str(i) + "=" + str(output)) \newline % Row Count 6 (+ 2) multiplicationTable()% Row Count 7 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}