Type Casting in Python – GATE, Interview & Placement Practice
501 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video So
0:02
in this video I will show you a very
0:04
important concept in Python which refers
0:06
to type casting converting from one data
0:09
type to another data type It's very
0:12
important concept in Python which is
0:13
asked typically using during interviews
0:16
competitive exams for placements If you
0:18
go for interview this question is asked
0:21
what is what you mean by type casting
0:24
type casting refers to basically
0:27
converting from string to integer from
0:30
integer to string from floating number
0:32
to integer number So Python has a series
0:35
of built-in methods which implement type
0:38
conversion or type casting So as you can
0:41
see we are taking the number
0:43
10 And now to convert this number to a
0:46
floatingoint number we use the floating
0:49
method right here Let me just show you
0:52
what do I mean by taking a full example
0:55
So if you have the number
0:57
10 10.0 so this is actually a
1:00
floatingoint number If you want to print
1:02
out what is the type of this number you
1:05
simply write type followed by the
1:07
variable So the output of this will be
1:10
float because it's a floatingoint number
1:13
because it involves this decimal 10.0 If
1:16
I try to execute this program if I you
1:20
will see it will return this it's a
1:22
floating point
1:23
number So if you want to convert this
1:26
into a integer we simply write here we
1:28
type cast it into a integer by calling
1:31
the int
1:33
method So then we refer the variable
1:36
name That's all So if you execute now it
1:39
will round this number to 10.0 will be
1:43
rounded off to 10
1:46
The interesting thing is that if you
1:48
write 10.5 what will be the output just
1:51
see it will not be 10 it will be 11
1:53
because 10.5 will be rounded off to 11
1:57
So if you see still it is 10 I think
2:00
Yeah So 10.5 is still rounded to if I
2:04
say 10.9
2:06
here Let me see the
2:09
output again This
2:11
is put to 10 So yeah So
2:17
this the round the rounding off is not
2:20
enabled by
2:22
default You need to explicitly tell the
2:26
Python compiler to float this to round
2:30
this number to 11 So by default it
2:32
doesn't do that We can even ask chat GPT
2:36
for that
2:37
But now if you want to basically you
2:40
have a string variable If you want to
2:43
convert the string into a number So we
2:52
have this is actually a string Now so if
2:56
you want to convert this string into a
2:59
number we can print out the type of this
3:03
So you can simply run this So now it's a
3:05
string So if you want to convert this
3:08
into a number if we simply write here
3:10
int and then followed by the variable
3:12
name So if you just execute
3:16
now so you can see it can't be converted
3:19
into int
3:21
because it's
3:23
a string So string can't be converted
3:26
into number So this error will come
3:29
right
3:31
here So for this you need to have this
3:34
into a number
3:37
then only it will be converted you will
3:41
see So in this easy way you can do type
3:44
casting very
3:47
easily So this is meant by type casting
3:51
a type conversion from one data type to
3:53
another data type So Python has the
3:56
series of built-in methods which lets
3:59
you do this So we have the string We
4:04
also have the int here we have the again
4:07
it we have also the floating type So
4:11
these are the series of methods which
4:14
are there So thank you very much for
4:17
watching this
4:19
video and uh also check out my website
4:23
freemediatools.com
4:25
uh which contains thousands of