\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{Padma (padma-it)} \pdfinfo{ /Title (programming-with-python.pdf) /Creator (Cheatography) /Author (Padma (padma-it)) /Subject (Programming with 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}{212121} \definecolor{LightBackground}{HTML}{F8F8F8} \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{Programming with Python Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Padma (padma-it)} via \textcolor{DarkBackground}{\uline{cheatography.com/121896/cs/22458/}}} \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}Padma (padma-it) \\ \uline{cheatography.com/padma-it} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 27th April, 2020.\\ 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{tabularx}{17.67cm}{x{4.8923 cm} x{6.0732 cm} x{5.9045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Python Operators}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Operator}} & {\bf{Example}} & {\bf{Result}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Assignment}} = & a,b,c = 5,6,2\{\{nl\}\}inc=dec=10 & \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Exponential}} ** & a ** c & 25 \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} {\bf{Unary operators}}\textasciitilde{} + - & print(-8)\{\{nl\}\}print(\textasciitilde{}7) & Negative number-8 \{\{nl\}\}-8;Negate opeartor changes all 1 to 0;0 to 1 \tn % Row Count 12 (+ 5) % Row 4 \SetRowColor{LightBackground} {\bf{Float Division}} / & a/b & \seqsplit{0.8333333333333334} \tn % Row Count 14 (+ 2) % Row 5 \SetRowColor{white} {\bf{Floor Division}} // & a//b, b//1 & 0, 1 \tn % Row Count 16 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{Remainder(Modulo)}} \% & a\%b, b\%a & 5, 1 \tn % Row Count 19 (+ 3) % Row 7 \SetRowColor{white} {\bf{Bitwise operators}} \& | \textgreater{}\textgreater{} \textless{}\textless{} \textasciicircum{} & a = 10 = 1010 (Binary) \{\{nl\}\}b = 4 = 0100 (Binary)\{\{nl\}\} & a\&b = 0\{\{nl\}\}a|b=14(1110)\{\{nl\}\}a\textasciicircum{}b=14\{\{nl\}\}a\textgreater{}\textgreater{}1=5\{\{nl\}\} 5\textless{}\textless{}1 = 10 \tn % Row Count 24 (+ 5) % Row 8 \SetRowColor{LightBackground} {\bf{Increment}} += & inc += 1 & 11 \tn % Row Count 26 (+ 2) % Row 9 \SetRowColor{white} {\bf{Decrement}} -= & dec -= 1 & 9 \tn % Row Count 28 (+ 2) % Row 10 \SetRowColor{LightBackground} {\bf{Identity}} & a=10\{\{nl\}\}b=5+2\{\{nl\}\}c=a & a is b : False\{\{nl\}\}a is c: True \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.8923 cm} x{6.0732 cm} x{5.9045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Python Operators (cont)}} \tn % Row 11 \SetRowColor{LightBackground} {\bf{Membership}} & in, not in & \tn % Row Count 2 (+ 2) % Row 12 \SetRowColor{white} {\bf{Logical}} and, or & True or False & True \tn % Row Count 4 (+ 2) % Row 13 \SetRowColor{LightBackground} {\bf{Boolean}} True, False & True and False & False \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Input and Output}} \tn % Row 0 \SetRowColor{LightBackground} & {\bf{Example}} & {\bf{Result}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{int(input())}} gets input and converts to integer & score = \seqsplit{int(input("Enter} number: ")) & 10 \{\{nl\}\} 20 \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} {\bf{input()}} gets input in string & name = input() & Test \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{ast.literal\_eval(input())}} gets input in Python data type format & list1 = \seqsplit{ast.literal\_eval(input())} & {[}1,2,3,4{]} \{\{nl\}\} ('a','e','i') \{\{nl\}\} \{'a':1,'b':2\} \tn % Row Count 14 (+ 6) % Row 4 \SetRowColor{LightBackground} {\bf{print}} format & print("\{0\} \{1\} using \{2\}".format("data","analysis","Python")) & data analysis using Python \tn % Row Count 19 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.8923 cm} x{6.0732 cm} x{5.9045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Conditional Statements}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Construct}} & {\bf{Example}} & {\bf{Result}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{if...elif...else}}\{\{nl\}\} \{\{nl\}\} & if score==100:\{\{nl\}\}~~ print("Perfect")\{\{nl\}\} elif 60\textless{}=score\textless{}100: \{\{nl\}\} ~~ print("First Class")\{\{nl\}\} else:\{\{nl\}\}~~ \seqsplit{print("Failed")} & \tn % Row Count 14 (+ 12) % Row 2 \SetRowColor{LightBackground} {\bf{for \{\{nl\}\}break \{\{nl\}\}else}} & for i in range(1, 4): \{\{nl\}\}~~ print(i) \{\{nl\}\}~~ if i==2: \{\{nl\}\}~~~~ break ~~ \{\{nl\}\} else:\{\{nl\}\} ~ ~ \# Executed when no break in for loop \{\{nl\}\} ~ ~ print("No Break") & 1\{\{nl\}\} 2 \tn % Row Count 32 (+ 18) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.8923 cm} x{6.0732 cm} x{5.9045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Conditional Statements (cont)}} \tn % Row 3 \SetRowColor{LightBackground} {\bf{in}} & my\_string = "Mary"\{\{nl\}\} for alphabet in my\_string:\{\{nl\}\} ~~ \seqsplit{print(alphabet)} \{\{nl\}\} my\_string = "Mary had a little lamb" \{\{nl\}\}for word in \seqsplit{my\_string.split():} \{\{nl\}\}~~ print(word) & M\{\{nl\}\} a\{\{nl\}\} r\{\{nl\}\} y\{\{nl\}\} Mary\{\{nl\}\} had a\{\{nl\}\} little\{\{nl\}\} lamb\{\{nl\}\} \tn % Row Count 15 (+ 15) % Row 4 \SetRowColor{white} {\bf{while}} & start =1\{\{nl\}\} total= 0\{\{nl\}\} while start\textless{}5:\{\{nl\}\}~~ total+=start\{\{nl\}\}~~ start+=1\{\{nl\}\}~~print(total) & 10 \tn % Row Count 25 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Inbuilt Functions}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Function Syntax}} & {\bf{Description}} & {\bf{Example}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{abs()}} & returns the absolute value of number & abs(-7.25) = 7.25 \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{all(iterable)}} & Check if all items in a list are True & all(True,False) is False\{\{nl\}\}all\{True,True) is True \tn % Row Count 9 (+ 4) % Row 3 \SetRowColor{white} {\bf{any(iterable)}} & Check if any of the items in a list are True & \seqsplit{any(empty\_iterable)} is False\{\{nl\}\}any(True,False) is True \tn % Row Count 14 (+ 5) % Row 4 \SetRowColor{LightBackground} {\bf{divmod(divident, divisor)}} & returns a {\bf{tuple}} containing the quotient and the remainder & \seqsplit{print(divmod(5}, 2)) is (2,1) \tn % Row Count 19 (+ 5) % Row 5 \SetRowColor{white} {\bf{eval(expression, globals, locals)}} & evaluates the smaller expression and runs it & x = 1\{\{nl\}\} \seqsplit{print(eval('x} + 1')) is {\bf{2}} \tn % Row Count 23 (+ 4) % Row 6 \SetRowColor{LightBackground} {\bf{exec()}} & executes all size Python code & x = 'name = "John"\textbackslash{}nprint(name)'\{\{nl\}\} exec(x) is {\bf{John}} \tn % Row Count 28 (+ 5) % Row 7 \SetRowColor{white} {\bf{help()}} & Gives help content of the function & \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Inbuilt Functions (cont)}} \tn % Row 8 \SetRowColor{LightBackground} {\bf{id()}} & returns id of the object & \tn % Row Count 2 (+ 2) % Row 9 \SetRowColor{white} {\bf{isinstance()}} & returns {\bf{True}} if the specified object is of the specified type & isinstance(5, float, int, str, list, dict, tuple) \tn % Row Count 7 (+ 5) % Row 10 \SetRowColor{LightBackground} {\bf{issubclass()}} & returns {\bf{True}} if the specified object is a subclass & \tn % Row Count 12 (+ 5) % Row 11 \SetRowColor{white} {\bf{range(start, stop, step)}} & {\bf{start:}} Optional. Integer Specifying at which position to start. Default is 0 \{\{nl\}\}{\bf{stop:}} Required. Integer. Runs till {\bf{stop-1}} \{\{nl\}\}{\bf{step:}} Optional. Integer number specifying the \seqsplit{incrementation}. Negative for decrement; Default is 1; & \tn % Row Count 31 (+ 19) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Inbuilt Functions (cont)}} \tn % Row 12 \SetRowColor{LightBackground} {\bf{reversed(sequence)}} & returns a reversed iterator object & same as \seqsplit{list.reverse()} \tn % Row Count 3 (+ 3) % Row 13 \SetRowColor{white} {\bf{round(number, digits)}} & returns a floating point number & \seqsplit{print(round(5}.76543, 2)) is 5.77 \tn % Row Count 6 (+ 3) % Row 14 \SetRowColor{LightBackground} {\bf{sorted(iterable, key=function, reverse=reverse)}} & {\bf{iterable:}}Required. The sequence to sort, list, dictionary, tuple etc.\{\{nl\}\} {\bf{key:}}Optional. A Function to execute to decide the order e.g lambda x:x\%5 Default is None\{\{nl\}\} {\bf{reverse}}Optional. A Boolean. False will sort ascending, True will sort descending. Default is False & \tn % Row Count 28 (+ 22) % Row 15 \SetRowColor{white} {\bf{sum(iterable, start)}} & returns sum of all items in an iterable & sum(list,value) = \seqsplit{value+list\_elements} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Inbuilt Functions (cont)}} \tn % Row 16 \SetRowColor{LightBackground} {\bf{type(object, bases, dict)}} & returns the type of the object & \tn % Row Count 3 (+ 3) % Row 17 \SetRowColor{white} {\bf{zip(iterator1, iterator2, iterator3 ...)}} & an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together & a = ("A", "B", "C")\{\{nl\}\} b = (1, 2, 3, 4)\{\{nl\}\} z = zip(a, b)\{\{nl\}\} print(list(z))\{\{nl\}\} {\bf{OUTPUT}}: {[}('A', 1), ('B', 2), ('C', 3){]} \tn % Row Count 15 (+ 12) % Row 18 \SetRowColor{LightBackground} {\bf{Custom Functions}} default return is None & def f1(*args):\{\{nl\}\}~~ print(args)\{\{nl\}\}~~ \seqsplit{return(sum(args))} & f1(1,2)\{\{nl\}\}f1(3,4,5,6) \tn % Row Count 22 (+ 7) % Row 19 \SetRowColor{white} {\bf{lambda arguments : expression}} & expression is executed and the result is returne & x = lambda a : a + 10\{\{nl\}\} print(x(5))\{\{nl\}\} {\bf{OUTPUT:}}15 \tn % Row Count 27 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{List Data Structure {[} {]} - Mutable}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Operation}} & {\bf{Example}} & {\bf{Result}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{Assignment}} & lst={[}'ant','bat','cat',42{]} & \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Creation}} & my\_list = {[}"Hi"{]} *5 & {[}'Hi', 'Hi', 'Hi', 'Hi', 'Hi'{]} \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} {\bf{List concatenation}} & my\_list = {[}"Hi"{]} + {[}"Padma"{]} & {[}'Hi', 'Padma'{]} \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Accessing the list}} & print(lst) & {[}'ant','bat','cat',42{]} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} {\bf{Indexing}} & print(lst{[}0{]})\{\{nl\}\}print(lst{[}-1{]}) & ant\{\{nl\}\}42 \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{Slicing}} & print(lst{[}1:2{]})\{\{nl\}\}print(lst{[}2:{]})\{\{nl\}\}print(lst{[} : -3{]}) & {[}'bat', 'cat'{]}\{\{nl\}\}{[}'cat',42{]}\{\{nl\}\}{[}'ant','bat'{]} \tn % Row Count 19 (+ 5) % Row 7 \SetRowColor{white} {\bf{Remove Last Element}} Stack \seqsplit{implementation} & lst.pop()\{\{nl\}\}lst.pop() & 42\{\{nl\}\}'cat' \tn % Row Count 23 (+ 4) % Row 8 \SetRowColor{LightBackground} {\bf{Remove Last Element}} Queue \seqsplit{implementation} & lst.pop(0)\{\{nl\}\}lst.pop(0) & 'ant'\{\{nl\}\}'bat' \tn % Row Count 27 (+ 4) % Row 9 \SetRowColor{white} {\bf{Remove any element}} & lst.remove(42)\{\{nl\}\}print(lst) & {[}'ant','bat','cat'{]} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{List Data Structure {[} {]} - Mutable (cont)}} \tn % Row 10 \SetRowColor{LightBackground} {\bf{Add new element at the end}} & lst.append('R')\{\{nl\}\}print(lst) & {[}'ant','bat','cat','R'{]} \tn % Row Count 3 (+ 3) % Row 11 \SetRowColor{white} {\bf{Add new element at index}} & lst.insert(3, 'o') & {[}'ant','bat','cat','o','R'{]} \tn % Row Count 6 (+ 3) % Row 12 \SetRowColor{LightBackground} {\bf{Add all items of a list}} & lst2 = {[}'dog','fish'{]}\{\{nl\}\}lst.extend(lst2)\{\{nl\}\}lst.append(lst2) & {[}'ant','bat','cat','o','R','dog','fish'{]}\{\{nl\}\}{[}'ant','bat','cat','o','R',{[}'dog','fish'{]}{]} \tn % Row Count 13 (+ 7) % Row 13 \SetRowColor{white} {\bf{Length}} & len(lst) & 5 \tn % Row Count 14 (+ 1) % Row 14 \SetRowColor{LightBackground} {\bf{Sort Elements}} & \seqsplit{print(sorted(lst))} & {[}'R', 'ant', 'bat', 'cat', 'o'{]} \tn % Row Count 17 (+ 3) % Row 15 \SetRowColor{white} {\bf{Maximum}} & n={[}2,6,9,3,2,1{]}\{\{nl\}\}print(max(n))\{\{nl\}\}print(max(lst, key=len)) & \{\{nl\}\}9\{\{nl\}\}ant \tn % Row Count 22 (+ 5) % Row 16 \SetRowColor{LightBackground} {\bf{Minimum}} & print(min(nums))\{\{nl\}\}print(min(lst, key=len)) & 1\{\{nl\}\}'o' \tn % Row Count 26 (+ 4) % Row 17 \SetRowColor{white} {\bf{Nested lists}} & nest = {[}{[}1, 2, 3, 4{]}, {[} 5, 6, 7{]}, {[}8, 9, 10{]}{]} \{\{nl\}\}print(nest{[}1{]}) \{\{nl\}\}print(nest{[}0{]}{[}2{]}) & {[}5, 6, 7{]}\{\{nl\}\} 3 \tn % Row Count 33 (+ 7) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{List Data Structure {[} {]} - Mutable (cont)}} \tn % Row 18 \SetRowColor{LightBackground} {\bf{List copies}} & print(id(lst))\{\{nl\}\} l1=lst\{\{nl\}\} print(id(l1))\{\{nl\}\} l2= DA.copy()\{\{nl\}\} print(id(l2))\{\{nl\}\} & 12241256679{\bf{76}} \{\{nl\}\}\{\{nl\}\}12241256679{\bf{76}} \{\{nl\}\}\{\{nl\}\}12241263956{\bf{56}} \tn % Row Count 8 (+ 8) % Row 19 \SetRowColor{white} {\bf{Integer List to string}} & \seqsplit{print(''.join(str(e)} for e in n)) & 269321 \tn % Row Count 11 (+ 3) % Row 20 \SetRowColor{LightBackground} {\bf{String list to String}} & \seqsplit{print('\&'.join(lst))} & \seqsplit{ant\&bat\&cat\&o\&R} \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.8923 cm} x{6.0732 cm} x{5.9045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Tuple Data Structure ( ) - Immutable}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Operation}} & {\bf{Example}} & {\bf{Result}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Single value tuple creation}} & tpl = (27,) & (27,) \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Repetition}} & tpl2 = 2*(27,) & (27,27) \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} {\bf{Assignment}} & tpl2 = tpl\{\{nl\}\}print(id(tpl))\{\{nl\}\}print(id(tpl2)) & \{\{nl\}\}15192281282161519228128216 1519228128216 1519228128216 \tn % Row Count 12 (+ 5) % Row 4 \SetRowColor{LightBackground} {\bf{Concatenation}} & t1 = (1, 2, 3) \{\{nl\}\} t2=(4, 5, 6)\{\{nl\}\}t3=t1+t2 & \{\{nl\}\}\{\{nl\}\}(1, 2, 3, 4, 5, 6) \tn % Row Count 16 (+ 4) % Row 5 \SetRowColor{white} {\bf{Convert to Tuple}} & tup3 = tuple({[}1,2,3{]})\{\{nl\}\} print(tup3)\{\{nl\}\} tup4 = tuple('Hello')\{\{nl\}\} print(tup4)\{\{nl\}\} & \{\{nl\}\}\{\{nl\}\} (1, 2, 3)\{\{nl\}\}\{\{nl\}\} ('H', 'e', 'l', 'l', 'o') \tn % Row Count 23 (+ 7) % Row 6 \SetRowColor{LightBackground} {\bf{Indexing}} & print(tup3{[}1{]})\{\{nl\}\}print(tup3{[}-1{]}) & 2\{\{nl\}\}3 \tn % Row Count 27 (+ 4) % Row 7 \SetRowColor{white} {\bf{Slicing}} & print(tup4{[}:3{]}) & ('H', 'e', 'l') \tn % Row Count 29 (+ 2) % Row 8 \SetRowColor{LightBackground} {\bf{Count Elements}} & \seqsplit{print(tup4.count("l"))} & 2 \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.8923 cm} x{6.0732 cm} x{5.9045 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Tuple Data Structure ( ) - Immutable (cont)}} \tn % Row 9 \SetRowColor{LightBackground} {\bf{Get Index of Element}} & \seqsplit{print(tup3.index(2))} & 1 \tn % Row Count 3 (+ 3) % Row 10 \SetRowColor{white} {\bf{Membership test}} & 3 in (1, 2, 3) & True \tn % Row Count 5 (+ 2) % Row 11 \SetRowColor{LightBackground} {\bf{Iteration}} & for x in (1, 2, 3): \{\{nl\}\}~~ print(x,end='') & 123 \tn % Row Count 9 (+ 4) % Row 12 \SetRowColor{white} {\bf{Length}} & \seqsplit{print(len(t3))} & 6 \tn % Row Count 10 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Dictionary Data Structure \{ \} - Mutable}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Operation}} & {\bf{Example}} & {\bf{Result}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{Create Empty Dictionary}} & d1 = \{\} & \{\} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Creation}} & d=\{'a':1, 'b':2\} & \{'a': 1, 'b': 2\} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{Creation using dict}} & d=dict(a=1, b=2) & \{'a': 1, 'b': 2\} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{Create from tuples and lists}} & dict({[}(1,'apple'), (2,'ball'){]}) & \{1: 'apple', 2:'ball'\} \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} {\bf{Mixed keys}} & d3 = \{'name': 'Padma', 1: {[}2, 4, 3{]}\} & \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{Create using fromkeys()}} & keys = \{'a', 'e', 'i', 'o', 'u' \}\{\{nl\}\} vowels = dict.fromkeys(keys)\{\{nl\}\} \seqsplit{print(vowels)} & \{'o': None, 'u': None, 'i': None, 'e': None, 'a': None\} \tn % Row Count 21 (+ 7) % Row 7 \SetRowColor{white} {\bf{Add multiple key,values}} & d = \{'a':1,'c':3\}\{\{nl\}\} d2 = \{'d': 4,'e':5\}\{\{nl\}\} d.update(d2)\{\{nl\}\} print(d)\{\{nl\}\} & \{\{nl\}\}\{\{nl\}\}\{'c': 3, 'e': 5, 'd': 4, 'a': 1\} \tn % Row Count 28 (+ 7) % Row 8 \SetRowColor{LightBackground} {\bf{Add/Change new key,value}} & d{[}'c'{]} = 3 \{\{nl\}\} \#{\bf{overwrites value if key exists}} & \{'a': 1, 'b': 2, 'c':3\} \tn % Row Count 33 (+ 5) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Dictionary Data Structure \{ \} - Mutable (cont)}} \tn % Row 9 \SetRowColor{LightBackground} {\bf{Using key as index}} & print(d{[}'a'{]}) & 1 \tn % Row Count 2 (+ 2) % Row 10 \SetRowColor{white} {\bf{get(key,default\_value)}} & \seqsplit{print(d.get('c'},'NA')) & NA \tn % Row Count 4 (+ 2) % Row 11 \SetRowColor{LightBackground} {\bf{Get all keys}} lazy fn & print({\bf{list}}(d.keys())) & {[}'a','b','c'{]} \tn % Row Count 6 (+ 2) % Row 12 \SetRowColor{white} {\bf{Get all values}} lazy fn & print({\bf{list}}(d.values())) & {[}1,2,3{]} \tn % Row Count 9 (+ 3) % Row 13 \SetRowColor{LightBackground} {\bf{Delete key,value}} & del d{[}'b'{]} & \{'a': 1, 'c':3\} \tn % Row Count 11 (+ 2) % Row 14 \SetRowColor{white} {\bf{Remove key,value}} & \seqsplit{print(d.pop('c'))} & 3 \tn % Row Count 13 (+ 2) % Row 15 \SetRowColor{LightBackground} {\bf{Remove any key,value}} & \seqsplit{print(d.popitem())} & ('a',1) \tn % Row Count 15 (+ 2) % Row 16 \SetRowColor{white} {\bf{Delete key,value}} & del d{[}'a'{]} & \tn % Row Count 17 (+ 2) % Row 17 \SetRowColor{LightBackground} {\bf{Delete dictionary}} & del d & \tn % Row Count 19 (+ 2) % Row 18 \SetRowColor{white} {\bf{Key Membership Test}} & print('c' in d)\{\{nl\}\}print(3 in d) & True\{\{nl\}\}False \tn % Row Count 22 (+ 3) % Row 19 \SetRowColor{LightBackground} {\bf{Iteration}}\{\{nl\}\}d.keys()\{\{nl\}\}d.values()\{\{nl\}\}d.items() & for i in d: \{\{nl\}\}~~ print(d{[}i{]}) & Default is keys iterated.\{\{nl\}\}Use d.keys() or d.values() \{\{nl\}\} Use d.items() to get (key,value) \tn % Row Count 30 (+ 8) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Dictionary Data Structure \{ \} - Mutable (cont)}} \tn % Row 20 \SetRowColor{LightBackground} {\bf{Sort keys}} & \seqsplit{print(sorted(d))} & {[}'a','b','c'{]} \tn % Row Count 2 (+ 2) % Row 21 \SetRowColor{white} {\bf{Get number of keys}} & \seqsplit{print(len(d))} & 3 \tn % Row Count 4 (+ 2) % Row 22 \SetRowColor{LightBackground} {\bf{Delete all key,values}} & d.clear() & \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Sets - Mutable DS of immutable elements}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Operation}} & {\bf{Example}} & {\bf{Result}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{Create Empty Set}} & set() & set() \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Create Non-empty Set}} & \{\{nl\}\}set2 = set({[}'c','d','e'{]})\{\{nl\}\}set3 = set({[}'c','f'{]}) & \{\{nl\}\}\{'c','d','e'\}\{\{nl\}\}\{'c','f'\}) \tn % Row Count 8 (+ 5) % Row 3 \SetRowColor{white} {\bf{List to set}} & lst={[}'a','a','b','c'{]}\{\{nl\}\} set1 = set(lst) & \{\{nl\}\}\{'a','b','c'\} \tn % Row Count 12 (+ 4) % Row 4 \SetRowColor{LightBackground} {\bf{Union}} & print(set1.union(set2))\{\{nl\}\}print(set1 | set2) & \{'a', 'e', 'd', 'c', 'b'\}\{\{nl\}\}\{'a', 'e', 'd', 'c', 'b'\} \tn % Row Count 17 (+ 5) % Row 5 \SetRowColor{white} {\bf{Intersection}} & print(set1.intersection(set2))\{\{nl\}\}print(set1 \& set2) & \{'c'\}\{\{nl\}\}\{'c'\} \tn % Row Count 22 (+ 5) % Row 6 \SetRowColor{LightBackground} {\bf{Difference}} & print(set1.difference(set2))\{\{nl\}\}print(set1-set2) & \{'a', 'b'\}\{\{nl\}\}\{'a', 'b'\} \tn % Row Count 26 (+ 4) % Row 7 \SetRowColor{white} {\bf{Symmetric difference}} & print(set1.symmetric\_difference(set2))\{\{nl\}\}print(set1\textasciicircum{}set2) & \{'e', 'b', 'a', 'd'\}\{\{nl\}\}\{'e', 'b', 'a', 'd'\} \tn % Row Count 31 (+ 5) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Sets - Mutable DS of immutable elements (cont)}} \tn % Row 8 \SetRowColor{LightBackground} {\bf{Intersection function}} does not take in list or tuples of sets but sets itself. & \seqsplit{print(set.intersection(set1}, set2, set3))\{\{nl\}\}print(set1.intersection(set2, set3)) & \{'c'\}\{\{nl\}\}\{'c'\} \tn % Row Count 7 (+ 7) % Row 9 \SetRowColor{white} {\bf{Remove dupliates from a list}} & \seqsplit{print(list(set(lst)))} & \tn % Row Count 10 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Comprehension}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Why comprehensions?} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Comprehensions are used to create iterable objects in a simpler and concise fashion.\{\{nl\}\}They are the complete substitute of for loops, map, reduce or nested loop\{\{nl\}\}Comprehensions are very compact and can be initialized in a single statement and occupies less space in the memory and it has less execution time} \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Types of comprehensions:} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}1. List comprehension\{\{nl\}\} 2. Nested list comprehension\{\{nl\}\} 3. Dictionary comprehension \{\{nl\}\} 4. Set comprehension\{\{nl\}\} 5. Generator comprehension\{\{nl\}\}} \tn % Row Count 13 (+ 5) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{1. LISTS COMPREHENSION:}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}{\bf{Syntax:}}\{\{nl\}\} output\_list = {[}output\_exp for var in input\_list if (var satisfies this condition){]}\{\{nl\}\}\{\{nl\}\} {\bf{Example:}}\{\{nl\}\} list\_using\_comp = {[}var x 2 for var in range(1, 10){]} \{\{nl\}\}\{\{nl\}\} Each time var is picked from iterable i.e. range here and the operation var x 2 is performed and the final value is added to the list.\{\{nl\}\}\{\{nl\}\} {\bf{OUTPUT:}}\{\{nl\}\} {[}2, 4, 6, 8, 10, 12, 14, 16, 18{]}\{\{nl\}\}} \tn % Row Count 23 (+ 10) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{2. Nested List comprehension}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}{\bf{Syntax:}}\{\{nl\}\} {[}inner\_list\_element for outer\_list\_element in outer\_list for inner\_list\_element in outer\_list\_element {]}\{\{nl\}\}\{\{nl\}\} {\bf{Example:}}\{\{nl\}\} {[} z for x in y for z in x.split(){]}\{\{nl\}\}\{\{nl\}\} {\bf{Without nested list comprehension:}}\{\{nl\}\} for x in y:\{\{nl\}\}~~ for z in x.split():\{\{nl\}\}~~~~ a.append(z)\{\{nl\}\}} \tn % Row Count 32 (+ 9) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Comprehension (cont)}} \tn % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{3. Dictionary Comprehensions:}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}{\bf{SYNTAX:}}\{\{nl\}\} output\_dict = \{key:value for (key, value) in iterable if (key, value satisfy this condition)\}\{\{nl\}\}\{\{nl\}\} {\bf{Example:}}\{\{nl\}\} dict\_using\_comp = \{var:var xx 3 for var in input\_list if var \% 2 != 0\} \{\{nl\}\} Similar to above, var is picked from input\_list and is set as key. The value is assigned by the result of var xx 3\{\{nl\}\}\{\{nl\}\} {\bf{OUTPUT:}}\{\{nl\}\} \{1: 1, 3: 27, 5: 125, 7: 343\}\{\{nl\}\}} \tn % Row Count 10 (+ 10) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{4. Set Comprehensions:}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}This is similar to dict comprehension as it will have \{\}\{\{nl\}\} But differs in the fact, retaining set principle, does not add duplicates.\{\{nl\}\}\{\{nl\}\} {\bf{EXAMPLE:}}\{\{nl\}\} input\_list = {[}1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 7{]}\{\{nl\}\} set\_using\_comp = \{var for var in input\_list if var \% 2 == 0\} \{\{nl\}\} {\bf{OUTPUT:}}\{\{nl\}\} \{2, 4, 6\}\{\{nl\}\}} \tn % Row Count 19 (+ 9) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{5. Generator Comprehensions:}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}One difference between this and list comprehension is that generator comprehensions use (). \{\{nl\}\}Generators don't allocate memory for the whole list. \{\{nl\}\}Instead, they generate each value one by one which is why they are memory efficient.\{\{nl\}\} List comprehensions impact performance for huge data.\{\{nl\}\} {\bf{EXAMPLE:}}\{\{nl\}\} input\_list = {[}1, 2, 3, 4, 4, 5, 6, 7, 7{]} \{\{nl\}\} output\_gen = (var for var in input\_list if var \% 2 == 0)\{\{nl\}\} print(output\_gen)\{\{nl\}\} {\bf{OUTPUT:}}\{\{nl\}\} 2 4 4 6\{\{nl\}\}} \tn % Row Count 31 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Maps, Filters and Reduce}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{1. What is a map?}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Map applies a function to all the items in an input\_list} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Syntax}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}map(function\_to\_apply, iterable)} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Why do we need maps?}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Most of the times we want to pass all the list elements to a function one-by-one and then collect the output.\{\{nl\}\}items = {[}1, 2, 3, 4, 5{]} squared = {[}{]} for i in items: squared.append(ixx2)} \tn % Row Count 10 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Map implementation:}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}squared = list(map(lambda n: nxx2, items))} \tn % Row Count 12 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Advantages:}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Instead of a list of inputs we can even have a list of functions} \tn % Row Count 15 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Return Type}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}lazy function; list() or tuple() to be used to list or tuple of items.} \tn % Row Count 18 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{2. What is a filter?}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}To filter some elements what we want and remove what we dont want} \tn % Row Count 21 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Syntax}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}filter(function or None, sequence)} \tn % Row Count 23 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Why do we need filters?}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}filter resembles a for loop but it is a builtin function and faster} \tn % Row Count 26 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Filter implementation:}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}less\_than\_zero = list(filter(lambda x: x \textless{} 0, number\_list))} \tn % Row Count 29 (+ 3) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Return Type}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}lazy function; list() or tuple() to be used to list or tuple of items.} \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Maps, Filters and Reduce (cont)}} \tn % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{3. What is a reduce function?}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Reduce is a really useful function for performing some computation on a list and returning the result.} \tn % Row Count 4 (+ 4) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Why do we need reduce?}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}If you want to compute the product of a list of integers. So the normal way you might go about doing this task in python is using a basic for loop} \tn % Row Count 9 (+ 5) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{reduce implementation:}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}functools.reduce((lambda x, y: x * y), {[}1, 2, 3, 4{]})} \tn % Row Count 12 (+ 3) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Return Type}}} \tn \mymulticolumn{1}{x{17.67cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Returns final element.} \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.3862 cm} x{6.2419 cm} x{6.2419 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{String Data Structure - Mutable}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Operation}} & {\bf{Example}} & {\bf{Result}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{String notation}} & w1='Hello'\{\{nl\}\} w2=" world "\{\{nl\}\} w3 = """Good morning""" & single ('), double (") and triple (''' or """) Same type of quotes in start and end \tn % Row Count 8 (+ 6) % Row 2 \SetRowColor{LightBackground} {\bf{Multi-Line Statements}} & str1 = w1+ \textbackslash{} \{\{nl\}\}~~~~~~~~ ~~ w2 + \textbackslash{} \{\{nl\}\}~~ ~~~~~~~~ w3 & {\emph{Hello world Good morning}} \tn % Row Count 20 (+ 12) % Row 3 \SetRowColor{white} {\bf{Comments notation}} & \# Comment & Hash sign (\#) that is not inside a string literal \tn % Row Count 24 (+ 4) % Row 4 \SetRowColor{LightBackground} {\bf{Multi line comments}} & '''\{\{nl\}\}Comments\{\{nl\}\}''' & Triple-quoted string \tn % Row Count 27 (+ 3) % Row 5 \SetRowColor{white} {\bf{Indexing}} {[} {]} & print(str1{[}1{]}) \{\{nl\}\}print(str1{[}-5{]}) & e \{\{nl\}\} r \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.3862 cm} x{6.2419 cm} x{6.2419 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{String Data Structure - Mutable (cont)}} \tn % Row 6 \SetRowColor{LightBackground} {\bf{Slicing}}{[} : {]} & print(str1{[}1:5{]})\{\{nl\}\} print(str1{[}:5{]})\{\{nl\}\} print(str1{[}1:{]}) & ello\{\{nl\}\} Hello\{\{nl\}\} ello world Good morning \tn % Row Count 5 (+ 5) % Row 7 \SetRowColor{white} {\bf{Update String}} & str1 ="Maths" & Reassign is {\bf{allowed}} \tn % Row Count 7 (+ 2) % Row 8 \SetRowColor{LightBackground} {\bf{Concatenation}} & print(w1 + w2) & Hello world \tn % Row Count 9 (+ 2) % Row 9 \SetRowColor{white} {\bf{Repetition}} & print(w1*2) & HelloHello \tn % Row Count 11 (+ 2) % Row 10 \SetRowColor{LightBackground} {\bf{Membership}} in, not in & H in w1\{\{nl\}\} e not in "Hello" & True\{\{nl\}\}False \tn % Row Count 14 (+ 3) % Row 11 \SetRowColor{white} {\bf{Raw String}} r/R & print('Hi\textbackslash{}nHello') \{\{nl\}\}\{\{nl\}\}print(r'Hi\textbackslash{}nHello') & Hi\{\{nl\}\} Hello \{\{nl\}\}Hi\textbackslash{}nHello \tn % Row Count 18 (+ 4) % Row 12 \SetRowColor{LightBackground} {\bf{String format opearator}}\% & print("Scores \%s - \%d" \%(str1,90))\{\{nl\}\}print("List: \%s" \% {[}1,2,3{]})\{\{nl\}\}print('\%6.2f' \% \seqsplit{(3.141592653589793},)) & Scores Maths - 90\{\{nl\}\}\{\{nl\}\}List: {[}1, 2, 3{]}\{\{nl\}\}\{\{nl\}\}~~~~~~ 3.14 \tn % Row Count 26 (+ 8) % Row 13 \SetRowColor{white} {\bf{ASCII to Unicode}} & \seqsplit{print(ord("A"))} & 65 \tn % Row Count 28 (+ 2) % Row 14 \SetRowColor{LightBackground} {\bf{Unicode to ASCII}} & \seqsplit{print(chr(65))} & A \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.3862 cm} x{6.2419 cm} x{6.2419 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{String Data Structure - Mutable (cont)}} \tn % Row 15 \SetRowColor{LightBackground} {\emph{*Change cases}} & print(w1.lower())\{\{nl\}\}print(w2.upper())\{\{nl\}\}print(str1.title()) & hello\{\{nl\}\}WORLD\{\{nl\}\}Hello World Good Morning \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.687 cm} p{1.687 cm} p{1.687 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Miscellaneous}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \end{document}