-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsec-operator.ptx
364 lines (318 loc) · 9.06 KB
/
sec-operator.ptx
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?xml version="1.0" encoding="UTF-8"?>
<!--*****************************************
This is part of Basic Programming
Copyright (C) 2024
Phạm Công Vinh
See the file COPYING for copying conditions.
******************************************-->
<section xml:id="sec-operator" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Operators</title>
<objectives>
<ul>
<li>Learn about four main types of operators.</li>
<li>Understand assignment operators through an example.</li>
</ul>
</objectives>
<introduction>
<definition xml:id="def-operator">
<idx><h>Definitions</h><h>of operators</h></idx>
<statement>
<p>
In programming, an <term>operator</term> is a symbol or keyword that describes a specific assignment or a mathematical, relational, or logical operation.
</p>
<p>
In other words, an operator describes an interaction between data.
</p>
</statement>
</definition>
<p>
Previously, the two operators <c>=</c> and <c>+</c> often show up in examples. Let's take a look at the most basic operators.
</p>
</introduction>
<subsection xml:id="subsec-op-arithmetic">
<title>Arithmetic Operators</title>
<p>
The four most basic arithmetic operators are:
</p>
<aside>
<title>Try It Out</title>
<p>
What operators do you think work with <xref ref="subsec-list" text="title" />?
</p>
</aside>
<sage language="python">
<input>
print(5 + 3) # Addition
print(5 - 3) # Subtraction
print(5 * 3) # Multiplication
print(5 / 3) # Division
# String concatenation
print("string 1 " + "string 2")
</input>
</sage>
<problem>
<pre>
8
2
15
1.6666666666666667
string 1 string 2
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>arithmetic operators</h></idx>
<idx><h>Code examples</h><h>string concatenation</h></idx>
<p></p>
<p>
The first four lines work just the same as mathematical operations.
</p>
<p>
In line 7, the operator <c>+</c> represents string concatenation (technically not arithmetic but I don't know where else to put it lol).
</p>
</investigation>
<p>
Some less popular operators are:
</p>
<sage language="python">
<input>
print(5 // 3) # Floor division
print(5 ** 3) # Exponentiation
print(5 % 3) # Modulus
</input>
</sage>
<problem>
<pre>
1
125
2
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>arithmetic operations</h></idx>
<p></p>
<p>
You can look them up on the Internet to see how they work.
</p>
</investigation>
<p>
And you should always keep in mind <xref ref="def-dtype-interaction"/>.
</p>
</subsection>
<subsection xml:id="subsec-op-assignment">
<title>Assignment Operators</title>
<p>
You've seen <c>=</c> being used very frequently. This is the most important <term>assignment operator</term> that you need to understand.
</p>
<definition xml:id="def-op-assignment">
<idx><h>Definitions</h><h>of assignment operations</h></idx>
<statement>
<p>
An <term>assignment operator</term> is used to declare or update a variable.
</p>
<p>
The value of a variable changes <em>if and only if</em> an assignment operator is used.
</p>
</statement>
</definition>
<note>
<idx><h>Notes</h><h>on equality</h></idx>
<p>
In programming, the assignment operator <c>=</c> should never be associated with equality.
</p>
</note>
<p>For example (using CodeLens is recommended):</p>
<program language="python" interactive="activecode">
<input>
a = 5
print(a)
a + 1
print(a)
a = a + 1
print(a)
</input>
</program>
<problem>
<pre>
5
5
6
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>declaration</h></idx>
<idx><h>Code examples</h><h>assignment</h></idx>
<idx><h>Code examples</h><h>updating variables</h></idx>
<p></p>
<p>
Firstly, we declare <c>a</c> and give it the value <c>5</c>.
</p>
<p>
In line 4, we have <c>a + 1</c>, but when we access the value of <c>a</c> in line 5, we still get <c>5</c>.
</p>
<p>
In line 7, we <em>update</em> the variable with <c>a = a + 1</c>. Therefore, in line 8 <c>a</c> has a value of <c>6</c>.
</p>
</investigation>
<p>
<idx><h>Notes</h><h>on assignment operators</h></idx>
Theoretically, <c>=</c> is the only assignment operator you need to know. But we also have others that serve as shorthands:
</p>
<aside>
<title>Try It Out</title>
<p>
Try using these.
</p>
</aside>
<p>
<dl>
<li>
<title><c>x += 5</c></title>
<p>
Is equivalent to <c>x = x + 5</c>
</p>
</li>
<li>
<title><c>x -= 5</c></title>
<p>
Is equivalent to <c>x = x - 5</c>
</p>
</li>
<li>
<title><c>x *= 5</c></title>
<p>
Is equivalent to <c>x = x * 5</c>
</p>
</li>
<li>
<title><c>x /= 5</c></title>
<p>
Is equivalent to <c>x = x / 5</c>
</p>
</li>
</dl>
</p>
</subsection>
<subsection xml:id="subsec-op-relational">
<title>Relational Operators</title>
<p>
<term>Relational operators</term> are mostly used in conditional statements, so they will be described in more detail in <xref ref="sec-conditional" text="type-local"/>.
</p>
<aside>
<title>Topic(s) you might be interested in</title>
<p>
<ul>
<li>
<p>
<idx><h>Links</h><h>Python = vs ==</h></idx>
<url href="https://www.google.com/search?q=Python+=+vs+==" visual="google.com/search?q=Python+=+vs+==">"Python = vs =="</url>
</p>
</li>
</ul>
</p>
</aside>
<p>
<idx><h>Overviews</h><h>of relational operators</h></idx>
As an overview:
<dl>
<li>
<title><c>x == y</c></title>
<p>
Returns <c>True</c> if x equals y
</p>
<p>
Returns <c>False</c> if x does <em>not</em> equal y
</p>
</li>
<li>
<title><c>x != y</c></title>
<p>
Opposite to <c>x == y</c>
</p>
</li>
<li>
<title><c>x < y</c></title>
<p>
Returns <c>True</c> if x is less than y
</p>
<p>
Returns <c>False</c> if x is greater than or equal to y
</p>
</li>
<li>
<title><c>x > y </c></title>
<p>
Returns <c>True</c> if x is greater than y
</p>
<p>
Returns <c>False</c> if x is less than or equal to y
</p>
</li>
<li>
<title><c>x <= y </c></title>
<p>
Opposite to <c>x > y</c>
</p>
</li>
<li>
<title><c>x >= y</c></title>
<p>
Opposite to <c>x < y</c>
</p>
</li>
</dl>
</p>
</subsection>
<subsection xml:id="subsec-op-logical">
<title>Logical Operators</title>
<p>
<term>Logical operators</term> are mostly used in conditional statements, so they will be described in more detail in <xref ref="sec-conditional" text="type-local"/>.
</p>
<p>
<idx><h>Overviews</h><h>of logical operators</h></idx>
As an overview:
<dl>
<li>
<title><c>x and y</c></title>
<p>
Returns <c>True</c> if both statements are <c>True</c>
</p>
<p>
Returns <c>False</c> if at least one statement is <c>False</c>
</p>
</li>
<li>
<title><c>x or y</c></title>
<p>
Returns <c>True</c> if at least one statement is <c>True</c>
</p>
<p>
Returns <c>False</c> if both statements are <c>False</c>
</p>
</li>
<li>
<title><c>not x</c></title>
<p>
Reverses the Boolean
</p>
<p>
Returns <c>True</c> if x is equal to <c>False</c> and vice-versa.
</p>
</li>
</dl>
</p>
</subsection>
<conclusion>
<p>
<cd>
</cd>
</p>
<exploration>
<title>Basic Programming <mdash /> Part 8: Operators</title>
<idx><h>Videos</h><h>part 08</h></idx>
<p>
Coming soon.
</p>
<video youtubeplaylist="PLBLdRr-v59vwnKvmvLtcgmAnsb2K1Ta_M" />
</exploration>
</conclusion>
</section>