How to Properly Check if a Variable is Not Null in Python
- Last modified: 19 November 2020
- Category: python tutorial
In this tutorial, I will show you how to check if a variable is empty in different methods.
note: Python programming uses None instead of null.
Contents
1. Checking if the variable is not null [Method 1]
#variable
var = "hello python"
#check is not null
if var is not None:
print('Var is not null')
output
Var is not null
2. Checking if the variable is not null [Method 2]
#variable
var = "hello python"
#check is not null
if var:
print('Var is not null')
output
Var is not null
3. Checking if the variable is not null [Method 3]
test = "hello python"
# if variable is not null
if test != None :
print('yes! the var is not null')
output
yes! the var is not null
Conclusion
All methods work perfectly, but the method 1 is the best way to check if a variable is not null (None)
English today is not an art to be mastered it's just a tool to use to get a result