The course is part of this learning path
This brief course takes a quick look at the none object in Python, as well as the is and is not operators.
Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. This course is part of a series of content designed to help you learn to program with the Python programming language.
Should you wish to ask me a specific question, you can do that with the contact details on screen. You can also reach support by using the email address: email@example.com. And one of our cloud experts will reply.
The number zero is kind of interesting because it indicates the lack of a value. The very concept of zero is used to determine the absence of something. For example: if you have five apples, that’s quantifiable. However, if you don’t have any apples, then you have zero apples. Knowing when something is missing can often be important.
In this lesson, we’re going to explore an important concept of the Python runtime. The concept of nothing. When working with data there’s often missing information. Data can be missing for any number of reasons. For example: maybe data couldn’t be collected for some reason.
This missing data begs the question how do we represent the fact that this data is missing? In some cases, you might be able to populate the missing data with sensible default values. However, that won’t work in all cases.
Python’s solution for representing missing data is by using the None object. When the Python runtime first starts up it creates a singular object called None. This object can be used to indicate when some expected data is missing.
Because the concept of None is so common it’s integrated into Python’s language syntax. When the interpreter encounters the keyword None, it automatically replaces that with the singular none object.
Here’s an example: Let’s start by binding the name email to the none object by using the None keyword. Notice that similar to the boolean keywords of True and False, None begins with an uppercase letter. By binding the name email to None, we’ve created a name binding that currently doesn’t have any data.
Now, if we rebind email to a string this name is bound to an object with some actual data. So, the concept of none enables us to determine if some piece of data is missing. To actually check if an object is None we can use the is and is not operators.
Imagine that we have a function named email_for_user which accepts a user name as input and returns the email address for that user. If no email address exists for this user then the function returns None. Now we have a bit of a problem. Because we don’t actually know if the name email is bound to a string containing an email address or if it’s bound to None.
Using the is operator we can compare the bound object to None. If the object bound to the name email is None then this next line runs and uses the built-in input function to ask for an email address. The is not operator can be used to determine if an object is not missing. These two operators are two sides of the same coin. Both compare an object to None and determine if the object is or is not None.
None is conceptually basic in that it’s a single object used to represent the absence of any other object. However, if you haven’t spent much time working with different types of data such as spreadsheets, web forms, databases, etc, then its use cases might be difficult to fully imagine.
The concept of representing missing data is quite common in computer science. Python can integrate with different databases, web applications, and applications. Many of these include some way to represent missing data. Because Python includes the None object, it can more accurately reflect the external data.
The world of data is a very messy place; full of missing and inaccurate data. Developers often have to work with and around this missing data. And the None object helps us to identify missing data.
Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:
- None is used to indicate the absence of data
- To determine if an object is or is not missing we can use the is and is not operators.
That's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.