1
00:00:00,720 --> 00:00:07,960
In this lecture, I want to briefly describe the building types in to see Python and the Python and

2
00:00:07,980 --> 00:00:11,650
micro python and to explain why these are important.

3
00:00:12,150 --> 00:00:18,600
So first of all, you should know that Python contains a number of built-In data types.

4
00:00:19,020 --> 00:00:26,450
These are numbers, sequences, mappings, classis instances and exceptions for our purposes.

5
00:00:26,730 --> 00:00:31,730
The most important of those built in types are the metrics and sequences.

6
00:00:32,100 --> 00:00:38,370
So you can see information here about the types of numbers the python supports this integers, float's

7
00:00:38,370 --> 00:00:40,080
and complex numbers.

8
00:00:40,500 --> 00:00:45,340
And then these are the types of sequences, lists, tuples and ranges.

9
00:00:45,410 --> 00:00:53,040
And I've got another lecture where I talk more about the sequence types now for again, our purposes.

10
00:00:53,040 --> 00:00:58,920
And I'm not getting into the details here, but for our purposes, the important thing to remember about

11
00:00:58,930 --> 00:01:06,640
types is that you can only do certain operations only between types of the same kind.

12
00:01:06,660 --> 00:01:10,350
So, for example, you can only numerically add two numbers.

13
00:01:10,350 --> 00:01:16,770
You can't add one number of type E.A., for example, with a string.

14
00:01:16,980 --> 00:01:24,840
And speaking of strange, you can see that down here, a string or SDR is a type of text sequence or

15
00:01:25,080 --> 00:01:27,630
text sequence type of a closer look at this.

16
00:01:28,010 --> 00:01:36,480
You said that the string is just a sequence of Unicode code points, as they call it, but we can also

17
00:01:36,480 --> 00:01:41,940
call those characters just not ASCII characters, but Unicode code points.

18
00:01:42,750 --> 00:01:50,610
So on the other side here, just to compare C Python on the list with micro Python on the right, you

19
00:01:50,610 --> 00:01:58,680
see that in Python we have most of the same building types that you find in Python, but not all of

20
00:01:58,680 --> 00:01:58,950
them.

21
00:02:00,150 --> 00:02:03,480
Scroll to the top of the left side.

22
00:02:03,810 --> 00:02:12,210
So you can see that on the micro python side, we still have exceptions, but arrays, bytes, dictionaries,

23
00:02:12,210 --> 00:02:16,810
floating point numbers, integers, lists and strings and tuples.

24
00:02:17,430 --> 00:02:19,660
So these are the built in types there as well.

25
00:02:20,310 --> 00:02:27,270
Let's have a look at a couple of simple examples to see what happens if you try to create operations,

26
00:02:27,480 --> 00:02:31,380
if you try to operate between data types that are not compatible.

27
00:02:32,040 --> 00:02:33,870
So let's start with simple numbers.

28
00:02:33,900 --> 00:02:38,100
So let's say that we've got a couple of variables.

29
00:02:38,100 --> 00:02:48,000
And as you can see, they hold integers or least one feature of Python that is particularly friendly

30
00:02:48,000 --> 00:02:55,830
for new programmers is that you don't have to tell Python what kind of data you are storing in a variable.

31
00:02:56,190 --> 00:02:58,090
You don't need to be explicit about that.

32
00:02:58,320 --> 00:03:04,050
So in C, for example, to create an integer variable and store an integer to it, you would need to

33
00:03:04,050 --> 00:03:05,190
do something like that.

34
00:03:05,190 --> 00:03:08,010
A E.A. A equals one.

35
00:03:08,020 --> 00:03:15,660
So you need to start by declaring the type of the data that you're about to store in a new variable

36
00:03:16,110 --> 00:03:23,010
by Python doesn't require that it can make insurence and figure out on its own that this number here

37
00:03:23,280 --> 00:03:28,050
is a integer and therefore variable A contains an integer.

38
00:03:28,290 --> 00:03:33,510
Or you could say that variable C will contain a floating point number like that.

39
00:03:34,350 --> 00:03:34,650
All right.

40
00:03:34,650 --> 00:03:36,870
So now we've got to integers.

41
00:03:36,870 --> 00:03:40,740
We can do something like this and add them together and the answer is going to be three.

42
00:03:41,520 --> 00:03:44,340
Now imagine that we've got another variable.

43
00:03:44,340 --> 00:03:45,720
Let's call that C.

44
00:03:45,750 --> 00:03:48,990
And now instead of numerical one, it's called the string one.

45
00:03:49,990 --> 00:03:50,620
Like that.

46
00:03:51,690 --> 00:03:52,390
No problem.

47
00:03:53,000 --> 00:04:00,270
Python is going to accept this new assignment and a new variable, but then if we try to add bait,

48
00:04:02,070 --> 00:04:07,830
let's make that a to see, then we're getting an error message.

49
00:04:07,830 --> 00:04:12,760
And Thony is also going to bring up the assistant to try and help us to fix it.

