From ec6f7800ee9afd2cf354b05883502a0a327561d2 Mon Sep 17 00:00:00 2001 From: Christian Kolset Date: Tue, 22 Apr 2025 16:26:11 -0600 Subject: Added latex files --- .../module_1/latex/basics_of_python_chapter.tex | 285 +++++++++++++++++++++ 1 file changed, 285 insertions(+) create mode 100644 tutorials/module_1/latex/basics_of_python_chapter.tex (limited to 'tutorials/module_1/latex/basics_of_python_chapter.tex') diff --git a/tutorials/module_1/latex/basics_of_python_chapter.tex b/tutorials/module_1/latex/basics_of_python_chapter.tex new file mode 100644 index 0000000..cceac35 --- /dev/null +++ b/tutorials/module_1/latex/basics_of_python_chapter.tex @@ -0,0 +1,285 @@ + +\chapter{Basics of Python} + + + \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: + Variable y is type: + \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} -- cgit v1.2.3