0:00
Hey everyone, welcome back to another
0:02
episode in our Python basics series. So
0:04
today we are going to talk about
0:06
something super important which is type
0:08
conversion. So now if you have ever
0:11
tried adding a string and a number and
0:13
got an error or ever wondered how to
0:17
turn your user input into numbers, you
0:21
can actually do math with this episode
0:26
So grab your keyboard and let's get
0:31
So first what is type conversion? First
0:34
things first, what does type conversion
0:38
even mean? Type conversion is just a
0:41
fancy way of saying changing a value
0:43
from one data type to another. like for
0:47
example turning a string like 42 into a
0:52
number 42 or changing a number into a
0:57
string so you can join it with the text.
1:00
So why does this matter? Because in
1:03
Python different data types behave
1:05
differently. You can't add a number to a
1:08
string without converting it or compare
1:11
values correctly if there are different
1:13
data types. So type conversion helps
1:16
your data to handle the right way and
1:21
So let's start with converting to an
1:25
Say you get user input and remember
1:29
input always comes in as a string
1:53
and we need it to be integer number. So
1:57
we are converting the text to an
2:02
So this will be using the int method and
2:06
inside we are going to give the text
2:22
Okay. So what we are expecting is this
2:25
text is actually a 42 and we are
2:28
converting it to integer 42 and 42 + 10
2:31
will be 52. Let's check.
2:37
Yep. So what we did here we took the
2:40
string 42 and used the integer to turn
2:43
it into a number. And now we can do math
2:49
So the pro tip is always use integer
2:53
when you expect a whole number from user
2:56
input like age or quantity.
3:01
So now let's say you have a number but
3:03
you want to combine it with the text
3:21
equal to sum 1 2 3 and text number. So
3:26
you are converting the
3:29
text sorry so you are converting the
3:31
integer to the string which is text. So
3:35
for that you will use string which is
3:39
and we are sending integer number here.
3:43
Now you just have to print
3:59
So what happening is if you see your
4:02
score is 123 it worked because now it's
4:08
So what we did we send the number we
4:10
need it in string. So that is what we
4:13
did. So we actually changed it to string
4:15
by using the method str function.
4:20
So and then we have concatenated the
4:23
string. That means we just added two
4:25
strings so so that it can print the
4:31
So the pro tip here is anytime you are
4:34
building a sentence with the numbers you
4:37
always have to convert them with the
4:39
string first and then you have to
4:44
You can also do something like this
4:48
by just giving comma here and you can
4:50
still do that but in some cases we have
4:53
to add plus in between you know the
4:56
strings you have to concatenate. So at
4:59
that time this is really important. All
5:02
right. What if you need to work with
5:05
decimals? You can use float.
5:19
you can see the text number here 3.14
5:24
and we need the decimal number. So
5:30
equal to so load of the text number and
5:35
now we want decimal number
5:47
And now if you run this.
5:51
So here we are giving the text
5:55
3.14 which is in string and we are
5:58
converting it to the float because we
6:00
need it in float. So then it will be
6:02
text number float of text number and
6:05
then it we are actually doing it
6:07
multiplying it with two. So that is what
6:14
So whenever you expect decimal numbers
6:16
like prices or weights or measurements,
6:19
always keep that in mind that we have to
6:21
convert the text into the float.
6:24
Finally, let's talk about converting to
6:27
boolean or bool. Booleans are either
6:30
true or false. We have already checked
6:32
like we have already worked with the
6:34
data types before. I want you to go back
6:37
if you still have any doubts regarding
6:39
the data types then please go back and
6:41
check the video then come back here and
6:43
start working on it. If you have any
6:45
doubts you can just drop down them in
6:47
the comments. We are always here to help
6:51
So what if you want to check if a value
6:54
is exist or is empty.
6:59
So, Python has a simple rule like zero,
7:03
empty strings, empty list and none are
7:06
considered as false. So, whatever the
7:09
values that you give zero or it could be
7:15
could be zero or any empty string like
7:21
any list. We are going to talk about
7:23
that in the future. You don't have to
7:24
worry about that first for now. and
7:28
none. So then it will take these values
7:40
So other than that whatever you give
7:42
will comes under true.
7:53
boole of zero. So if you run this one,
7:59
let me just keep everything in comments.
8:05
So it is taking it as false and like if
8:15
and let's just also give
8:21
empty string as well.
8:26
And then you can give
8:41
if you see boolean of zero is that true
8:44
or false. So this function is always
8:46
checks whether the value is true or
8:48
false. So whenever you give zero or
8:51
empty string or none then it will take
8:53
it as false. So 0 is false and 42 is
8:58
some value is there. So it will consider
8:59
it as true other than zero none you know
9:02
empty string. So here it is an empty
9:04
string so it is considering it as false
9:07
and here you gave some value so it will
9:09
take it as true. So you can use bool to
9:12
quickly check if a value is empty or non
9:15
zero which is super useful for
9:18
conditions which we are going to talk
9:20
about in the upcoming sessions. You
9:22
don't have to worry. So the pro tip here
9:24
is this is really a lifesaver when
9:27
checking user input or optional
9:33
And that's your crash course on type
9:35
conversion in Python. And I think we
9:38
have covered int for integers,
9:41
string for strings, str for strings and
9:45
float for decimal numbers,
9:49
float for decimal numbers, and bool for,
9:52
you know, true or false. Master these
9:55
four and you will be able to handle user
9:58
input, process data correctly, and avoid
10:01
a ton of frustrating bucks. If this
10:03
video helped you out, go ahead and like,
10:06
subscribe, and tap the bell icon so you
10:09
never miss an episode in this series.
10:11
Thanks for coding with me, and I'll see
10:13
you in the next one where we'll dive
10:15
into even more cooler stuff.