diff options
Diffstat (limited to 'book/module1')
| -rw-r--r-- | book/module1/arrays.tex | 411 | ||||
| -rw-r--r-- | book/module1/basics_of_python.tex | 289 | ||||
| -rw-r--r-- | book/module1/module1.aux | 140 | ||||
| -rw-r--r-- | book/module1/module1.tex | 3 |
4 files changed, 843 insertions, 0 deletions
diff --git a/book/module1/arrays.tex b/book/module1/arrays.tex new file mode 100644 index 0000000..a5486d9 --- /dev/null +++ b/book/module1/arrays.tex @@ -0,0 +1,411 @@ + \hypertarget{matrixarrays}{% +\section{matrixArrays}\label{matrixarrays}} + +In computer programming, an array is a structure for storing and +retrieving data. We often talk about an array as if it were a grid in +space, with each cell storing one element of the data. For instance, if +each element of the data were a number, we might visualize a +``one-dimensional'' array like a list: + +\begin{longtable}[]{@{}llll@{}} +\toprule +1 & 5 & 2 & 0 \\ +\midrule +\endhead +\bottomrule +\end{longtable} + +A two-dimensional array would be like a table: + +\begin{longtable}[]{@{}llll@{}} +\toprule +1 & 5 & 2 & 0 \\ +\midrule +\endhead +8 & 3 & 6 & 1 \\ +1 & 7 & 2 & 9 \\ +\bottomrule +\end{longtable} + +A three-dimensional array would be like a set of tables, perhaps stacked +as though they were printed on separate pages. If we visualize the +position of each element as a position in space. Then we can represent +the value of the element as a property. In other words, if we were to +analyze the stress concentration of an aluminum block, the property +would be stress. + +\begin{itemize} +\tightlist +\item + From + \href{https://numpy.org/doc/2.2/user/absolute_beginners.html}{Numpy + documentation} + +\end{itemize} + +If the load on this block changes over time, then we may want to add a +4th dimension i.e.~additional sets of 3-D arrays for each time +increment. As you can see - the more dimensions we add, the more +complicated of a problem we have to solve. It is possible to increase +the number of dimensions to the n-th order. This course we will not be +going beyond dimensional analysis. + +\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} + +\hypertarget{numpy---the-pythons-array-library}{% +\section{Numpy - the python's array +library}\label{numpy---the-pythons-array-library}} + +In this tutorial we will be introducing arrays and we will be using the +numpy library. Arrays, lists, vectors, matrices, sets - You might've +heard of them before, they all store data. In programming, an array is a +variable that can hold more than one value at a time. We will be using +the Numpy python library to create arrays. Since we already have +installed Numpy previously, we can start using the package. + +Before importing our first package, let's as ourselves \emph{what is a +package?} A package can be thought of as pre-written python code that we +can re-use. This means the for every script that we write in python we +need to tell it to use a certain package. We call this importing a +package. + +\hypertarget{importing-numpy}{% +\subsection{Importing Numpy}\label{importing-numpy}} + +When using packages in python, we need to let it know what package we +will be using. This is called importing. To import numpy we need to +declare it a the start of a script as follows: + +\begin{Shaded} +\begin{Highlighting}[] +\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np} +\end{Highlighting} +\end{Shaded} + +\begin{itemize} +\tightlist +\item + \texttt{import} - calls for a library to use, in our case it is Numpy. +\item + \texttt{as} - gives the library an alias in your script. It's common + convention in Python programming to make the code shorter and more + readable. We will be using \emph{np} as it's a standard using in many + projects. +\end{itemize} + +\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} + +\hypertarget{creating-arrays}{% +\section{Creating arrays}\label{creating-arrays}} + +Now that we have imported the library we can create a one dimensional +array or \emph{vector} with three elements. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=}\NormalTok{ np.array([}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{])} +\end{Highlighting} +\end{Shaded} + +To create a \emph{matrix} we can nest the arrays to create a two +dimensional array. This is done as follows. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{matrix }\OperatorTok{=}\NormalTok{ np.array([[}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{],} +\NormalTok{ [}\DecValTok{4}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{6}\NormalTok{],} +\NormalTok{ [}\DecValTok{7}\NormalTok{,}\DecValTok{8}\NormalTok{,}\DecValTok{9}\NormalTok{]])} +\end{Highlighting} +\end{Shaded} + +\emph{Note: for every array we nest, we get a new dimension in our data +structure.} + + \hypertarget{display-arrays}{% +\section{Display arrays}\label{display-arrays}} + +Using command print("") Accessing particular elements of an array +\ldots.. + + \hypertarget{practice-problem}{% +\section{Practice Problem}\label{practice-problem}} + +Problem statement + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{1}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k}{as} \PY{n+nn}{np} + +\PY{n}{x} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{7}\PY{p}{,} \PY{l+m+mi}{10} \PY{p}{,}\PY{l+m+mi}{12}\PY{p}{]}\PY{p}{)} + +\PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{)} + +\PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +[ 7 10 12] +10 + \end{Verbatim} + + \hypertarget{numpy-array-creation-functions}{% +\subsection{Numpy array creation +functions}\label{numpy-array-creation-functions}} + +Numpy comes with some built-in function that we can use to create arrays +quickly. Here are a couple of functions that are commonly used in +python. + +\hypertarget{np.arange}{% +\subsubsection{np.arange}\label{np.arange}} + +The \texttt{np.arange()} function returns an array with evenly spaced +values within a specified range. It is similar to the built-in +\texttt{range()} function in Python but returns a Numpy array instead of +a list. The parameters for this function are the start value +(inclusive), the stop value (exclusive), and the step size. If the step +size is not provided, it defaults to 1. + +\begin{Shaded} +\begin{Highlighting}[] +\OperatorTok{\textgreater{}\textgreater{}\textgreater{}}\NormalTok{ np.arange(}\DecValTok{4}\NormalTok{)} +\NormalTok{array([}\FloatTok{0.}\NormalTok{ , }\FloatTok{1.}\NormalTok{, }\FloatTok{2.}\NormalTok{, }\FloatTok{3.}\NormalTok{ ])} +\end{Highlighting} +\end{Shaded} + +In this example, \texttt{np.arange(4)} generates an array starting from +0 and ending before 4, with a step size of 1. + +\hypertarget{np.linspace}{% +\subsubsection{np.linspace}\label{np.linspace}} + +The \texttt{np.linspace()} function returns an array of evenly spaced +values over a specified range. Unlike \texttt{np.arange()}, which uses a +step size to define the spacing between elements, \texttt{np.linspace()} +uses the number of values you want to generate and calculates the +spacing automatically. It accepts three parameters: the start value, the +stop value, and the number of samples. + +\begin{Shaded} +\begin{Highlighting}[] +\OperatorTok{\textgreater{}\textgreater{}\textgreater{}}\NormalTok{ np.linspace(}\FloatTok{1.}\NormalTok{, }\FloatTok{4.}\NormalTok{, }\DecValTok{6}\NormalTok{)} +\NormalTok{array([}\FloatTok{1.}\NormalTok{ , }\FloatTok{1.6}\NormalTok{, }\FloatTok{2.2}\NormalTok{, }\FloatTok{2.8}\NormalTok{, }\FloatTok{3.4}\NormalTok{, }\FloatTok{4.}\NormalTok{ ])} +\end{Highlighting} +\end{Shaded} + +In this example, \texttt{np.linspace(1.,\ 4.,\ 6)} generates 6 evenly +spaced values between 1. and 4., including both endpoints. + +Try this and see what happens: + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=}\NormalTok{ np.linspace(}\DecValTok{0}\NormalTok{,}\DecValTok{100}\NormalTok{,}\DecValTok{101}\NormalTok{)} +\NormalTok{y }\OperatorTok{=}\NormalTok{ np.sin(x)} +\end{Highlighting} +\end{Shaded} + +\hypertarget{other-useful-functions}{% +\subsubsection{Other useful functions}\label{other-useful-functions}} + +\begin{itemize} +\tightlist +\item + \texttt{np.zeros()} +\item + \texttt{np.ones()} +\item + \texttt{np.eye()} +\end{itemize} + + \hypertarget{practice-problem}{% +\subsection{Practice problem}\label{practice-problem}} + +Problem statement below + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{2}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n}{y}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{linspace}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{,}\PY{l+m+mi}{20}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{y}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +[10. 12.5 15. 17.5 20. ] + \end{Verbatim} + + \hypertarget{working-with-arrays}{% +\subsection{Working with Arrays}\label{working-with-arrays}} + +Now that we have been introduced to some ways to create arrays using the +Numpy functions let's start using them. + +\hypertarget{indexing}{% +\subsubsection{Indexing}\label{indexing}} + +Indexing in Python allows you to access specific elements within an +array based on their position. This means you can directly retrieve and +manipulate individual items as needed. + +Python uses \textbf{zero-based indexing}, meaning the first element is +at position \textbf{0} rather than \textbf{1}. This approach is common +in many programming languages. For example, in a list with five +elements, the first element is at index \texttt{0}, followed by elements +at indices \texttt{1}, \texttt{2}, \texttt{3}, and \texttt{4}. + +Here's an example of data from a rocket test stand where thrust was +recorded as a function of time. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{thrust\_lbf }\OperatorTok{=}\NormalTok{ np.array(}\FloatTok{0.603355}\NormalTok{, }\FloatTok{2.019083}\NormalTok{, }\FloatTok{2.808092}\NormalTok{, }\FloatTok{4.054973}\NormalTok{, }\FloatTok{1.136618}\NormalTok{, }\FloatTok{0.943668}\NormalTok{)} + +\OperatorTok{\textgreater{}\textgreater{}\textgreater{}}\NormalTok{ thrust\_lbs[}\DecValTok{3}\NormalTok{]} +\end{Highlighting} +\end{Shaded} + +Due to the nature of zero-based indexing. If we want to call the value +\texttt{4.054973} that will be the 3rd index. + +\hypertarget{operations-on-arrays}{% +\subsubsection{Operations on arrays}\label{operations-on-arrays}} + +\begin{itemize} +\tightlist +\item + Arithmetic operations (\texttt{+}, \texttt{-}, \texttt{*}, \texttt{/}, + \texttt{**}) +\item + \texttt{np.add()}, \texttt{np.subtract()}, \texttt{np.multiply()}, + \texttt{np.divide()} +\item + \texttt{np.dot()} for dot product +\item + \texttt{np.matmul()} for matrix multiplication +\item + \texttt{np.linalg.inv()}, \texttt{np.linalg.det()} for linear algebra +\end{itemize} + +\hypertarget{statistics}{% +\paragraph{Statistics}\label{statistics}} + +\begin{itemize} +\tightlist +\item + \texttt{np.mean()}, \texttt{np.median()}, \texttt{np.std()}, + \texttt{np.var()} +\item + \texttt{np.min()}, \texttt{np.max()}, \texttt{np.argmin()}, + \texttt{np.argmax()} +\item + Summation along axes: \texttt{np.sum(arr,\ axis=0)} +\end{itemize} + +\hypertarget{combining-arrays}{% +\paragraph{Combining arrays}\label{combining-arrays}} + +\begin{itemize} +\tightlist +\item + Concatenation: \texttt{np.concatenate((arr1,\ arr2),\ axis=0)} +\item + Stacking: \texttt{np.vstack()}, \texttt{np.hstack()} +\item + Splitting: \texttt{np.split()} +\end{itemize} + + \hypertarget{exercise}{% +\section{Exercise}\label{exercise}} + +Let's solve a statics problem given the following problem + +A simply supported bridge of length L=20L = 20L=20 m is subjected to +three point loads: + +\begin{itemize} +\tightlist +\item + \(P1=1010 kN\) at \(x=5m\) +\item + \(P2=15 kN\) at \(x=10m\) +\item + \(P3=20 kN\) at \(x=15m\) +\end{itemize} + +The bridge is supported by two reaction forces at points AAA (left +support) and BBB (right support). We assume the bridge is in static +equilibrium, meaning the sum of forces and sum of moments about any +point must be zero. + +\hypertarget{equilibrium-equations}{% +\paragraph{Equilibrium Equations:}\label{equilibrium-equations}} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\item + \textbf{Sum of Forces in the Vertical Direction}: + + \(R_A + R_B - P_1 - P_2 - P_3 = 0\) +\item + \textbf{Sum of Moments About Point A}: + + \(5 P_1 + 10 P_2 + 15 P_3 - 20 R_B = 0\) +\item + \textbf{Sum of Moments About Point B}: + + \(20 R_A - 15 P_3 - 10 P_2 - 5 P_1 = 0\) +\end{enumerate} + +\hypertarget{system-of-equations}{% +\paragraph{System of Equations:}\label{system-of-equations}} + +\[ +\begin{cases} +R_A + R_B - 10 - 15 - 20 = 0 \\ +5 \cdot 10 + 10 \cdot 15 + 15 \cdot 20 - 20 R_B = 0 \\ +20 R_A - 5 \cdot 10 - 10 \cdot 15 - 15 \cdot 20 = 0 +\end{cases} +\] + + \hypertarget{solution}{% +\subsubsection{Solution}\label{solution}} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{3}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k}{as} \PY{n+nn}{np} + +\PY{c+c1}{\PYZsh{} Define the coefficient matrix A} +\PY{n}{A} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[} + \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,} + \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{20}\PY{p}{]}\PY{p}{,} + \PY{p}{[}\PY{l+m+mi}{20}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]} +\PY{p}{]}\PY{p}{)} + +\PY{c+c1}{\PYZsh{} Define the right\PYZhy{}hand side vector b} +\PY{n}{b} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[} + \PY{l+m+mi}{45}\PY{p}{,} + \PY{l+m+mi}{5}\PY{o}{*}\PY{l+m+mi}{10} \PY{o}{+} \PY{l+m+mi}{10}\PY{o}{*}\PY{l+m+mi}{15} \PY{o}{+} \PY{l+m+mi}{15}\PY{o}{*}\PY{l+m+mi}{20}\PY{p}{,} + \PY{l+m+mi}{5}\PY{o}{*}\PY{l+m+mi}{10} \PY{o}{+} \PY{l+m+mi}{10}\PY{o}{*}\PY{l+m+mi}{15} \PY{o}{+} \PY{l+m+mi}{15}\PY{o}{*}\PY{l+m+mi}{20} +\PY{p}{]}\PY{p}{)} + +\PY{c+c1}{\PYZsh{} Solve the system of equations Ax = b} +\PY{c+c1}{\PYZsh{} Using least squares to handle potential overdetermination} +\PY{n}{x} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{linalg}\PY{o}{.}\PY{n}{lstsq}\PY{p}{(}\PY{n}{A}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{rcond}\PY{o}{=}\PY{k+kc}{None}\PY{p}{)}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} + +\PY{c+c1}{\PYZsh{} Display the results} +\PY{n+nb}{print}\PY{p}{(}\PY{l+s+sa}{f}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Reaction force at A (R\PYZus{}A): }\PY{l+s+si}{\PYZob{}}\PY{n}{x}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{l+s+si}{:}\PY{l+s+s2}{.2f}\PY{l+s+si}{\PYZcb{}}\PY{l+s+s2}{ kN}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{l+s+sa}{f}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Reaction force at B (R\PYZus{}B): }\PY{l+s+si}{\PYZob{}}\PY{n}{x}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{l+s+si}{:}\PY{l+s+s2}{.2f}\PY{l+s+si}{\PYZcb{}}\PY{l+s+s2}{ kN}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +Reaction force at A (R\_A): 25.11 kN +Reaction force at B (R\_B): -24.89 kN + \end{Verbatim} + + + % Add a bibliography block to the postdoc diff --git a/book/module1/basics_of_python.tex b/book/module1/basics_of_python.tex new file mode 100644 index 0000000..64883e7 --- /dev/null +++ b/book/module1/basics_of_python.tex @@ -0,0 +1,289 @@ + \maketitle + + + + + \hypertarget{basics-of-python}{ +\section{Basics of Python}\label{basics-of-python}} + +This page contains important fundamental concepts used in Python such as +syntax, operators, order or precedence and more. + + \hypertarget{syntax}{% +\subsection{Syntax}\label{syntax}} + +\hypertarget{indentations-and-blocks}{% +\subsubsection{Indentations and blocks}\label{indentations-and-blocks}} + +In python \emph{indentations} or the space at the start of each line, +signifies a block of code. This becomes important when we start working +with function and loops. We will talk more about this in the controls +structures tutorial. + +\hypertarget{comments}{% +\subsubsection{Comments}\label{comments}} + +Comments can be added to your code using the hash operator (\#). Any +text behind the comment operator till the end of the line will be +rendered as a comment. If you have an entire block of text or code that +needs to be commented out, the triple quotation marks (""") can be used. +Once used all the code after it will be considered a comment until the +comment is ended with the triple quotation marks. + + \hypertarget{operators}{% +\subsection{Operators}\label{operators}} + +In python, operators are special symbols or keywords that perform +operations on values or variables. This section covers some of the most +common operator that you will see in this course. + +\hypertarget{arithmetic-operators}{% +\subsubsection{Arithmetic operators}\label{arithmetic-operators}} + +\begin{longtable}[]{@{}ll@{}} +\toprule +Operator & Name \\ +\midrule +\endhead ++ & Addition \\ +- & Subtraction \\ +* & Multiplication \\ +/ & Division \\ +\% & Modulus \\ +** & Exponentiation \\ +// & Floor division \\ +\bottomrule +\end{longtable} + +\hypertarget{comparison-operators}{% +\subsubsection{Comparison operators}\label{comparison-operators}} + +Used in conditional statements such as \texttt{if} statements or +\texttt{while} loops. Note that in the computer world a double equal +sign (\texttt{==}) means \emph{is equal to}, where as the single equal +sign assigns the variable or defines the variable to be something. + +\begin{longtable}[]{@{}ll@{}} +\toprule +Operator & Name \\ +\midrule +\endhead +== & Equal \\ +!= & Not equal \\ +\textgreater{} & Greater than \\ +\textless{} & Less than \\ +\textgreater= & Greater than or equal to \\ +\textless= & Less than or equal to \\ +\bottomrule +\end{longtable} + +\hypertarget{logical-operators}{% +\subsubsection{Logical operators}\label{logical-operators}} + +\begin{longtable}[]{@{}ll@{}} +\toprule +Operator & Descrription \\ +\midrule +\endhead +\texttt{and} & Returns True if both statemetns are true \\ +\texttt{or} & Returns True if one of the statements is true \\ +\texttt{not} & Reerse the result, returns False if the result is true \\ +\bottomrule +\end{longtable} + +\hypertarget{identity-operators}{% +\subsubsection{Identity operators}\label{identity-operators}} + +\begin{longtable}[]{@{}ll@{}} +\toprule +Operator & Description \\ +\midrule +\endhead +is & Returns True if both variables are the same object \\ +is not & Returns True if both variables are not the same object \\ +\bottomrule +\end{longtable} + + \hypertarget{order-of-operation}{% +\subsection{Order of Operation}\label{order-of-operation}} + +Similarly to the order or precedence in mathematics, different computer +languages have their own set of rules. Here is a comprehensive table of +the order of operation that python follows. + +\begin{longtable}[]{@{} + >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.51}} + >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.49}}@{}} +\toprule +Operator & Description \\ +\midrule +\endhead +\texttt{()} & Parentheses \\ +\texttt{**} & Exponentiation \\ +\texttt{+x} \texttt{-x} \texttt{\textasciitilde{}x} & Unary plus, unary +minus, and bitwise NOT \\ +\texttt{*} \texttt{/} \texttt{//} \texttt{\%} & Multiplication, +Division, floor division, and modulus \\ +\texttt{+} \texttt{-} & Addition and subtraction \\ +\texttt{\textless{}\textless{}} \texttt{\textgreater{}\textgreater{}} & +Bitwise left and right shifts \\ +\& & Bitwise AND \\ +\^{} & Bitwise XOR \\ +\textbar{} & Bitwise OR \\ +\texttt{==} \texttt{!=} \texttt{\textgreater{}} \texttt{\textgreater{}=} +\texttt{\textless{}} \texttt{\textless{}=} \texttt{is} \texttt{is\ not} +\texttt{in} \texttt{not\ in} & Comparision, identity and membership +operators \\ +\texttt{not} & logical NOT \\ +\texttt{and} & AND \\ +\texttt{or} & OR \\ +\bottomrule +\end{longtable} + + \hypertarget{data-types}{% +\subsection{Data types}\label{data-types}} + +Data types are different ways a computer stores data. Other data types +use fewer bits than others allowing you to better utilize your computer +memory. This is important for engineers because The most common data +types that an engineer encounters in python are numeric types. - +\texttt{int} - integer - \texttt{float} - a decimal number - +\texttt{complex} - imaginary number + +The comprehensive table below show all built-in data types available in +python. + +\begin{longtable}[]{@{}ll@{}} +\toprule +Category & Data Type \\ +\midrule +\endhead +Text & int, float, complex \\ +Sequance & list, tuple, range \\ +Mapping & dict \\ +Set & set, frozenset \\ +Boolean & bytes, bytearray, memoryview \\ +Binary & bytes, bytearray, memoryview \\ +None & NoneType \\ +\bottomrule +\end{longtable} + + \hypertarget{variables}{% +\subsection{Variables}\label{variables}} + +A \textbf{variable} in Python is a name that stores a value, allowing +you to use and manipulate data efficiently. + +\hypertarget{declaring-and-assigning-variables}{% +\paragraph{Declaring and Assigning +Variables}\label{declaring-and-assigning-variables}} + +It is common in low-level computer languages to declare the datatype if +the variable. In python, the datatype is set whilst you assign it. We +assign values to variables using a single \texttt{=}. + +```python + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{1}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n}{x} \PY{o}{=} \PY{l+m+mi}{33} \PY{c+c1}{\PYZsh{} Integer} +\PY{n}{y} \PY{o}{=} \PY{l+m+mf}{3.14} \PY{c+c1}{\PYZsh{} Float} +\PY{n}{name} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Joe}\PY{l+s+s2}{\PYZdq{}} \PY{c+c1}{\PYZsh{} String} +\PY{n}{is\PYZus{}valid} \PY{o}{=} \PY{k+kc}{True} \PY{c+c1}{\PYZsh{} Boolean} + +\PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{o}{+}\PY{l+m+mi}{3}\PY{o}{*}\PY{n}{y}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +1098.42 + \end{Verbatim} + + Change the x and y values above, re-run the cell to see what happens. + +You can assign multiple variables at once: + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{a, b, c }\OperatorTok{=} \DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3} +\end{Highlighting} +\end{Shaded} + +Similarly we can assign the same value to multiple variables: + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=}\NormalTok{ y }\OperatorTok{=}\NormalTok{ z }\OperatorTok{=} \DecValTok{100} +\end{Highlighting} +\end{Shaded} + + \hypertarget{rules}{% +\subparagraph{Rules}\label{rules}} + +\begin{itemize} +\tightlist +\item + Must start with a letter or \texttt{\_} +\item + Cannot start with a number +\item + Can only contain letters, numbers, and \texttt{\_} +\item + Case-sensitive (\texttt{Name} and \texttt{name} are different) +\end{itemize} + +\hypertarget{updating-variables}{% +\paragraph{Updating Variables}\label{updating-variables}} + +You can change a variable's value at any time. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=} \DecValTok{5} +\NormalTok{x }\OperatorTok{=}\NormalTok{ x }\OperatorTok{+} \DecValTok{10} \CommentTok{\# Now x is 15} +\end{Highlighting} +\end{Shaded} + +Or shorthand: + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{+=} \DecValTok{10} \CommentTok{\# Same as x = x + 10} +\end{Highlighting} +\end{Shaded} + +\hypertarget{variable-types-type-checking}{% +\paragraph{Variable Types \& Type +Checking}\label{variable-types-type-checking}} + +Use \texttt{type()} to check a variable's type. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{2}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n}{x} \PY{o}{=} \PY{l+m+mi}{10} +\PY{n+nb}{print}\PY{p}{(}\PY{l+s+sa}{f}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{ Variable x is type: }\PY{l+s+si}{\PYZob{}}\PY{n+nb}{type}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{l+s+si}{\PYZcb{}}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)} + +\PY{n}{y} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Hello}\PY{l+s+s2}{\PYZdq{}} +\PY{n+nb}{print}\PY{p}{(}\PY{l+s+sa}{f}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{ Variable y is type: }\PY{l+s+si}{\PYZob{}}\PY{n+nb}{type}\PY{p}{(}\PY{n}{y}\PY{p}{)}\PY{l+s+si}{\PYZcb{}}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] + Variable x is type: <class 'int'> + Variable y is type: <class 'str'> + \end{Verbatim} + + \hypertarget{exercise}{% +\section{Exercise}\label{exercise}} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{ }{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] + +\end{Verbatim} +\end{tcolorbox} + + + % Add a bibliography block to the postdoc diff --git a/book/module1/module1.aux b/book/module1/module1.aux new file mode 100644 index 0000000..4acdee2 --- /dev/null +++ b/book/module1/module1.aux @@ -0,0 +1,140 @@ +\relax +\providecommand\hyper@newdestlabel[2]{} +\@writefile{toc}{\contentsline {chapter}{\numberline {2}Module 1}{5}{chapter.2}\protected@file@percent } +\@writefile{lof}{\addvspace {10\p@ }} +\@writefile{lot}{\addvspace {10\p@ }} +\gdef \LT@ii {\LT@entry + {1}{49.24928pt}\LT@entry + {2}{79.04018pt}} +\gdef \LT@iii {\LT@entry + {1}{49.24928pt}\LT@entry + {2}{123.12889pt}} +\@writefile{toc}{\contentsline {section}{\numberline {2.1}Basics of Python}{7}{section.2.1}\protected@file@percent } +\newlabel{basics-of-python}{{2.1}{7}{Basics of Python}{section.2.1}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.1.1}Syntax}{7}{subsection.2.1.1}\protected@file@percent } +\newlabel{syntax}{{2.1.1}{7}{Syntax}{subsection.2.1.1}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Indentations and blocks}{7}{subsubsection*.10}\protected@file@percent } +\newlabel{indentations-and-blocks}{{2.1.1}{7}{Indentations and blocks}{subsubsection*.10}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Comments}{7}{subsubsection*.12}\protected@file@percent } +\newlabel{comments}{{2.1.1}{7}{Comments}{subsubsection*.12}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.1.2}Operators}{7}{subsection.2.1.2}\protected@file@percent } +\newlabel{operators}{{2.1.2}{7}{Operators}{subsection.2.1.2}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Arithmetic operators}{7}{subsubsection*.14}\protected@file@percent } +\newlabel{arithmetic-operators}{{2.1.2}{7}{Arithmetic operators}{subsubsection*.14}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Comparison operators}{7}{subsubsection*.16}\protected@file@percent } +\newlabel{comparison-operators}{{2.1.2}{7}{Comparison operators}{subsubsection*.16}{}} +\gdef \LT@iv {\LT@entry + {1}{49.24928pt}\LT@entry + {2}{247.13966pt}} +\gdef \LT@v {\LT@entry + {1}{49.24928pt}\LT@entry + {2}{266.75583pt}} +\gdef \LT@vi {\LT@entry + {1}{230.89124pt}\LT@entry + {1}{222.0767pt}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Logical operators}{8}{subsubsection*.18}\protected@file@percent } +\newlabel{logical-operators}{{2.1.2}{8}{Logical operators}{subsubsection*.18}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Identity operators}{8}{subsubsection*.20}\protected@file@percent } +\newlabel{identity-operators}{{2.1.2}{8}{Identity operators}{subsubsection*.20}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.1.3}Order of Operation}{8}{subsection.2.1.3}\protected@file@percent } +\newlabel{order-of-operation}{{2.1.3}{8}{Order of Operation}{subsection.2.1.3}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.1.4}Data types}{8}{subsection.2.1.4}\protected@file@percent } +\newlabel{data-types}{{2.1.4}{8}{Data types}{subsection.2.1.4}{}} +\gdef \LT@vii {\LT@entry + {2}{49.85371pt}\LT@entry + {2}{148.51701pt}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.1.5}Variables}{9}{subsection.2.1.5}\protected@file@percent } +\newlabel{variables}{{2.1.5}{9}{Variables}{subsection.2.1.5}{}} +\newlabel{declaring-and-assigning-variables}{{2.1.5}{9}{Declaring and Assigning Variables}{paragraph*.22}{}} +\@writefile{toc}{\contentsline {paragraph}{\nonumberline Declaring and Assigning Variables}{9}{paragraph*.22}\protected@file@percent } +\newlabel{rules}{{2.1.5}{9}{Rules}{subparagraph*.24}{}} +\@writefile{toc}{\contentsline {subparagraph}{\nonumberline Rules}{9}{subparagraph*.24}\protected@file@percent } +\newlabel{updating-variables}{{2.1.5}{9}{Updating Variables}{paragraph*.26}{}} +\@writefile{toc}{\contentsline {paragraph}{\nonumberline Updating Variables}{9}{paragraph*.26}\protected@file@percent } +\gdef \LT@viii {\LT@entry + {1}{11.44395pt}\LT@entry + {1}{17.44395pt}\LT@entry + {1}{17.44395pt}\LT@entry + {1}{11.44395pt}} +\gdef \LT@ix {\LT@entry + {1}{11.44395pt}\LT@entry + {1}{17.44395pt}\LT@entry + {1}{17.44395pt}\LT@entry + {1}{11.44395pt}} +\newlabel{variable-types-type-checking}{{2.1.5}{10}{Variable Types \& Type Checking}{paragraph*.28}{}} +\@writefile{toc}{\contentsline {paragraph}{\nonumberline Variable Types \& Type Checking}{10}{paragraph*.28}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {2.2}Exercise}{10}{section.2.2}\protected@file@percent } +\newlabel{exercise}{{2.2}{10}{Exercise}{section.2.2}{}} +\@writefile{toc}{\contentsline {section}{\numberline {2.3}matrixArrays}{10}{section.2.3}\protected@file@percent } +\newlabel{matrixarrays}{{2.3}{10}{matrixArrays}{section.2.3}{}} +\@writefile{toc}{\contentsline {section}{\numberline {2.4}Numpy - the python's array library}{10}{section.2.4}\protected@file@percent } +\newlabel{numpy---the-pythons-array-library}{{2.4}{10}{Numpy - the python's array library}{section.2.4}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.4.1}Importing Numpy}{11}{subsection.2.4.1}\protected@file@percent } +\newlabel{importing-numpy}{{2.4.1}{11}{Importing Numpy}{subsection.2.4.1}{}} +\@writefile{toc}{\contentsline {section}{\numberline {2.5}Creating arrays}{11}{section.2.5}\protected@file@percent } +\newlabel{creating-arrays}{{2.5}{11}{Creating arrays}{section.2.5}{}} +\@writefile{toc}{\contentsline {section}{\numberline {2.6}Display arrays}{11}{section.2.6}\protected@file@percent } +\newlabel{display-arrays}{{2.6}{11}{Display arrays}{section.2.6}{}} +\@writefile{toc}{\contentsline {section}{\numberline {2.7}Practice Problem}{11}{section.2.7}\protected@file@percent } +\newlabel{practice-problem}{{2.7}{11}{Practice Problem}{section.2.7}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.7.1}Numpy array creation functions}{12}{subsection.2.7.1}\protected@file@percent } +\newlabel{numpy-array-creation-functions}{{2.7.1}{12}{Numpy array creation functions}{subsection.2.7.1}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline np.arange}{12}{subsubsection*.30}\protected@file@percent } +\newlabel{np.arange}{{2.7.1}{12}{np.arange}{subsubsection*.30}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline np.linspace}{12}{subsubsection*.32}\protected@file@percent } +\newlabel{np.linspace}{{2.7.1}{12}{np.linspace}{subsubsection*.32}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Other useful functions}{12}{subsubsection*.34}\protected@file@percent } +\newlabel{other-useful-functions}{{2.7.1}{12}{Other useful functions}{subsubsection*.34}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.7.2}Practice problem}{12}{subsection.2.7.2}\protected@file@percent } +\newlabel{practice-problem}{{2.7.2}{12}{Practice problem}{subsection.2.7.2}{}} +\@writefile{toc}{\contentsline {subsection}{\numberline {2.7.3}Working with Arrays}{13}{subsection.2.7.3}\protected@file@percent } +\newlabel{working-with-arrays}{{2.7.3}{13}{Working with Arrays}{subsection.2.7.3}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Indexing}{13}{subsubsection*.36}\protected@file@percent } +\newlabel{indexing}{{2.7.3}{13}{Indexing}{subsubsection*.36}{}} +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Operations on arrays}{13}{subsubsection*.38}\protected@file@percent } +\newlabel{operations-on-arrays}{{2.7.3}{13}{Operations on arrays}{subsubsection*.38}{}} +\newlabel{statistics}{{2.7.3}{13}{Statistics}{paragraph*.40}{}} +\@writefile{toc}{\contentsline {paragraph}{\nonumberline Statistics}{13}{paragraph*.40}\protected@file@percent } +\newlabel{combining-arrays}{{2.7.3}{13}{Combining arrays}{paragraph*.42}{}} +\@writefile{toc}{\contentsline {paragraph}{\nonumberline Combining arrays}{13}{paragraph*.42}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {2.8}Exercise}{13}{section.2.8}\protected@file@percent } +\newlabel{exercise}{{2.8}{13}{Exercise}{section.2.8}{}} +\newlabel{equilibrium-equations}{{2.8}{14}{Equilibrium Equations:}{paragraph*.44}{}} +\@writefile{toc}{\contentsline {paragraph}{\nonumberline Equilibrium Equations:}{14}{paragraph*.44}\protected@file@percent } +\newlabel{system-of-equations}{{2.8}{14}{System of Equations:}{paragraph*.46}{}} +\@writefile{toc}{\contentsline {paragraph}{\nonumberline System of Equations:}{14}{paragraph*.46}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{\nonumberline Solution}{14}{subsubsection*.48}\protected@file@percent } +\newlabel{solution}{{2.8}{14}{Solution}{subsubsection*.48}{}} +\@setckpt{module1/module1}{ +\setcounter{page}{15} +\setcounter{equation}{0} +\setcounter{enumi}{3} +\setcounter{enumii}{0} +\setcounter{enumiii}{0} +\setcounter{enumiv}{0} +\setcounter{footnote}{0} +\setcounter{mpfootnote}{0} +\setcounter{part}{0} +\setcounter{chapter}{2} +\setcounter{section}{8} +\setcounter{subsection}{0} +\setcounter{subsubsection}{0} +\setcounter{paragraph}{0} +\setcounter{subparagraph}{0} +\setcounter{figure}{0} +\setcounter{table}{8} +\setcounter{tcbbreakpart}{1} +\setcounter{tcblayer}{0} +\setcounter{tcolorbox@number}{6} +\setcounter{caption@flags}{2} +\setcounter{continuedfloat}{0} +\setcounter{float@type}{8} +\setcounter{parentequation}{0} +\setcounter{FancyVerbLine}{2} +\setcounter{section@level}{3} +\setcounter{Item}{16} +\setcounter{Hfootnote}{0} +\setcounter{bookmark@seq@number}{0} +\setcounter{LT@tables}{9} +\setcounter{LT@chunks}{2} +} diff --git a/book/module1/module1.tex b/book/module1/module1.tex new file mode 100644 index 0000000..cecf097 --- /dev/null +++ b/book/module1/module1.tex @@ -0,0 +1,3 @@ +\chapter{Module 1} +\input{module1/basics_of_python} +\input{module1/arrays} |
