\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{mindko} \pdfinfo{ /Title (python.pdf) /Creator (Cheatography) /Author (mindko) /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}{9999FF} \definecolor{LightBackground}{HTML}{F2F2FF} \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}{mindko} via \textcolor{DarkBackground}{\uline{cheatography.com/25840/cs/6968/}}} \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}mindko \\ \uline{cheatography.com/mindko} \\ \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{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Abbreviation}} \tn % Row 0 \SetRowColor{LightBackground} str & string \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} len() & lenght \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} print() & print \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} int() & change to be integer \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} float() & make it to be decimal number \tn % Row Count 6 (+ 2) % Row 5 \SetRowColor{white} while : & While something is true the condition in the loop will proceed \tn % Row Count 9 (+ 3) % Row 6 \SetRowColor{LightBackground} for ...... in...... : & For ..... in (your list or string) repeat the command inside the loop that many times \tn % Row Count 13 (+ 4) % Row 7 \SetRowColor{white} while True : & forever proceed \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} If ........: then....... else & If the "If" statement is true the loop will proceed the condition inside then loop but if the "If" statement is false the loop will proceed the condition inside else loop \tn % Row Count 22 (+ 8) \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 change \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{string} & A list of character such as letter, number or symbol \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{boolean} & True and False (with capital letter) \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{modulo} & Remainder of the division \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{syntax} & The grammar of writing pyton \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} float & number with decimal point \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} \seqsplit{integer} & Rounded number with no decimal point \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.69678 cm} x{4.28022 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{function}} \tn % Row 0 \SetRowColor{LightBackground} input & receives information from user \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} print & show the result \tn % Row Count 2 (+ 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 giving name}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- letter \newline % Row Count 1 (+ 1) - number \newline % Row Count 2 (+ 1) - underscore \newline % Row Count 3 (+ 1) - NO SPACE!!!!!! \newline % Row Count 4 (+ 1) - start with letter or underscore {\bf{ONLY}} \newline % Row Count 5 (+ 1) {\bf{Valid name}} \newline % Row Count 6 (+ 1) - myvar\_1 \newline % Row Count 7 (+ 1) - myvar1\_ \newline % Row Count 8 (+ 1) - \_myvar1 \newline % Row Count 9 (+ 1) {\bf{Invalid name}} \newline % Row Count 10 (+ 1) - 1myvar (number be the first letter of the name) \newline % Row Count 11 (+ 1) - my var (no space in the name) \newline % Row Count 12 (+ 1) - my-var (no dash)% 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}{Define the function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def areaofcircle (radius): \#define function named areaofcircle, parameter radius \newline if radius \textless{}= 0: \newline return "Error: Invalid radius" \newline pi = 3.1415 \newline area = 3.1415 * radius**2 \newline return area \# return the area of the circle \newline \newline userradius = float(input("Enter the radius:")) \newline print ("The area of the circle is", \seqsplit{areaofcircle(userradius))}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The result \newline Enter the radius:0 \newline The area of the circle is Error: Invalid 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}{Define Function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def bacon() : \newline print ("hello it's bacon") \newline return \newline \newline bacon()} \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 of Palindrome}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{string = input("Please type the string:") \newline string = str(string) \newline letter\_num = 0 \newline reverse = "" \newline while letter\_num \textless{} len(string) : \newline reverse = string{[}letter\_num{]}+ reverse \newline letter\_num = letter\_num + 1 \newline if string == reverse : \newline print ("This string is palindrome") \newline else : \newline print ("This string is not palindrome")} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Result of the function : \newline Please type the string:456 \newline This string is not palindrome \newline \newline {\bf{OR}} \newline \newline Please type the string:12321 \newline This string is 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}{Maximum Value}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#write a function that returns the largest of two values \newline \#name: max2 \newline \#arguments: num1, num2 \newline \#return: the largest value \newline \newline \newline def max2(num1, num2): \newline if num1\textgreater{}num2 : \newline maxvalue = num1 \newline else : \newline maxvalue = num2 \newline return maxvalue \newline \newline user\_num1 = int(input("Enter the first number:")) \newline user\_num2 = int(input("Enter the second number:")) \newline print ("The largest value is:",max2(user\_num1, user\_num2))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Enter the first number:5 \newline Enter the second number:2 \newline The largest value is: 5} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Maximum three function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#write a function that returns the largest number of three value \newline \#name: max3 \newline \#arguments: num1, num2, num3 \newline \#return: the largest value \newline \newline def max3 (num1,num2,num3): \newline maxvalue = num1 \newline if num2 \textgreater{} maxvalue: \newline maxvalue = num2 \newline if num3 \textgreater{} maxvalue: \newline maxvalue = num3 \newline return maxvalue \newline user\_num1 = int(input("Enter the first number:")) \newline user\_num2 = int(input("Enter the second number:")) \newline user\_num3 = int(input("Enter the third number:")) \newline print ("The largest value is:",max3(user\_num1, user\_num2, user\_num3))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Enter the first number:12 \newline Enter the second number:3 \newline Enter the third number:456 \newline The largest value is: 456} \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}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mylist = {[}1,2,3,4,5{]} \newline \newline for number in mylist : \newline print (number)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1 \newline 2 \newline 3 \newline 4 \newline 5} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Even number from -100 to -1}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{number = -100 \newline while number \textless{} -1 : \newline print (number) \newline number = number + 2} \tn \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}{Operator}} \tn % Row 0 \SetRowColor{LightBackground} == & compare \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} != & not equal \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} while & loop \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} + & plus \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} - & minus \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} / & divide and quotient is float \tn % Row Count 7 (+ 2) % Row 6 \SetRowColor{LightBackground} \textgreater{}= & greater than or equal \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} \textgreater{} & greater than \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} \textless{}= & less than or equal \tn % Row Count 10 (+ 1) % Row 9 \SetRowColor{white} \textless{} & less than \tn % Row Count 11 (+ 1) % Row 10 \SetRowColor{LightBackground} \% & keep the remainder \tn % Row Count 12 (+ 1) % Row 11 \SetRowColor{white} ** & power \tn % Row Count 13 (+ 1) % Row 12 \SetRowColor{LightBackground} \# & comment \tn % Row Count 14 (+ 1) % Row 13 \SetRowColor{white} // & divide and quotient is integer \tn % Row Count 16 (+ 2) % Row 14 \SetRowColor{LightBackground} * & multiply \tn % Row Count 17 (+ 1) % Row 15 \SetRowColor{white} \seqsplit{"""........"""} & multi-line comment \tn % Row Count 19 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.55618 cm} x{2.38004 cm} p{0.64078 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Capitalize}} \tn % Row 0 \SetRowColor{LightBackground} print \seqsplit{(mystr.upper())} & all letter become uppercase & HELLO THERE \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} print \seqsplit{(mystr.lower())} & all letter become lowercase & hello there \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} print \seqsplit{(mystr.capitalize())} & first letter become uppercase, all other lowercase & Hello there \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} print \seqsplit{(mystr.title())} & first letter of each word is uppercase & Hello There \tn % Row Count 12 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Put letter in different line}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mystr = "Hello" \newline \newline letter\_num = 0 \newline \newline while letter\_num \textless{} len(mystr): \newline print (mystr{[}letter\_num{]}) \newline letter\_num = letter\_num + 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Out put \newline H \newline e \newline l \newline l \newline o} \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}{Different type of list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import random \newline \newline intlist = {[}1,2,3{]} \newline random\_int = random.choice(intlist) \newline print (intlist,random\_int) \newline \newline fplist = {[}1.02,3.02,5.36{]} \newline random\_fp = random.choice(fplist) \newline print (fplist, random\_fp) \newline \newline strlist = {[}'mind','mom','hall'{]} \newline random\_str = random.choice(strlist) \newline print (strlist, random\_str) \newline \newline mylist = {[}1,2.35,'tiger'{]} \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}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{[}1, 2, 3{]} 2 \newline {[}1.02, 3.02, 5.36{]} 5.36 \newline {[}'mind', 'mom', 'hall'{]} mom \newline {[}1, 2.35, 'tiger'{]} tiger \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}{Function defeinition}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{def printDefinition(word): \newline \#word = user\_input \newline if word=="variable": \newline print (""" \newline A variable is the value that can change. You can refered it by make the name of the variable \newline """) \newline elif \seqsplit{word=="function":\#function} \newline print (""" \newline A function define the block of code that can be reuse \newline """) \newline elif \seqsplit{word=="parameter":\#parameter} \newline print(""" \newline A parameter is the thing that you give to the function in Pyton \newline """) \newline elif \seqsplit{word=="argument":\#argument} \newline print(""" \newline An argument is the thing that you give to the function \newline """) \newline elif word=="function call":\#function call \newline print(""" \newline A function call is command that call code in the function to run or execute \newline """) \newline elif word=="string":\#string \newline print(""" \newline A string is the list of letter, number, space or everything \newline """) \newline else : \newline print ("Unknow word") \newline return \newline while True: \newline user\_input = input("Enter word:") \newline \seqsplit{printDefinition(user\_input)}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{RESULT \newline Enter word:funciton \newline Unknow word \newline Enter word:function \newline \newline A function define the block of code that can be reuse \newline \newline Enter word:hi \newline Unknow word \newline Enter word:hello \newline Unknow word' \newline Enter word:edlfw \newline Unknow word \newline Enter word:variable \newline \newline A variable is the value that can change. You can refered it by make the name of the variable \newline \newline Enter word:funciton call \newline Unknow word \newline Enter word: \newline \newline The program keep asking to enter the word because the loop {\bf{while 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}{Maximum number in list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#write a function that returns the largest number in a list \newline \#name: maxlist \newline \#argument: numlist \newline \#return the largest value in a list \newline \newline def maxlist(numlist): \newline maxvalue = numlist{[}0{]} \newline for item in numlist : \newline if item \textgreater{}= maxvalue: \newline maxvalue = item \newline return maxvalue \newline numlist = {[}1,2,35,2654,232,5,2,5{]} \newline print(maxlist(numlist))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{2654} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Print fifth character from the list}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{myword = "hellothere" \newline print (myword{[}4{]})} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{o} \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}{expected output of the program}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{mystring = "" \newline count = 0 \newline while count \textless{} 5 : \newline mystring = mystring + str(count) \newline print (mystring) \newline count = count + 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Result must be: \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{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Operation}} \tn % Row 0 \SetRowColor{LightBackground} string + string & combine together \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} string * string & invalid syntax \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} string * number & repeat the string by the number \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} number + number & addition \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} number * number & multiple \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} string ** string & invalid syntax \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} string ** number & invalid syntax \tn % Row Count 14 (+ 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}{Reverse}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{word = input ("Please type the world : ") \newline letter\_num = 0 \newline reverse = "" \newline while letter\_num \textless{} len(word) : \newline reverse = word{[}letter\_num{]}+ reverse \newline letter\_num = letter\_num + 1 \newline print ("reverse :", reverse) \newline \newline OR \newline \newline word = input("Please type the 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}{Convert decimal to binary}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{number = input ("What you want to convert to binary :") \newline number = int(number) \newline binary = "" \newline while (number \textgreater{} 0): \newline remainder = number\%2 \newline binary = str(remainder)+ binary \newline number = number//2 \newline print (binary)} \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 number}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{number = input ("What you want to countdown :") \newline number = int(number) \newline countdown = "" \newline while number \textgreater{} 0: \newline countdown = countdown + str(number) + " " \newline number = number - 1 \newline print(countdown)} \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}{Circle area}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{user\_radius = input("What is a radius of a circle?") \# to get number from user \newline \newline radius = float(user\_radius) \#Convert the given radius to a floating point \newline \newline pi = float(3.1415) \#determine the value of variable called pi \newline \newline area = pi(radius\textasciicircum{}2\textasciicircum{}) \#Calculate the area of the circle using exponents \newline \newline print ("The area of the circle is", area) \#Show the area of the circle to the user} \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{LightBackground} \mymulticolumn{1}{x{5.377cm}}{chance = 5 \newline score = 0 \newline \newline \newline mylist = {[}'coke','bacon', 'chicken', 'pocky', 'pepsi', 'pizza'{]} \newline \newline import random \newline random\_item = random.choice(mylist) \newline \newline \newline \newline \newline while chance \textgreater{} 0: \newline \newline print \seqsplit{("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")} \newline print ("Guessing Game") \newline print \seqsplit{("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")} \newline \newline \newline print ("Words:", mylist) \newline \newline \newline \newline \newline user\_guese = input("Guese the word: ") \newline if user\_guese == random\_item: \newline score = score+100 \newline print ("That's correct! Score:", score) \newline random\_item = random.choice(mylist) \newline else: \newline chance = chance-1 \newline if user\_guese in mylist: \newline print ("Sorry, wrong choice!") \newline print ("Chances Remaining:", chance) \newline else: \newline print ("Sorry, that is not ever in the list") \newline print ("Chances Remaining:", chance) \newline \newline print ("Game Over! The word was", random\_item) \newline print ("Final Score:", 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}{Area of triangle}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# write a function that computers the area of a triangle \newline \newline \#name: areaofTriangle \newline \#parameters : b, h \newline \#return : area \newline \newline def areaofTriangle(b,h): \newline area = 0.5{\emph{b}}h \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 the triangle is", \seqsplit{areaofTriangle(user\_base},user\_height))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{RESULT \newline Enter the base of the triangle:6 \newline Enter the height of the triangle:10 \newline The area of the triangle is 30.0 \newline \textgreater{}\textgreater{}\textgreater{}} \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 of volume}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# write a function that computers the area of a triangle \newline \newline \#name: areaofTriangle \newline \#parameters : b, h \newline \#return : area \newline \newline def areaofTriangle(b,h): \newline area = 0.5{\emph{b}}h \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 \#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 = areaofTriangle(b,h)*l \newline return volume \newline user\_lenght = float(input("Enter the lenght of the prism:")) \newline print ("The volume of the prism is", volumeofPrism(user\_base,user\_height,user\_lenght))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{RESULT \newline Enter the base of the triangle:4 \newline Enter the height of the triangle:6 \newline Enter the lenght of the prism:10 \newline The volume of the prism is 120.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}{While loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{wlist = {[}2,4,5,6,7,8{]} \newline \newline letternum = 0 \newline while letternum \textless{} len(wlist) : \newline print (wlist{[}letternum{]}) \newline letternum = letternum + 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{2 \newline 4 \newline 5 \newline 6 \newline 7 \newline 8} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}