summaryrefslogtreecommitdiff
path: root/book/module1/basics_of_python.tex
blob: 4b9db0599ab46830837f3347a91d94e43a988763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
\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. \#\#\# 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. \#\#\# 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{}

\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}