In Python, a variable can have absolutely nothing in it,
and that is called a 'NoneType'.
That's a data type.
It's a proper one. But there's just nothing in it.
So if we said that I'm about to make a grocery list, but
I don't have any items in it yet.
I could either do, you know, list is equal to an empty list, 'lst = []',
or I could say, "You know what,
I haven't even started my list yet, so it could be None.
Now that gets a little confusing with the word 'ls' and None,
so let's do something else.
Let's say how many sports do you play?
'sports = None'.
Let's say I play no sports.
I can do 'type(sports)'.
I can also, fun fact, do 'type(None)',
it'll show me the exact same thing, because this is really
just a pointer to whatever that value is.
Now, 'None' in itself is nothing.
If we cast this to a Boolean, we could do 'bool(None)',
we'll get to see, it's False.
Always evaluates as False because there's nothing in there.
Now let's see if we can do anything with this.
'sport.', and I hit tab, and nothing's happening. Literally
cannot do anything with it.
So you're probably thinking, "Kalob, where is this useful?
If I can't do anything with this and its value is nothing,
where is this useful?
Well, down the road and we'll explore this much further on,
but down the road you could say something along the lines
of 'if sports is None', do something and what this is saying,
we'll learn more about this in depth,
but what this is saying is if 'sports' is None, it is exactly
None, then do something,
otherwise maybe do something else.
Now, where this gets really useful, especially in cases like
web development, you could say up here, '?name
=', and you could check to see what that value is,
and by default you would say it's None.
So an example using a dictionary, not quite the same thing
as using a query string from a URL, but a dictionary,
so a 'dict = '
And let's not use the keyword.
'dict', let's just use the variable called 'd'.
So in here we've got a dictionary.
And if I wanted to get('thing'), this is going to return 'thing2' for me.
But what if I'm trying to 'get' something that doesn't exist?
I'm trying to 'get('something_else')', and there's a typo in
there, but I don't care.
There's nothing.
Now let's put this into a variable.
Let's put this into a throwaway variable called ''a, and let's
'print(a)'. Yeah, there's nothing there.
'type(a)'. It's a NoneType.
And so by default, this second parameter is the default,
is going to be None.
Now what we can say here, is if it's 'None', then we can say
'if a is None, print("Did not find something_else")'.
And just like that, we're able to check to see if there was
an item, a key in this dictionary, and if there wasn't,
well, then there's 'None',
and we can actually say if there was 'None', then do something.
So that's a 'NoneType'
in a nutshell. It's actually not a very useful feature because
you can use pretty much anything else if you wanted to, you
wouldn't have to use 'None'.
You could use an empty string as that would result in a False
if statement as well,
False Boolean.
Same with an empty dictionary or a tuple or a list.
You could use pretty much anything else.
But as those are usually reserved for actual specific tasks,
this one isn't, this one's just saying, "Hey, there's nothing
there". And in this case, that's perfect.
We wanted to see if there was nothing there.
You don't really need to know or do anything with this lesson,
but it's good to know that 'None' and 'NoneType', or rather just
'NoneType',
but we use the word 'None'.
'NoneType' is a data type in Python.