Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

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:

  • What None is and how to test for it
  • When and why to use None as a default parameter
  • What None and NoneType mean in your traceback
  • How to use None in type checking
  • How null in Python works under the hood
Download

Sample Code (.zip)

1015 bytes
Download

Course Slides (.pdf)

2.5 MB

00:00 Hello, and welcome to this tutorial on None in Python. Before we dive in, I’d like to tell you about the null keyword. null doesn’t exist in Python but it exists in many other languages, and it’s in many ways similar to the Python None keyword.

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:36 So once you’ve internalized these concepts in one language, you’ll see that they transfer to new languages. And as you might have guessed, null and None have a lot in common.

00:46 I would like to argue in this introductory video that we should pay extra attention to the None keyword, even though it might sound so familiar.

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:34 Words are transparent because they have cognates in other languages, and this is very useful to you if you’re learning foreign languages. But let’s consider a slightly different example.

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:00 Since you want to make a good impression on what might be your future in-laws, you decide to compliment them on their cooking, even though all they served you is a fried egg.

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.

03:06 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.

03:21 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.

03:38 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.

03:54 So, I hope I’ve convinced you that None is important and interesting, and I’ll see you in the next video!

kiran on July 29, 2020

what is difference between null and blank ?

kemjones on Aug. 1, 2020

By “null” do you mean “None”? By “blank” I assume you mean ‘’ (empty string).

The main difference between None and ‘’ is their types: The type of None is NoneType and the type of ‘’ is str (string).

type(None) <class ‘NoneType’> type(‘’) <class ‘str’>

Another difference is how they’re used. Both are “Falsy” (meaning in conditional expressions, they evaluate to False).

‘Yes’ if None else ‘No’ ‘No’ ‘Yes’ if ‘’ else ‘No’ ‘No’

not None True not ‘’ True

But they format and concatenate differently.

’{}’.format(None) ‘None’ ‘{}’.format(‘’) ‘’

None + ‘more’ Traceback (most recent call last): File “<input>”, line 1, in <module> TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’ ‘’ + ‘more’ ‘more’

kemjones on Aug. 1, 2020

Wow, I really should've previewed that before I posted… ;-)

Here’s what I meant to write:

By “null” do you mean “None”?

By “blank” I assume you mean ‘’ (empty string).

The main difference between None and ‘’ is their types: The type of None is NoneType and the type of ‘’ is str (string).

>>> type(None)
<class NoneType>  
>>> type(‘’)  
<class str>  

Another difference is how they’re used. Both are “Falsy” (meaning in conditional expressions, they evaluate to False).

>>> Yes if None else No  
No  
>>> Yes if ‘’ else No  
No

>>> not None
True
>>> not ‘’
True

But they format and concatenate differently.

>>> {}.format(None)
None
>>> {}.format(‘’)
‘’

>>> None + more
Traceback (most recent call last): File <input>, line 1, in <module> TypeError: unsupported operand type(s) for +: NoneType and str
>>> ‘’ + more
more

kiran on Aug. 2, 2020

@kemjones Thanks for the update.

>>> None + more
Traceback (most recent call last): File <input>, line 1, in <module> TypeError: unsupported operand type(s) for +: NoneType and str
>>> ‘’ + more
more

this is perfect example. Thanks.

Bartosz Zaczyński RP Team on Aug. 3, 2020

@manu

As Christian explained in his video, there’s no “null” in Python, but a lot of other programming languages use it to denote missing values.

None is a special value in Python used to indicate a lack of something. For example, functions that don’t return anything, in fact, return None implicitly:

>>> print(exec(''))
None

The word “blank” refers to empty strings, which have no characters:

>>> empty_string = ""
>>> len(empty_string)
0

Note that an empty string exists in memory and can tell you its length, which is zero, so it isn’t the same as no string whatsoever:

>>> empty_string = ""
>>> empty_string is None
False

kiran on Aug. 3, 2020

@Bartosz Zaczyński Thanks for the update.

Become a Member to join the conversation.