Let's learn about typecasting.
Now, typecasting is basically taking one data type and changing
it into another data type.
So for instance, we could say 20 == 20, and this is
going to return True.
But if we said 20 == 20.0, True or False? Also True
because it's a number.
But what if we said 20 == "20", with the string?
Python is looking at this and saying 20 is not "20".
The reason for that is because this is a number,
this is an integer.
So integers and floats can be compared very easily, because
really, if you just get rid of this, it is the exact same.
But this is comparing an integer with a string.
Those are fundamentally different data types.
So what do we do here? If we ask someone for their age,
so let's say 'age = input(
"What is your age
(enter a number)?', and I'm going to put the number 30 in here.
We're now going to see that if I type in 'type(age)',
this is going to be a string.
I mentioned this quite a while ago that anytime you accept
user input, it always comes in as a string.
Program never knows what it's going to be.
And even though I typed a number, and I suggested that it
should be a number, a program doesn't know that. Programs are
not that smart. And so it always comes back as a string.
Now what if I said 'age'.
Let's just verify. The age is in fact '30'.
You can see it's got apostrophes around it telling us that
it's a string. 'age == 30',
It's False. But actually 'age' is 30.
It's just a string.
So what we can do in this particular situation, is we can
change that to an integer.
At least we can try to do that anyways.
So if we just had 'int(age)', look at that.
It is now 30. It doesn't have apostrophes around it.
So let's go ahead and
overwrite this. 'age = int(age)'.
And then let's do 'type(age)'.
We're going to see this is now an integer.
And if we do, 'age == 30', because we have now typecasted
this from a string, and we've changed it into an integer, and
it's now comparing against an integer,
it's True.
And this is one of the reasons that we want to do typecasting.
But there are several other reasons you might want to do
some typecasting as well.
For instance, let's say we have some sort of tuple and we've
got numbers (1, 2, 3, 5)'
And let's say for whatever reason, that was actually supposed to be a 4,
that was a typo.
Well, if we look at this, it's still a tuple of '(1,2, 3, 5)'.
'tup', If we get the index of 0-1-2-3, we cannot overwrite this.
And let's try this.
Doesn't work.
'tuple' object does not support item assignments.
Can't do it.
But just to confirm that we are actually working with an
index that does work.
Hey, look at that.
It does in fact work.
So let's say we have this tuple, and we wanted to change it.
Well, we have a few different things we could do.
We could just write a new tuple if we wanted to.
We could just say, okay, 'tup' is now equal to 4. We
could do that. Or we could change this to a list, and then
back into a tuple.
So we could do the tuple is going to change into a list.
We could then edit the list, and convert it back to a tuple
if we need to.
So we're going to turn this into a list with the list data
type, and you can see it looks actually just like a function,
and put that tuple in there.
Then we're going to say 'print(lst)',
also 'print(type(lst))', just so that we're super
super certain what we're working with here.
Sure enough, this is now a list, it's a class 'list'.
Cool. So now we can say list 0-1-2-3 is equal to 4.
'lst'. It's now been changed.
And we can overwrite tuple with this list.
And so we can say 'tuple = tuple', and then put that list in there.
Now, let's take a look at this tuple.
It's now fixed.
So all we did was we changed that tuple to a list,
edited the list, and then changed that list back into a tuple.
So that's one use case.
A more general use case would actually be Booleans.
So if you wanted to say, is 'name' in here, 'name' is not in
here. So let's go ahead,
and assign 'name = input(
"What is your name?")'
It's going to ask me for my name.
My name is going to be 'Kalob'.
[no audio]
Yep, there it is, 'Kalob'.
And what I can do here, is I can say 'if name' has something
in it, but for some people, that can be a little bit confusing.
'If name', well, yeah, there's always a name.
If you're just looking at this, name is definitely assigned
to something, which is not necessarily the case here, but
it does look like it's always assigned to something.
So what we can say is 'has_name = bool()',
Put a 'name' in there and let's do 'type(has_name)'.
It's now a Boolean.
'has_name', is that True or False?
[no audio]
It's True. So now we can say 'if has_name', instead of 'name', 'print("The
name is, name")', just like that.
Now, one thing that I really like to do, is I like to turn
strings into int, whenever I can, because I like my data types
always being pristine.
So 30, for instance, from earlier should not be a string,
it should be an integer.
And one of my favorite things to do, is to take a list and
get rid of all the duplicates.
So let's create a list of 'groceries'.
So say we've got '['Milk', 'Bread', 'Eggs']',
and let's say I wasn't paying attention,
and I wrote 'Milk' the second time, and I wrote 'Eggs' the second
time, and 'Oranges', and 'Chocolate'.
I've got two duplicates in here.
I've got 'Eggs' in here twice, and I've got 'Milk' in here twice.
So let's go ahead, set this.
We can see that 'groceries' is going to be exactly as we set
it. But what we can do, is we can actually say this is a list,
but let's just go ahead, turn this into a set. Run that.
Run that. And look at that.
It got rid of all the duplicates automatically
for me. That is powerful. That is really nice.
I don't have to loop through it,
I don't have to create another list from a loop or anything like that,
I can simply say this is now a set, and I want to make sure
that everything in here is unique.
Now, remember, sets don't hold their order,
so there is a pro and a con here.
But if you want to change it back into a list, you can absolutely do that too.
Now, this is pretty crazy because we've got a list in here,
we're casting it to be a set, so it's going to be completely
unique. and then we're turning it back into a list so that
it holds whatever order it has.
So let's go ahead, run that,
and it did not keep its order, but from now on, it will.
So whatever this order ends up being, it will now be that
order. We can always do 'groceries.',
and where are you?
We can do a little bit of sorting here.
[no audio]
'groceries', and there we go, sorted in alphabetical order, and that's
more typecasting in here.
So again, list to a set to a list,
just to make sure that our initial list is totally unique.
We've set it back to a list so that we have the '.sort'
method available, and then we ran it so that our list is
in alphabetical order.
That is actually a very good use case for typecasting.
Now, what I would like you to do, is I want you to experiment
on your own just a little bit here.
So I want you to create some sort of dictionary, and it's
going to have to have keys and values in here.
'key': 'value'; and put a second 'key' and a second 'value' in there,
maybe call it 'person'.
Anyways, you need some sort of dictionary, and then I want
you to cast that dictionary as a list, cast it as a tuple.
You can actually even go as far as to say something like
'list(dictionary.items)', because if you remember, let's
actually execute this. 'dictionary.items()'.
This gives us a weird sort of data type called 'dict_items'.
But what happens if you were to turn this into a list?
I mean, it's already got a list on the inside, but look at that.
It is now just a list.
Anyways, try that with a list,
try it with a tuple,
see what you can do.
What happens when you cast a dictionary as a string?
That's an interesting one.
Try that. See what happens.
See if you get an error. And one last thing, errors are okay.
It's a good way to learn what you can and cannot do in a
programming language.
There's nothing you're going to break with casting anyways.
So give that a shot.
Cast this as a string, a list, and a tuple,
and when you've done that, let's head on over to the next lesson.