diff options
Diffstat (limited to 'book/module1/basics_of_python.tex')
| -rw-r--r-- | book/module1/basics_of_python.tex | 248 |
1 files changed, 0 insertions, 248 deletions
diff --git a/book/module1/basics_of_python.tex b/book/module1/basics_of_python.tex deleted file mode 100644 index a68400e..0000000 --- a/book/module1/basics_of_python.tex +++ /dev/null @@ -1,248 +0,0 @@ -\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. - -\subsection{Syntax}\label{syntax} - -\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. - -\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.f - -\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. - -\subsubsection{Arithmetic operators}\label{arithmetic-operators} - -\begin{longtable}[]{@{}ll@{}} -\toprule\noalign{} -Operator & Name \\ -\midrule\noalign{} -\endhead -\bottomrule\noalign{} -\endlastfoot -+ & Addition \\ -- & Subtraction \\ -* & Multiplication \\ -/ & Division \\ -\% & Modulus \\ -** & Exponentiation \\ -// & Floor division \\ -\end{longtable} - -\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\noalign{} -Operator & Name \\ -\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 \\ -\end{longtable} - -\subsubsection{Logical operators}\label{logical-operators} - -\begin{longtable}[]{@{}ll@{}} -\toprule\noalign{} -Operator & Descrription \\ -\midrule\noalign{} -\endhead -\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} - -\subsubsection{Identity operators}\label{identity-operators} - -\begin{longtable}[]{@{}ll@{}} -\toprule\noalign{} -Operator & Description \\ -\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 \\ -\end{longtable} - -\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.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 -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 \\ -\end{longtable} - -\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\noalign{} -Category & Data Type \\ -\midrule\noalign{} -\endhead -\bottomrule\noalign{} -\endlastfoot -Text & int, float, complex \\ -Sequance & list, tuple, range \\ -Mapping & dict \\ -Set & set, frozenset \\ -Boolean & bytes, bytearray, memoryview \\ -Binary & bytes, bytearray, memoryview \\ -None & NoneType \\ -\end{longtable} - -\subsection{Variables}\label{variables} - -A \textbf{variable} in Python is a name that stores a value, allowing -you to use and manipulate data efficiently. - -\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{=}. - -\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: - -\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} - -\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} - -\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} - -\paragraph{Variable Types \& Type -Checking}\label{variable-types-type-checking} - -Use \texttt{type()} to check a variable's type. - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{x }\OperatorTok{=} \DecValTok{10} -\BuiltInTok{print}\NormalTok{(}\BuiltInTok{type}\NormalTok{(x)) }\CommentTok{\# Output: \textless{}class \textquotesingle{}int\textquotesingle{}\textgreater{}} - -\NormalTok{y }\OperatorTok{=} \StringTok{"Hello"} -\BuiltInTok{print}\NormalTok{(}\BuiltInTok{type}\NormalTok{(y)) }\CommentTok{\# Output: \textless{}class \textquotesingle{}str\textquotesingle{}\textgreater{}} -\end{Highlighting} -\end{Shaded} |
