-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsec-data-type.ptx
337 lines (294 loc) · 8.33 KB
/
sec-data-type.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
<?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-data-type" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Data Types</title>
<objectives>
<ul>
<li>Learn about data types and how to use them.</li>
<li>Understand <term>interactions</term> between data types.</li>
</ul>
</objectives>
<p>
Pretty much everything in existence can be stored as data<mdash></mdash>from numbers and letters to information about a car model.
</p>
<definition xml:id="def-data-type">
<idx><h>Definitions</h><h>of data types</h></idx>
<statement>
<p>
And to <term>categorize</term> data, we make use of <term>data types</term>.
</p>
</statement>
</definition>
<p>
<idx><h>Overviews</h><h>of data types</h></idx>
Most modern programming languages have the following data types:
<dl>
<li>
<title>str</title>
<p>
<term>A sequence of characters</term>, enclosed in quotation marks <c>""</c> or single quotation marks <c>''</c>.
</p>
</li>
<li>
<title>int</title>
<p>
<term>An integer</term>, or a whole number (without a decimal point).
</p>
</li>
<li>
<title>float</title>
<p>
<term>A floating-point number</term> (with a decimal point).
</p>
</li>
<li>
<title>bool</title>
<p>
<term>A Boolean value</term>, which can only equate to either <c>True</c> or <c>False</c>. It's frequently used in conditional statements.
</p>
</li>
</dl>
</p>
<aside>
<title>Topic(s) you might be interested in</title>
<p>
<ul>
<li>
<p>
<idx><h>Links</h><h>Python is dynamically typed</h></idx>
<url href="https://www.google.com/search?q=Python+is+dynamically+typed" visual="google.com/search?q=Python+is+dynamically+typed">"Python is dynamically typed"</url>
</p>
</li>
</ul>
<ul>
<li>
<p>
<idx><h>Links</h><h>static vs dynamic typing</h></idx>
<url href="https://stackoverflow.com/a/27791387/19779259" visual="stackoverflow.com/a/27791387/19779259">example of static vs dynamic typing</url>
</p>
</li>
</ul>
</p>
</aside>
<note>
<idx><h>Notes</h><h>on data type determination</h></idx>
<p>
Python is a dynamically typed language. This means when a variable is declared or updated, its data type is <em>automatically</em> determined or re-determined respectively.
</p>
</note>
<p>
For example (it's recommended to use CodeLens):
</p>
<program language="python" interactive="activecode">
<input>
a = 5
b = type(a)
print(a, b)
a = "5"
b = type(a)
print(a, b)
</input>
</program>
<problem>
<pre>
5 <class 'int'>
5 <class 'str'>
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>data types</h></idx>
<idx><h>Functions</h><h>type()</h></idx>
<p></p>
<p>
The command <c>type(a)</c> returns the data type of <c>a</c> written in single quotation marks.
</p>
<p>
(You don't have to care about the word <c>class</c> yet. Here it can be understood as <q>classify</q>. But to be accurate, it's refering to <term>Class</term>, which is a more advanced topic discussed in <xref ref="sec-class"/>.)
</p>
<aside>
<title>Try It Out</title>
<p>
Is <c>5</c> different from <c>5.0</c>?
</p>
</aside>
<p>
So, when we declare <c>a = 5</c>, the variable <c>a</c> is of type <c>int</c>.
</p>
<p>
But when we assign <c>a</c> the value <c>"5"</c>, it automatically updates to <c>str</c>.
</p>
</investigation>
<p>
To conclude, <c>a = "5"</c> differs from <c>a = 5</c>. If data is surrounded by (single) quotation marks, then it is of type <c>str</c>. From that, we have the following:
</p>
<definition xml:id="def-dtype-syntax">
<idx><h>Definitions</h><h>of data type syntax</h></idx>
<statement>
<p>
Different data types have different <term>syntax</term>.
</p>
</statement>
</definition>
<p>Some examples of the main data types:</p>
<sage language="python">
<input>
a = 3
b = 3.0
c = "3.0"
d = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
</input>
</sage>
<problem>
<pre>
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>data types</h></idx>
<p></p>
<p>
Same as above.
</p>
<p>
Again, you don't have to care about <c>class</c> yet.
</p>
</investigation>
<p>
Next, we have:
</p>
<definition xml:id="def-dtype-interaction">
<idx><h>Definitions</h><h>of data interactions</h></idx>
<statement>
<p>
Data types determine possible <term>interactions</term> between data.
</p>
<p>
In other words, there are many possible interactions between data (mathematical calculations, string concatenation, <etc></etc>), and what they are <em>depends on</em> the data type(s) involved.
</p>
</statement>
</definition>
<p>
For example:
</p>
<sage language="python">
<input>
a = 5
b = -2
print(a + b)
c = 3.5
d = 5.6
print(c + d)
print(a + c)
</input>
</sage>
<problem>
<pre>
3
9.1
8.5
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>data interactions</h></idx>
<p></p>
<p>
We know both <c>a</c> and <c>b</c> are of type <c>int</c>. So, adding them together makes sense and is allowed.
</p>
<p>
Similarly, <c>c</c> and <c>d</c> are both <c>float</c>, so addition is valid.
</p>
<p>
The last line is a bit more interesting since we can still perform an addition between <c>int</c> and <c>float</c>.
</p>
</investigation>
<p>
We can also <q>add</q> two or more strings together, resulting in a new string that is the concatenation of the component strings. For example:
</p>
<sage language="python">
<input>
s1 = "Hello"
s2 = "Jack"
s3 = " Jane"
print(s1 + s2)
print(s1 + s3)
</input>
</sage>
<problem>
<pre>
HelloJack
Hello Jane
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>string concatenation</h></idx>
<p></p>
<p>
<c>s1 + s2</c> is valid and concatenates the strings, same with <c>s1 + s3</c>.
</p>
<p>
Notice the difference between <c>s2</c> and <c>s3</c> and how it's reflected in the concatenated strings.
</p>
</investigation>
<p>
However, we <em>cannot</em> add <c>int</c> and <c>str</c> together, which will result in an <term>error</term>:
</p>
<aside>
<title>Topic(s) you might be interested in</title>
<p>
<ul>
<li>
<p>
<idx><h>Links</h><h>Python add int to string</h></idx>
<url href="https://www.google.com/search?q=Python+how+to+add+int+to+string" visual="google.com/search?q=Python+how+to+add+int+to+string">"Python how to add int to string"</url>
</p>
</li>
</ul>
</p>
</aside>
<sage language="python">
<input>
a = 5
s = "string"
print(a + s)
</input>
</sage>
<problem>
<pre>
TypeError
Cell In [1], line 3
...
TypeError: unsupported operand type(s) for +: 'int' and 'str'
</pre>
</problem>
<investigation>
<idx><h>Code examples</h><h>invalid data interactions</h></idx>
<p></p>
<p>
The operator <c>+</c> is an <em>invalid</em> interaction between <c>int</c> và <c>str</c>.
</p>
</investigation>
<p>
<cd>
</cd>
</p>
<exploration>
<title>Basic Programming <mdash /> Part 6: Data Types</title>
<idx><h>Videos</h><h>part 06</h></idx>
<p>
Coming soon.
</p>
<video youtubeplaylist="PLBLdRr-v59vwnKvmvLtcgmAnsb2K1Ta_M" />
</exploration>
</section>