Hint: You can adjust the default video playback speed 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 see our video player troubleshooting guide to resolve the issue.

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

Comments & Discussion

manu 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

manu 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

manu on Aug. 3, 2020

@Bartosz Zaczyński Thanks for the update.

Become a Member to join the conversation.