Home »
Python »
Python programs
Python program for not None test
Python example for None keyword: Here, we are going to learn how to check whether a variable contains a value or not?
Submitted by IncludeHelp, on April 07, 2019
As we have discussed in the previous post (Python None keyword), that "None" is a keyword which can be used to assign a null value to a variable or to check whether a variable contains any value or not.
Example:
Here, we have 3 variables a, b and c, a is being assigned with "Hello", b is being assigned with "None" and c is being assigned with 10.
We are testing whether variables have a value or None, if they have values, we are printing their values.
# python code for not None test
# variable 1 with value
a = "Hello"
# variable 2 with None
b = None
# variable 3 with value
c = 10
# performing is not None test
if a is not None:
print("value of a: ", a)
else:
print("\'a\' contains None")
if b is not None:
print("value of b: ", b)
else:
print("\'b\' contains None")
if c is not None:
print("value of c: ", c)
else:
print("\'c\' contains None")
Output
value of a: Hello
'b' contains None
value of c: 10
Python Basic Programs »