You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In programming, **data types** define the kind of data a variable can hold, such as numbers, text, or more complex structures, and they determine what operations can be performed on that data.
16
16
@@ -21,44 +21,87 @@ Using data types in Python is straightforward: you assign a value to a variable,
21
21
For example, `x = 10` creates an integer, whereas `x = 10.3` creates a float.
22
22
A float in a decimal placed number and is stored in python in a 64 bit format.
23
23
24
-
For an overview of data types use the cheat sheet.
24
+
For an overview of data types use the **cheat sheet**.
25
25
26
-
### Primitive Data Types
26
+
::: {.callout-note}
27
27
28
-
Primitive data types contain single values e.g. numbers and words
28
+
To check the data type of a python object use the ```type()``` function.
29
29
30
-
The main thing to remember is that sequences of letters like words are called strings and require quotation marks around them:
30
+
```{python}
31
+
32
+
x = 10
33
+
print(type(x))
34
+
35
+
```
36
+
:::
37
+
38
+
39
+
## Primitive Data Types
40
+
41
+
Primitive data types contain single values e.g. numbers and words.
42
+
43
+
### Numerical data types
44
+
45
+
Python has three numerical data types:
46
+
- integers: whole numbers `int`
47
+
- floats: decimal placed numbers which are also known as floating point numbers `float`
48
+
- complex numbers: python has direct support for complex numbers (*a + bi*). These are not used often by most python users and will not be discussed further in this course.
49
+
50
+
### Words and text
51
+
52
+
Sequences of letters like words are called strings and require quotation marks around them:
31
53
32
54
```name = "Alice"``` creates a string.
33
55
56
+
Strings can be single words or paragraphs.
57
+
58
+
### Boolean: true or false
59
+
34
60
The boolean data types use the key words ```True``` and ```False```
35
61
62
+
### The placeholder
63
+
36
64
```None``` can also be used as a placeholder for a variable has no meaningful value yet.
37
65
38
-
Understanding data types is essential for writing correct programs, performing calculations, and managing data efficiently.
39
-
Data types behave slightly differently in different languages for example the way numbers a rounded.
66
+
## The importance of data types
40
67
41
-
## Python operators
68
+
Python makes working with data types easier than many other programming languages. When you create a variable, Python automatically decides what type of data it is based on the value you give it. In most cases, the correct data type is a straightforward choice. For example, when indexing a table or referring to whole number counts integers are used, whereas measurements often require decimal places and so floats are used.
42
69
43
-
**operators** are symbols or keywords used to perform operations on data and variables, like calculations or comparisons.
44
-
This of mathematical singles like ```+``` 0r ```<```.
70
+
That said, Python’s data types each have their own rules and behaviors. As you’ll see in the following exercises, understanding these differences is important for writing correct programs, doing calculations, and working with data effectively. It’s also helpful to know that data types can behave differently in other languages—for example, in how numbers are rounded.
45
71
46
-
Most of the operators in python make sense. Use the cheat sheet table to look them up if you need.
72
+
## Immutable versus mutable data types
47
73
48
-
::: {.callout-exercise}
49
-
#### Using operators
74
+
In Python, data types can be either **mutable** or **immutable**, which refers to whether their values can be changed after they are created.
75
+
76
+
Most python primitive data types, such as integers, floats, booleans, and strings, are **immutable**, meaning their values cannot be changed after they are created. If you appear to modify one of these values, Python actually creates a new object instead with the same variable name. This makes primitive types simple and predictable to work with, especially for beginners.
77
+
78
+
In contrast, composite data types (like lists, tuples, sets, and dictionaries) can be either **mutable** or **immutable**. Two types that are **mutable**, and so can be changed after they are created are lists and dictionaries. We will look at composite data types in more detail in the next section.
79
+
80
+
::: {.callout-note}
50
81
51
-
Use the console to test different operators from the tables.
52
-
Try adjusting the operators in the expenses_calculator.py script and seeing how the behaviour of the script changes.
82
+
To demonstrate that the primitive data types are immutable you ca change their value and use the id() function to check where they are located in memory.
53
83
84
+
```{python}
85
+
86
+
x = 10
87
+
print("Initial value:", x)
88
+
print("Initial memory location:", id(x))
89
+
90
+
# "Modify" the integer
91
+
x = 15
92
+
93
+
print("New value:", x)
94
+
print("New memory location:", id(x))
95
+
96
+
```
54
97
:::
55
98
56
99
57
100
## Composite Data Types - The exciting stuff!
58
101
59
-
Complex data types is where things really become interesting and the basis for more complex programming
102
+
Composite data types are where things start to become more interesting and form the foundation for more complex programming.
60
103
61
-
In Python, composite data types allow you to store multiple values in a single variable. They can hold a collection of items, which can be of different data types. The main composite data types in Python are:
104
+
In Python, composite data types allow you to store multiple values within a single variable. These values are grouped together as a collection, and they can even be of different data types. The main composite data types in Python are:
62
105
63
106
**Lists**
64
107
@@ -88,7 +131,7 @@ In Python, composite data types allow you to store multiple values in a single v
88
131
89
132
Let's initialize one of each composite data type with a list of 10 animals, and then demonstrate some of the main differences.
All built-in data types in Python have associated built-in functions that allow you to convert values from one data type to another or explicitly create an object of that type. Examples of converting between data types are presented below.
267
+
268
+
It is not possible to convert between all data types; some conversions require additional information and input.
269
+
270
+
Resources about data types can be found in the python documentation:
271
+
https://docs.python.org/3/library/functions.html
272
+
https://docs.python.org/3/library/stdtypes.html
273
+
274
+
Examples of converting between data types:
275
+
276
+
277
+
278
+
**1. Converting int to float**
279
+
```
280
+
x = 5 # integer
281
+
y = float(x) # converting to float
282
+
print(y)
283
+
```
284
+
Output:
285
+
```
286
+
5.0
287
+
```
288
+
289
+
**2. Converting float to int**
290
+
```
291
+
x = 3.23 # float
292
+
y = int(x) # converting to int
293
+
print(b)
294
+
```
295
+
Output:
296
+
```
297
+
3
298
+
```
299
+
300
+
**3. Converting int to str**
301
+
```
302
+
x = 10 # integer
303
+
y = str(x) # converting to string
304
+
print(y)
305
+
```
306
+
Output:
307
+
```
308
+
'10'
309
+
```
310
+
311
+
**4. Converting str to int**
312
+
```
313
+
x = "123" # string
314
+
y = int(x) # converting to int
315
+
print(y)
316
+
```
317
+
Output:
318
+
```
319
+
123
320
+
```
321
+
322
+
**5. Converting str to float**
323
+
```
324
+
x = "45.67" # string
325
+
y = float(x) # converting to float
326
+
print(x)
327
+
```
328
+
Output:
329
+
```
330
+
45.67
331
+
```
332
+
333
+
**6. Converting list to tuple**
334
+
```
335
+
my_list = [1, 2, 3] # list
336
+
my_tuple = tuple(my_list) # converting to tuple
337
+
print(my_tuple)
338
+
```
339
+
Output:
340
+
```
341
+
(1, 2, 3)
342
+
```
343
+
344
+
**7. Converting tuple to list**
345
+
```
346
+
my_tuple2 = (4, 5, 6) # tuple
347
+
my_list2 = list(my_tuple2) # converting to list
348
+
print(my_list2)
349
+
```
350
+
Output:
351
+
```
352
+
[4, 5, 6]
353
+
```
354
+
355
+
**8. Converting int to bool**
356
+
```
357
+
num_bool = 0 # integer
358
+
bool_value = bool(num_bool) # converting to boolean
**operators** are symbols or keywords used to perform operations on data and variables, like calculations or comparisons.
18
+
This of mathematical singles like ```+``` 0r ```<```.
19
+
20
+
Most of the operators in python make sense. Use the cheat sheet table to look them up if you need.
21
+
22
+
::: {.callout-exercise}
23
+
#### Using operators
24
+
25
+
Use the console to test different operators from the tables.
26
+
Try adjusting the operators in the expenses_calculator.py script and seeing how the behaviour of the script changes.
27
+
28
+
:::
29
+
30
+
::: {.callout-warning}
31
+
Strings in Python are immutable, meaning they cannot be changed after they are created. When a string is modified—such as through concatenation or other operations—Python creates a new string object instead of altering the original one. Because of this, repeatedly building strings using operations like + in loops can be inefficient in both time and memory, since many intermediate string objects may be created. Python automatically manages memory using a process called garbage collection, which is how it reclaims memory from objects that are no longer needed. More specifically, garbage collection removes objects that are no longer reachable in the program—meaning there are no variable names or references pointing to them anymore, either because they were reassigned or deleted. In most cases, you do not need to manage this process manually. Instead, for efficiently building large strings, it is better to use tools designed for this purpose, such as ''.join() or io.StringIO, which reduce the number of temporary string objects created.
0 commit comments