Hello and welcome to this video.
In this video I'm going to introduce you to casting in Python.
What is casting casting is the process of converting the value of a variable from one data type to another.
Usually you can do this by using various types of built in functions in Python.
The three common functions that can be used for casting are int which converts a value to an integer
an integer.
Basically a whole number.
You can also use a function called float which converts a value to a floating point number floating
point is a decimal number.
You also have a function called T R which is short for string a converts a value to a string or text.
I'm going to show you some examples of casting and I'm going to be using my Python interactive shell
if you on the windows just open up your command prompt and just typing python and press enter and that
gives you access to the interactive shell.
If you're on a Mac you can type in Python 3 on your terminal and that will give you access to the interactive
show.
So I want to start off by creating some variables.
I'm gonna create a variable and we'll call it X and I'm going to give it a value of 70 which is going
to be a string.
So I'm going to put quotes around it.
Anything that has a quote rounded is treated as a string.
So I press enter to that and I'm going to add another variable called this one.
Why now give it's also a string value so I'll do single quote.
Make this a negative number.
I'm making negative 20 so this at the closing quotes of these two variables are text or strings.
The values are treated as a string.
Now if I use a print statement failed to print and I pass in the value of x plus y and presenter you
can see is giving me 70 minus 20.
That's because it's treating the values as string that is treating them as text.
I can now do some casting here by converting the values into an integer which is a whole number.
So to convert the values into an integer I can do the print statement again and inside the print function.
I'm going to nest the int function and inside that in function I'm going to pass in the variable x and
then I'll add my addition and then I'll do the int function again.
And inside the parentheses for that in function a passing variable Y.
Now if I run this again I should get a proper result you can see now is giving me 50 which is a whole
number on integer.
So I was able to do some casting by converting the value original value which was a text value in to
an int which is an integer or a whole number.
All right.
So that's an example of doing casting and changing a value to an integer or a whole number.
Let me show you an example of converting to a float.
So I'm going to create two other variables will not create a variable C and I'm going to give it a value
of seven.
I'm going to create a variable code D.
I'm going to give it a value of eight.
So these values are integers.
If I want to convert the values into a float a float is a decimal number I can use the function code
float.
So inside my print function I'm going to nest a function called float which will convert the values
into a decimal or a floating point number.
So inside our float function I'm going to passing the first variable which is C All right.
And then I will do an addition and I add the function float again I'm passing the second variable which
is D.
Now if I press enter it should give me fifteen point five.
The point 0 here makes it a floating point number.
So anything with a decimal he's treated as a floating point number.
So that's an example of casting that converts to a floating point number.
I'm going to do one more when I create a couple of variables again a creative variable e I'm going to
give it a value of nine.
I'll press into a crate on another variable f I'm going to give it a value of say eight point five so
this the values are the first value for E.
That's an integer whole number and the value for F is a floating point number to convert the values
into a string.
I need to use a function called S T R so I'm going to type in print which is a print function and inside
the print function.
I'm going to add the SDR function.
So inside the parentheses for the SGI function are passing the first variable e.
Okay.
And then I'll do a comma.
I'll add a text that says and aloud to a comma after that okay.
And then I'll add another SDR function and inside the parentheses are passing variable f Okay and then
I'll do a comma.
Now add another text that will be added to the variable values.
So that is it.
I'm just gonna close that and I'll close the parentheses so I press enter and you can see here it says
nine and a point five are strings.
So I was able to convert the values of the variable E and F in to string by using the PCR function.
So that's basically what casting is where you change the value of a variable from one data type to another.
Thanks for watching and bye for now.