50
00:04:13,350 --> 00:04:20,640
So obviously here, the error message, which actually comes from the Python interpreter, indicates

51
00:04:20,640 --> 00:04:28,860
that we try to use an operator sine plus against two data types that are not compatible.

52
00:04:29,010 --> 00:04:35,050
And T and String says it makes no sense trying to add an integer to a string.

53
00:04:35,460 --> 00:04:40,670
So in such a case, what we need to do is to use casting.

54
00:04:41,010 --> 00:04:50,010
So casting tells Python that we want to convert a data type of one kind into a terror type of another

55
00:04:50,010 --> 00:04:50,520
kind.

56
00:04:51,150 --> 00:04:56,970
Now, this type of casting may not always work, but in many cases it does.

57
00:04:57,000 --> 00:05:05,550
So in this case, if you want to really add integer one to string one, what we can do is to cast string

58
00:05:05,550 --> 00:05:07,200
one into an integer.

59
00:05:07,200 --> 00:05:13,140
And that is a compatible casting because as you can see, the contents of the string is really number

60
00:05:13,140 --> 00:05:13,490
one.

61
00:05:13,890 --> 00:05:16,230
So we can say a plus.

62
00:05:16,440 --> 00:05:18,210
I can t see.

63
00:05:18,900 --> 00:05:20,970
And there is all this, too.

64
00:05:21,210 --> 00:05:28,380
So we used the casting instruction and t to convert the string into an integer.

65
00:05:28,650 --> 00:05:30,030
This may not always work.

66
00:05:30,240 --> 00:05:40,920
So for example, if I've got the which is a combination of say a number and a letter and then I try

67
00:05:40,920 --> 00:05:43,260
to say plus I t.

68
00:05:46,200 --> 00:05:51,280
This is not going to work because in this case the cast instruction failed.

69
00:05:51,300 --> 00:05:56,700
There's no way for Python to know how to convert this string into a number.

70
00:05:57,540 --> 00:05:58,010
All right.

71
00:05:58,290 --> 00:06:01,410
Now, the opposite, of course, is also true.

72
00:06:01,440 --> 00:06:06,210
So let's say that we have to I'm going to go for new letters.

73
00:06:06,480 --> 00:06:22,680
Let's go for P equals hello with a space in the end and let's make the world and now let's add a D and

74
00:06:22,680 --> 00:06:29,400
you can see that Python knows that this is a string and that is a string as well.

75
00:06:29,430 --> 00:06:38,340
So now the plus operator, instead of numerically adding these two variables, it will concatenate them.

76
00:06:38,340 --> 00:06:43,710
You add them together and create one larger string.

77
00:06:44,040 --> 00:06:46,300
The combination of the two component strings.

78
00:06:47,130 --> 00:06:54,390
Now, if I tried to create a new variable, let's call it one.

79
00:06:55,590 --> 00:06:57,720
Obviously, this is a numerical integer.

80
00:06:57,720 --> 00:07:05,220
And then I'm trying to add E to F, then I'm getting a similar error message.

81
00:07:05,380 --> 00:07:13,290
Again, the system is telling me that I can't concatenate a string to an integer, which is basically

82
00:07:13,290 --> 00:07:16,510
the message that the interpreter is giving us directly right here.

83
00:07:17,130 --> 00:07:19,550
So how to do this again?

84
00:07:19,980 --> 00:07:30,300
We can use casting and we can say that I want it to be added to the string version of F or to the to

85
00:07:30,300 --> 00:07:34,200
the string casted version of numerical one.

86
00:07:34,510 --> 00:07:37,080
And that will give us a one.

87
00:07:39,140 --> 00:07:47,750
So, of course, you can do similar types of operations with other data types, but at this point I

88
00:07:47,750 --> 00:07:53,570
just want you to understand the differences between the various stereotypes and that often, but not

89
00:07:53,570 --> 00:07:54,030
always.

90
00:07:54,050 --> 00:08:00,200
It is possible to convert one data type to the other for more complex stereotypes, like, for example,

91
00:08:00,200 --> 00:08:01,280
for sequences.

92
00:08:01,970 --> 00:08:05,240
These are data types such as lists and dictionaries.

93
00:08:05,540 --> 00:08:08,840
Things are a little bit more complicated because you've got multiple elements.

94
00:08:09,020 --> 00:08:16,340
And in some cases, such as in a tuple, for example, that I've got a dedicated lecture about, things

95
00:08:16,340 --> 00:08:23,420
get even more interesting since a tuple is capable of containing multiple dark types so you can have

96
00:08:23,420 --> 00:08:28,850
a table that in it you can store numbers and strings, for example, and even arrays and things of that

97
00:08:28,850 --> 00:08:29,220
sort.

98
00:08:29,570 --> 00:08:31,580
The things that get a little bit more complicated.

99
00:08:31,580 --> 00:08:35,780
But again, with a little bit of planning ahead, it's impossible to program.
