Python's None: Null in Python (Overview)
If you have experience with other programming languages, like C or Java, then you’ve probably heard of the concept of
null. Many languages use this to represent a pointer that doesn’t point to anything, to denote when a variable is empty, or to mark default parameters that you haven’t yet supplied.
null is often defined to be
0 in those languages, but
null in Python is different.
Python uses the keyword
None to define
null objects and variables. While
None does serve some of the same purposes as
null in other languages, it’s another beast entirely. As the
null in Python,
None is not defined to be
0 or any other value. In Python,
None is an object and a first-class citizen!
In this course, you’ll learn:
Noneis and how to test for it
- When and why to use
Noneas a default parameter
NoneTypemean in your traceback
- How to use
Nonein type checking
nullin Python works under the hood
Hello, and welcome to this tutorial on
None in Python. Before we dive in, I’d like to tell you about the
null doesn’t exist in Python but it exists in many other languages, and it’s in many ways similar to the Python
00:17 That’s because programming languages have a lot in common, just like natural human languages do. In fact, the hardest programming language for you to learn will probably be the first one you learn, because you will encounter concepts such as variables, conditionals, and so on, which appear again in every other language which you will learn.
00:54 Let’s look at an example from natural languages. Consider this. It’s a house. The English word for this is house. The German word for this is Haus. Even if you don’t speak any German and don’t read any German, what you see on the left of this slide probably looks very familiar and you would be able to understand it if you came across it.
01:15 The reason for that is, of course, that natural languages are often related to each other, so Germanic languages and Romance languages feel a bit like variations on a theme, right? And there are many words or expressions, for instance, in Italian that a French speaker would understand, or that a Spanish speaker would understand. This is called transparency.
01:45 You’re an English speaker and you decide to go on holiday to Portugal. And while you’re there, you meet a girl and things are going very well with her. So well, in fact, that she invites you to meet her parents, and being very hospitable and warm, they decide to cook for you.
02:09 So you’ll try to say something like, “The food is exquisite!” Except you want to impress them, so you’re going to try to say this in Portuguese, and you might try to say something like “A comida é esquisita!” And this seems like it should go down very well, except it doesn’t. Not at all.
02:25 You find yourself in a very embarrassing situation. And the reason for that is that although esquisita and exquisite share common roots, they’re actually quite different in key ways. In fact, the meaning of esquisita in Portuguese isn’t exquisite, like in English. Instead, it’s strange, weird or unpleasant. So, no wonder that didn’t go down very well.
02:49 This is what language learners sometimes refer to as false friends. They’re words which seem like they should mean the same thing, but instead of being helpful, what they do is they give you a misplaced confidence. Because they look so familiar, you think you understand them, and that’s where you trip up.
And that’s what I hope you’ll take away from this course: that
None in Python has its own specificities, which are not like
null in other languages. Being aware of these specificities will keep you out of trouble and it’ll make you a better programmer.
This course is broken down into the following chapters. There’s an introductory video, that’s where we are now. This is followed by “What is
None and How to test for it,” that’s sort of an overview of what the
None keyword is. Next, we’ll look at how you can use
None as a default parameter in function definitions and why you should.
And then finally, just before the concluding video, we’ll look at what happens when
None shows up in tracebacks. What’s the best way to tackle those situations where your error messages are featuring
NoneTypes? After that, we’ll wrap up in the concluding video and we’ll go over all of the key points that we discussed.
Become a Member to join the conversation.