Hi, everybody, and welcome.
This is a bonus lecture to the last lectures that we've dealt with variables and that this lecture we
will talk about casting.
So we basically learned what casting is without knowing it in the last lecture.
We have, for example, this command right here.
And we converted this number, which is a float because it has two numbers after that, similar as we
talked in the last.
So we converted it into a string.
So this action is casting.
When you put a variable inside brackets and outside, you type in the you type in the type of the variable.
So basically you cast it because it is one type.
This is the first type.
And then you cast it into a different type, which is a string.
So, all right.
So Python is an object oriented language, as you all know.
And as such, it uses classes to define it, to define data types, including its primitive types,
which are string, integer and variable.
So in this lecture, we'll talk about casting, by the way, the new class Biton file.
Call it casting like variable casting.
And
and first of all, let's just enter into action and say here, casting from integer to string, which
we already know how to do, and let's declare a integer variable.
And that's said that is five and then let's do the following thing, let's do our string variable,
you know what, integer?
In Dejour, variable casted to string and what we want to do here, as Samia's we've seen a few seconds
ago right here, let's put a SDR in brackets and put the variable string variable inside.
So SDR recants and put the integer variable.
Right here.
So basically, if we have the integer variable here and we have the SDR casting action above it, so
this variable should be good, you guess what type is this one?
Let's try it.
Let's just print it.
But if we just copy this variable and place it right here, we are obviously going to see the simple
value of it.
So we're going to see five, right?
So
it's just five.
We can see the type of it.
But if you put the key word type outside of it and cover it with brackets like this, so everything
that I'm marked right here is inside of the type, right.
Sorry, inside the print.
So all of this, we want to print it and now let's see what we want to print.
We have the variable right here, which is an integer type variable with a value of five.
And we have set the keywords a type outside of it.
So basically what we want to print is as we want to print and the type of the variable.
All right.
That special word in Python and let's run it
right.
Amazing.
So we can see the type of the variable here.
So the integer variable cast it to string the casting was successful.
And we can see it right here because we got the string type.
All right.
So let's do another example.
We can do another print here with.
Like, just for us to know, you know what, let's live it like this, so when you won't get confused.
Right.
So that's another point here and with quotation marks and put some stars just to differentiate what
we get in the blogosphere.
All right.
So let's do another print.
All right.
And right here, casting from an integer.
To float, so again, let's, uh, let's we have actually we have this integer variable, so let's just
use it and.
All right, so we're focusing on this one.
This is our base variable that we going to cast now.
So let's do the following thing, same as we did here.
Let's create an integer variable.
All right, let's button on the score here, integer variable casted.
To float and now let's let's dive the key word, float the brackets and let's copied and pasted here.
And now what we want to do is to print the type.
That's the type of this variable, right?
This is the new one we've done the casting right here and here is the new variable.
So let's copy it and place it inside that the type brackets.
And right, click on the file and run, you guys are so casting from integer to flow, so this variable
is a float as we want it and let's do another one.
Just copy the stars.
Place sits right here and now we want to do casting from.
From float to integer.
All right, so we have here.
All right, this one, this guy right here.
We know it's a float, right, we have it's a float, so let's do casting on top of casting.
All right, so let's bring this guy right here, which is a float back to being integer.
So I'll just copy this one.
And create a variable called float.
Very able cast to.
Integer and displays it and then put it outside like that.
And so this one is a casting for the float variable.
Copy this one copied and then again print.
Type records and then based it right here.
So we pasted this one inside the type inside the print run at.
All right, great, guys.
So now we know how to change one variable into another.
So as you can see, Python is a very dynamic language.
So you can get us the flexibility that we want and the code.
And let me show you just one last thing.
Let's try to print this integer variable and then let's try to print this integer variable cast to float.
Let's print their values.
All right.
So I'm just going to copy it.
And then print and then based it.
And I still do the same as here, here.
Brent, let's copy this white sorry, copy this one and based it right here.
And let's run it, so all right, so as you can see, guys here, we have a string variable.
Well.
Well, well, sorry about that.
Oh, yeah, it's OK.
So here we printed the integer variable.
All right, this is the integer.
Yep, so the values five, and then we printed here the integer variable cast to float.
So this one is a float.
So you see that after we cast it it from integer to float, it has a decimal point and we can just verify
that indeed we have a float here, as we can see a decimal point.
That's about it, guys.
So now you know how to do casting.
And this is a great ability and great power for the role for test cases that we going to write, and
that's about it.
So it's not very difficult, but it's a really useful tool.
All right.
See you in the next lecture.