Have you ever encountered or used the phrase null in any situation? I'm confident that at this point, you can recall numerous instances and circumstances where you used the words "Null" or "None" to reject or say no to something. So you guessed it right, today we will understand the term “Null” in programming language. In today’s article, we will talk in-depth about Null in Python.
What is Null in Python?
Python doesn't have an attribute with the term null, to speak specifically. Python uses None in place of null. It's used to specify a null value or absolutely no value. None is different from null in many aspects, but it shares some functionalities with null in other languages.
None is distinct from 0 (zero), False (zero), and an empty string. It is a distinct data type (NoneType), and only None is capable of being None.
How do you set null in Python?
In Python, it is simple to initialize a value for a variable using several data types, such as integer, float, and string. Let's look at some examples to set null.
We can also assign a null value to a variable by using the data type None, and we can check the value of the variable by using the identity operator (is, is not).
x=None if x is None: print("True") else: print("False")
You can also check the type of the variable by using the pre-defined function of python type().
Python returns None as a value when in a function there is no return statement present.
#declaring the user defined function def Favtututor_null(): pass # calling the function print(Favtututor_null())
Here, in the above code, we have defined a function Favtutor_null but you haven’t defined any functionalities or rules set to it. So whenever a function is being called it returns empty i.e. None value. None itself doesn’t have any output so you have to print it explicitly.
Using None as a Default Parameter
We use None as a default value for the optional parameters in a function and are mutable data types like lists and dictionaries. Let us see why this is important using an example.
#declaring a function containing a list of subjects Favtutor teaches def Favtutor_subjects(new_subj, subj_list=): # subj_list is a storage box or list that holds all the # elements which are passed in new_subj by the user subj_list.append(new_subj) # appending the values in a list # returning the list return subj_list print(Favtutor_subjects(["python" , "Java"]))
print(Favtutor_subjects(["C++" , "C#"]))
[['python', 'Java'], ['C++', 'C#']]
Now, as we can see though we have called a Favtutor_subjects function individually 2 times with different list values but it has appended the list in the previous value this can cause a wastage of storage as well as transcription error in our program as one may not want to access the previously assigned values to the list.
As a solution to this problem, None comes into the role. We can set the list value as None initially and as soon as the function is called n times our compiler always checks for an empty variable and then assigns the new list to it to store the values of a list.
def Favtutor_subjects(new_subj, subj_list=None): if subj_list is None: subj_list =  subj_list.append(new_subj) return subj_list
print(Favtutor_subjects(["python" , "Java"])) print(Favtutor_subjects(["C++" , "C#"]))
[['python', 'Java']] [['C++', 'C#']]
This updated function behaves as you want by making a new list, on every function calling the list value is set to None is empty, and not able to use the previous list. It works because your code will execute lines 2 and 3 every time it calls the function with the default parameter.
Deciphering None in Tracebacks
If you have worked over some programs of python you might have come across the Attribute error stating 'Nonetype' error in your tracebacks, this is what state that there is a None value in your variable which you aren't expecting to be. The variable doesn't have any value stored in it.
This usually occurs because you’re trying to call a method on it. In the above example if we don't initialize the variable as a list in the third row again after initializing it as None. There will be a traceback error as now, subj_list is on Nonetype and not a list that can be appended with different strings or integer values.
That is if any variable is none type then you can not use any method like add, remove, or delete on it.
def Favtutor_subjects(new_subj, subj_list=None): if subj_list is None: subj_list.append(new_subj) return subj_list print(Favtutor_subjects(["python" , "Java"])) print(Favtutor_subjects(["C++" , "C#"]))
AttributeError Traceback (most recent call last) Cell In, line 5 3 subj_list.append(new_subj) 4 return subj_list ----> 5 print(Favtutor_subjects(["python" , "Java"])) 6 print(Favtutor_subjects(["C++" , "C#"])) Cell In, line 3, in Favtutor_subjects(new_subj, subj_list) 1 def Favtutor_subjects(new_subj, subj_list=None): 2 if subj_list is None: ----> 3 subj_list.append(new_subj) 4 return subj_list AttributeError: 'NoneType' object has no attribute 'append'
You can try resolving the above error by checking your code and try debugging it by printing the data type of every used variable and finding out which variable is having wrong data type and then try fixing your code.
Points to Remember
- Undefined variables and Nonetype variables are totally different. No variable is Nonetype until it is explicitly defined as None.
- None is a Singleton object.
- =None cannot be overwritten, not even as a class attribute or in the confines of a function.
- None is used as both a flag and a valid value.
Let's prove that None is a Singleton object We will verify this by creating multiple variables with None as their value and checking their id to distinguish.
a=None b=None c=None print(id(a)) print(id(b)) print(id(c))
140706933708792 140706933708792 140706933708792
All variables(a,b,c) has the same id, this shows that all none variable points to the same thing.
To conclude, None is a powerful tool used to define a null variable in the Python toolbox. Like True and False, None is an immutable keyword and None can be used to mark missing values and also default parameters. Test for None with identity operator (is and is not).