diff options
Diffstat (limited to 'book/module1/basics_of_python.tex')
| -rw-r--r-- | book/module1/basics_of_python.tex | 200 |
1 files changed, 75 insertions, 125 deletions
diff --git a/book/module1/basics_of_python.tex b/book/module1/basics_of_python.tex index 29f8cc4..4b9db05 100644 --- a/book/module1/basics_of_python.tex +++ b/book/module1/basics_of_python.tex @@ -1,57 +1,37 @@ - \hypertarget{basics-of-python}{ -\section{Basics of Python}\label{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}} +\subsection{Syntax}\label{syntax} -\hypertarget{indentations-and-blocks}{% -\subsubsection{Indentations and blocks}\label{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. +structures tutorial. \#\#\# 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.f -\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}} +\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} +common operator that you will see in this course. \#\#\# Arithmetic +operators \textbar{} Operator \textbar{} Name \textbar{} \textbar{} --- +\textbar{} --- \textbar{} \textbar{} + \textbar{} Addition \textbar{} +\textbar{} - \textbar{} Subtraction \textbar{} \textbar{} * \textbar{} +Multiplication \textbar{} \textbar{} / \textbar{} Division \textbar{} +\textbar{} \% \textbar{} Modulus \textbar{} \textbar{} ** \textbar{} +Exponentiation \textbar{} \textbar{} // \textbar{} Floor division +\textbar{} -\hypertarget{comparison-operators}{% -\subsubsection{Comparison operators}\label{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 @@ -59,60 +39,66 @@ 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 +\toprule\noalign{} Operator & Name \\ -\midrule +\midrule\noalign{} \endhead +\bottomrule\noalign{} +\endlastfoot == & 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}} +\subsubsection{Logical operators}\label{logical-operators} \begin{longtable}[]{@{}ll@{}} -\toprule +\toprule\noalign{} Operator & Descrription \\ -\midrule +\midrule\noalign{} \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 +\bottomrule\noalign{} +\endlastfoot +and & Returns True if both statemetns are true \\ +or & Returns True if one of the statements is true \\ +not & Reerse the result, returns False if the result is true \\ \end{longtable} -\hypertarget{identity-operators}{% -\subsubsection{Identity operators}\label{identity-operators}} +\subsubsection{Identity operators}\label{identity-operators} \begin{longtable}[]{@{}ll@{}} -\toprule +\toprule\noalign{} Operator & Description \\ -\midrule +\midrule\noalign{} \endhead +\bottomrule\noalign{} +\endlastfoot 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}} +\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 + >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.5093}} + >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.4907}}@{}} +\toprule\noalign{} +\begin{minipage}[b]{\linewidth}\raggedright +Operator +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Description +\end{minipage} \\ +\midrule\noalign{} \endhead +\bottomrule\noalign{} +\endlastfoot \texttt{()} & Parentheses \\ \texttt{**} & Exponentiation \\ \texttt{+x} \texttt{-x} \texttt{\textasciitilde{}x} & Unary plus, unary @@ -132,11 +118,9 @@ operators \\ \texttt{not} & logical NOT \\ \texttt{and} & AND \\ \texttt{or} & OR \\ -\bottomrule \end{longtable} - \hypertarget{data-types}{% -\subsection{Data types}\label{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 @@ -149,10 +133,12 @@ The comprehensive table below show all built-in data types available in python. \begin{longtable}[]{@{}ll@{}} -\toprule +\toprule\noalign{} Category & Data Type \\ -\midrule +\midrule\noalign{} \endhead +\bottomrule\noalign{} +\endlastfoot Text & int, float, complex \\ Sequance & list, tuple, range \\ Mapping & dict \\ @@ -160,42 +146,28 @@ Set & set, frozenset \\ Boolean & bytes, bytearray, memoryview \\ Binary & bytes, bytearray, memoryview \\ None & NoneType \\ -\bottomrule \end{longtable} - \hypertarget{variables}{% -\subsection{Variables}\label{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}} +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. +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=} \DecValTok{10} \CommentTok{\# Integer} +\NormalTok{y }\OperatorTok{=} \FloatTok{3.14} \CommentTok{\# Float} +\NormalTok{name }\OperatorTok{=} \StringTok{"Joe"} \CommentTok{\# String} +\NormalTok{is\_valid }\OperatorTok{=} \VariableTok{True} \CommentTok{\# Boolean} +\end{Highlighting} +\end{Shaded} You can assign multiple variables at once: @@ -213,8 +185,7 @@ Similarly we can assign the same value to multiple variables: \end{Highlighting} \end{Shaded} - \hypertarget{rules}{% -\subparagraph{Rules}\label{rules}} +\subparagraph{Rules}\label{rules} \begin{itemize} \tightlist @@ -228,8 +199,7 @@ Similarly we can assign the same value to multiple variables: Case-sensitive (\texttt{Name} and \texttt{name} are different) \end{itemize} -\hypertarget{updating-variables}{% -\paragraph{Updating Variables}\label{updating-variables}} +\paragraph{Updating Variables}\label{updating-variables} You can change a variable's value at any time. @@ -248,37 +218,17 @@ Or shorthand: \end{Highlighting} \end{Shaded} -\hypertarget{variable-types-type-checking}{% \paragraph{Variable Types \& Type -Checking}\label{variable-types-type-checking}} +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} - +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=} \DecValTok{10} +\BuiltInTok{print}\NormalTok{(}\BuiltInTok{type}\NormalTok{(x)) }\CommentTok{\# Output: \textless{}class \textquotesingle{}int\textquotesingle{}\textgreater{}} - % Add a bibliography block to the postdoc +\NormalTok{y }\OperatorTok{=} \StringTok{"Hello"} +\BuiltInTok{print}\NormalTok{(}\BuiltInTok{type}\NormalTok{(y)) }\CommentTok{\# Output: \textless{}class \textquotesingle{}str\textquotesingle{}\textgreater{}} +\end{Highlighting} +\end{Shaded} |
