Python None Keyword Usage [Practical Examples]

 

Introduction to Python None Keyword

Python None is used to define a null value. It is not the same as an empty string, False, or a zero. It is a data type of the class NoneType object. Assigning a value of None to a variable is one way to reset it to its original, empty state. In this tutorial, we will learn about Python None and will see how we can declare and initialize None in Python. We will also compare the Python None with other values and try to evaluate it. We will also discuss the None value in Python conditions by solving various examples. At the same time, we will cover some of the applications of None in the Python programming language as well.

In a nutshell, this tutorial will contain all the necessary details and examples about Python None that you need you to need to know in order to start working with it.

Advertisement

 

Getting started with Python None Keyword

As we already discussed that Python none is used to define a null value. It is not similar to an empty string or zero value, it is used to show null value in Python. In this section, we will discuss how we can define and declare a none in Python and will confirm the type by printing null variable types. Also, we will compare the Python none with some other Python values as well.

 

Declaring and assigning Python None value

Python None is a keyword, used for null values. Let us start by declaring a none variable and then printing it. See the Python code below:

# Python none declaration
myVariable = None
# printing 
print(myVariable)

Output:

None

So, when we try to print a variable assigned none, then it will print None.

 

Get type of variable assigned to Python None Keyword

Now let us print the type of variable that is assigned Python None. See the following Python program, where first we declared a None variable and then print its type.

# Python none declaration
myVariable = None
# printing the type of None varible
print(type(myVariable))

Output:

Advertisement
<class 'NoneType'>

This is in accordance with what we discussed above. A variable assigned to None in Python points to an instance of NoneType class

 

Comparison of Python None with other values

In Python when it comes to comparison( Equality), we usually use '==' or 'is' keyword. In this section, we will use both of these to compare Python None with other values. Let us first start, comparing a None value with a None value. See the Python program below:

# Python none declaration
myVariable1 = None
myVariable2 = None
# comparing None value using == operator
print(myVariable1 == myVariable2)
# comparing one value using is keyword
print(myVariable1 is myVariable2)

Output:

True
True

Notice that we get True as output because both of the variables are assigned a None value. Now, let us compare the None with an empty string. See the Python program below:

# Python none declaration
myVariable1 = None
myVariable2 = ""
# comparing None value using == operator
print(myVariable1 == myVariable2)
# comparing one value using is keyword
print(myVariable1 is myVariable2)

Output:

False
False

Notice that this time we get False as an output because The None value is not the same as an empty string. The None value has the following important properties.

Advertisement
  • None is not a  zero.
  • None is not the same as False.
  • None is not the same as an empty string ('').
  • Comparing None to any value will return False except None itself.

 

Python None in Conditional statements

The if, elif, and else statements are used in conditional statements. You can read more about Python conditional statements from the article on Python conditional statements. ( Please add a link here ). Here we will use the Python None value in conditional statements. See the Python program below:

# Python none declaration
myVariable1 = None
# Python if statement
if (myVariable1):
    # printing
    print("Condition is True")
# Python else statement
else:
    # printing
    print("Condition is False")

Output:

Condition is False

None value is considered as False in conditions but that does not mean None is the same as False. They both are different values.

 

Python None in While loop as a condition

As we already discussed that the None value is not the same as the False or Zero value in Python. Let us confirm it by using the None in while condition. See the program below which used the Python None inside while loop as a condition.

# Python none declaration
myVariable1 = None
# Python while loop
while( myVariable1 < 10):
    # printing
    print("hello")
    # incrementing the value
    myVariable1=+1

Output:

Python None

Notice that we get an error because None belongs to NoneType and cannot be used to compare with an integer type.

Advertisement

 

The applications of Python None

Python none has many important applications in the Python programming language. It is used to initialize variables, used to fix mutable default arguments, used to return None value from the function, and many more. In this section, we will discuss some of these applications of Python None in general.

 

Using Python None as an initial value of variables

When a variable doesn’t have any meaningful initial value, we can assign None. For example, let say we want to start the game if the current state is None. We can initialize our current state to none and only start the game if the value is none. For example, see the below example of Python initializing variables to none.

# Python none declaration
Current_state = None
Game_start = None
# Python if statement
if Current_state is None:
    #game start value 
    Game_start = 'Game started!!'
# Python else statement
else:
    # game start value
    Game_start = 'Exit the previous game!!'
# print
print(Game_start)

Output:

Game started!!

In the above example, we had initialized both variables to variables to None value and we use if statement to check, if the current state is none, then assign a different value to the start variable.

 

Using Python None to fix the mutable default argument issue

Before going to fix the mutable default argument issue by using Python None, let us understand the problem first. Let us say we have a python function that takes and value and a  list and append that value to the list and returns the list. See the example below:

# python function
def Student(name, details=[]):
    # appending the value to the list
    details.append(name)
    # return the list
    return details
# creating the list 
details = ["Age", "class"]
# assigning the name
name = "Bashir"
# calling the function
print(Student(name, details))

Output:

Advertisement
['Age', 'class', 'Bashir']

As expected we get a list with the appended value. However, the problem arises when we use the default value of the second parameter. For example, see the program below:

# python function
def Student(name, details=[]):
    # appending the value to the list
    details.append(name)
    # return the list
    return details
# assigning the name
name1 = "Bashir"
name2 = "Alam"
# calling the function
student1 = Student(name1)
student2 = Student(name2)
# printing
print(student1)
print(student2)

Output:

['Bashir', 'Alam']
['Bashir', 'Alam']

Notice that the function creates the list, once defined and uses the same list in each successive call, that is why we are getting both names in the same list but our target is to create different lists for each of the students. We can fix this issue by using None inside our function. See the example below:

# python function with Details to None
def Student(name, details= None):
    # using Python 
    if details is None:
        details = []
    # appending the value to the list
    details.append(name)
    # return the list
    return details
# assigning the name
name1 = "Bashir"
name2 = "Alam"
# calling the function
student1 = Student(name1)
student2 = Student(name2)
# printing
print(student1)
print(student2)

Output:

['Bashir']
['Alam']

Notice that we solved the problem by using the None value and getting a different list for each of the students, rather than getting the same list and appending the names of each student in one list.

 

Using Python None as a return value of a function

In the Python programming language, when a function does not return anything, then by default its return value is None. See the following program below:

Advertisement
# python function without any return value
def Student(name):
    name = "Alam"
# calling the function and printing
print(Student("Bashir")) 

Output:

None

Notice that the function does not have any return value and by default, it return None.

 

Summary

None is used to define a null value. It is not the same as an empty string, False, or a zero. It is a data type of the class NoneType object.  Assigning a value of None to a variable is one way to reset it to its original, empty state. In this tutorial, we learned about Python None. We discussed how we can declare and assign a None value to a variable and then compared the None value with other values through various examples.

We also covered how we can use the None value in different conditions as well. Moreover, we also discussed some of the applications of None value in the Python programming language including initializing variables, fixing mutable default arguments, and using to return None value from the function. To summarize, this tutorial contains all the necessary information and examples that you need to know in order to start working with Python None.

 

Further Reading

Python None
Python Conditional statements
Python loops

 

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X