Online Tutorials & Training Materials | STechies.com
Register Login

Null Object in Python with Example

02 Nov 2019 12:21 pm || 0

While coding in Python, it is very common to assign or initialize variables with string, float or integer values. But some you may want to assign a null value to a variable. Unlike other programming languages such as PHP or Java, Python does not have a null value. Instead, there is the None keyword that you can use to define a null value.

In this article, we will focus on some of the operations associated with the None keyword.      

Python None Object

In python, the None keyword is used to define the Null value of an object or variable; it is an object and a NoneType data type.

Note:

  • We can define None to any variable or object.
  • None is not equal to 0
  • None is not equal to FALSE
  • None is not equal to an empty string
  • None is only equal to None

We can check None by keyword “is” and syntax “==”

Example:

# Python program to check None value
# initialized a variable with None value
myval = None
# Check by using 'is' keyword
if myval is None:
 print('myval is None')
else:
 print('myval is not None')
# Check by using '==' syntax
if myval == None:
 print('myval is None')
else:
 print('myval is not None')

Output:

myval is None 
myval is None

Function Explained:

Here the myval variable is initialized with the None keyword. In the next line, if myval is None:, it is checked whether the variable myval has a null value. If the statement is True, the statement print('myval is None') is displayed to the screen. If the statement is False, print('myval is not None') is printed. Here the is operator is used for the evaluation of the variable.

In the next section of the code, the if statement along with the equal to operator is used for checking whether the myVal variable has a null value. If the statement evaluates to True, the print('myval is None') is printed using the print() method.

If the statement evaluates to False, the print('myval is not None') is printed. As the myVal is set to None, the final result of the code execution is:

  • myval is None
  • myval is None

Check None type

Example

# Python program to check None value
# initialized a variable with None value
myval = None
print(type(myval))

Output:

<class 'NoneType'>

Function Explained:

Here, the myVal variable is assigned the value of None. Then, the print() method is used for printing out the type of the variable. The type method is used within the print() method. Thus, the line print(type(myval)) displays the result:

<class 'NoneType'>

This specifies the type of the myVal variable that is None.

Check if None is Equal to None

Example

# Python program to check None value
# initialized a variable with None value
myval = None
if None is None:
    print('None is None')
else:
    print('None is Not None')
print(None == None)

Output:

None is None
True

Function Explained:

In the first line, the None value is stored in the myVal variable. The next line is

if None is None:

Here, the if statement is used to check whether the None value is equal to None. If the statement evaluates to True, the string “None is None” is printed to the screen. If the condition is False, the string None is not None. The last statement prints whether None==None is True.

As per the code, the myVal variable has a None value and the statement None==None is also True. The final output is:

None is None

True

Check if None is Equal to False

Example:

# initialized a variable with None value
myval = None
if myval == False:
    print('None is equal to False')
else:
    print('None is Not Equal to False')
print(None == False)

Output:

None is Not Equal to False
False

Function Explained:

The myVal variable has None value. Then the if statement is used for checking if the myVal variable is equal to False. The print statement ‘None is equal to False’ is printed if the condition is True. Otherwise, the string ‘None is equal to False’ is printed to the console.

The last line is print(None==False) prints the result that can be True or False. After the execution of the program, the output is:

None is Not Equal to False

False

This happens as the myVal variable has a None value, so the myVal is not equal to False. Due to the same reason, the last print statement evaluates to False.

Check if None is Equal to an Empty String

Example

# Initialised a variable with None value
myval = ''
if myval == None:
    print('None is equal to empty string')
else:
    print('None is Not Equal to empty string')
print(None == myval)

Output:

None is Not Equal to an empty string
False

Function Explained:

Here the myVal variable is initialized with an empty string “ ”. Then the if statement is used for checking if this variable is equal to None. The equals to the operator is used for this evaluation. If the condition is True, the string None is equal to empty string” is printed. Otherwise, the string “None is not equal to empty string” is displayed to the screen.

The last statement is print(None==myVal). This prints True or False if the myVal variable has a None value.

As the myVal variable does not have a None value but an empty string, the string “None is not equal to empty string”. The last print() statement evaluates to False, so False is printed out.

Return None or Null From Function

# Python program to check None value
# Using a custom function
# initialized a variable with None value
myval = None
# Define function
def nullfunction(myval):
    # Check if variable is None
    if myval is None:
        result = None
    else:
        result = 'myval is not None'
    return result
print('Output: ',nullfunction(myval))

Output: 

None

Function Explained:

In the first line, the myVal variable is initialized with the None value. Then a function called nullfunction(myVal) is defined. The method takes the variable myVal as its argument. Inside it, the if statement along with the is operator, is used to check if the myVal has a None value. When the condition evaluates to True, the None value is assigned to a variable called result. Otherwise, the string “myVal is not None” is assigned to result. Then the result variable is returned by the function.

The last print() method prints out the result of the method call, nullfunction(myval). As the myval variable is equal to None, the method returns None. So, the final result is:

Output: None

Conclusion

The article has discussed different ways through which you can check whether a variable has a None value or not. Make sure there are no syntax errors while using the equals to the operator or comparison operator. During the function definition, make sure you type in the def keyword and call the function without it.