In this lecture, I want to briefly describe the building types in to see Python and the Python and
micro python and to explain why these are important.
So first of all, you should know that Python contains a number of built-In data types.
These are numbers, sequences, mappings, classis instances and exceptions for our purposes.
The most important of those built in types are the metrics and sequences.
So you can see information here about the types of numbers the python supports this integers, float's
and complex numbers.
And then these are the types of sequences, lists, tuples and ranges.
And I've got another lecture where I talk more about the sequence types now for again, our purposes.
And I'm not getting into the details here, but for our purposes, the important thing to remember about
types is that you can only do certain operations only between types of the same kind.
So, for example, you can only numerically add two numbers.
You can't add one number of type E.A., for example, with a string.
And speaking of strange, you can see that down here, a string or SDR is a type of text sequence or
text sequence type of a closer look at this.
You said that the string is just a sequence of Unicode code points, as they call it, but we can also
call those characters just not ASCII characters, but Unicode code points.
So on the other side here, just to compare C Python on the list with micro Python on the right, you
see that in Python we have most of the same building types that you find in Python, but not all of
them.
Scroll to the top of the left side.
So you can see that on the micro python side, we still have exceptions, but arrays, bytes, dictionaries,
floating point numbers, integers, lists and strings and tuples.
So these are the built in types there as well.
Let's have a look at a couple of simple examples to see what happens if you try to create operations,
if you try to operate between data types that are not compatible.
So let's start with simple numbers.
So let's say that we've got a couple of variables.
And as you can see, they hold integers or least one feature of Python that is particularly friendly
for new programmers is that you don't have to tell Python what kind of data you are storing in a variable.
You don't need to be explicit about that.
So in C, for example, to create an integer variable and store an integer to it, you would need to
do something like that.
A E.A. A equals one.
So you need to start by declaring the type of the data that you're about to store in a new variable
by Python doesn't require that it can make insurence and figure out on its own that this number here
is a integer and therefore variable A contains an integer.
Or you could say that variable C will contain a floating point number like that.
All right.
So now we've got to integers.
We can do something like this and add them together and the answer is going to be three.
Now imagine that we've got another variable.
Let's call that C.
And now instead of numerical one, it's called the string one.
Like that.
No problem.
Python is going to accept this new assignment and a new variable, but then if we try to add bait,
let's make that a to see, then we're getting an error message.
And Thony is also going to bring up the assistant to try and help us to fix it.
So obviously here, the error message, which actually comes from the Python interpreter, indicates
that we try to use an operator sine plus against two data types that are not compatible.
And T and String says it makes no sense trying to add an integer to a string.
So in such a case, what we need to do is to use casting.
So casting tells Python that we want to convert a data type of one kind into a terror type of another
kind.
Now, this type of casting may not always work, but in many cases it does.
So in this case, if you want to really add integer one to string one, what we can do is to cast string
one into an integer.
And that is a compatible casting because as you can see, the contents of the string is really number
one.
So we can say a plus.
I can t see.
And there is all this, too.
So we used the casting instruction and t to convert the string into an integer.
This may not always work.
So for example, if I've got the which is a combination of say a number and a letter and then I try
to say plus I t.
This is not going to work because in this case the cast instruction failed.
There's no way for Python to know how to convert this string into a number.
All right.
Now, the opposite, of course, is also true.
So let's say that we have to I'm going to go for new letters.
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
you can see that Python knows that this is a string and that is a string as well.
So now the plus operator, instead of numerically adding these two variables, it will concatenate them.
You add them together and create one larger string.
The combination of the two component strings.
Now, if I tried to create a new variable, let's call it one.
Obviously, this is a numerical integer.
And then I'm trying to add E to F, then I'm getting a similar error message.
Again, the system is telling me that I can't concatenate a string to an integer, which is basically
the message that the interpreter is giving us directly right here.
So how to do this again?
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
the string casted version of numerical one.
And that will give us a one.
So, of course, you can do similar types of operations with other data types, but at this point I
just want you to understand the differences between the various stereotypes and that often, but not
always.
It is possible to convert one data type to the other for more complex stereotypes, like, for example,
for sequences.
These are data types such as lists and dictionaries.
Things are a little bit more complicated because you've got multiple elements.
And in some cases, such as in a tuple, for example, that I've got a dedicated lecture about, things
get even more interesting since a tuple is capable of containing multiple dark types so you can have
a table that in it you can store numbers and strings, for example, and even arrays and things of that
sort.
The things that get a little bit more complicated.
But again, with a little bit of planning ahead, it's impossible to program.