Null in Python

Posted in /   /  

Null in Python

Vinay Khatri
Last updated on June 9, 2022

    In other high-level programming languages such as Java, C and C++ we have the concept of null. There null is used to represent or point empty value or no value at all. But in Python, we do not have null instead we use the None keyword which is pretty similar to the concept of null.

    Python None keyword

    In Python, we use the None keyword to define no value or null objects. In other languages null is also defined as 0 but in Python, we can not define 0 as None, because "None" itself a separate object in Python. Example

    >>> type(None)
    <class 'NoneType'>

    Declaration of Null or None variable in Python

    In static languages, we need to declare a variable before initialisation. But Python is a dynamic language, so here we do not have the concept of variable declaration In Python, we can directly initialise or assign a value to the variable. In Python, if we try to access a variable before initialisation, then the interpreter thrown an error "name not defined" . Example 1

    #accessing a variable before initializaiton
    >>> print(a)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'a' is not defined

    Example 2

    >>> a = None
    >>> print(a)

    <Note>: In Python, a variable will occupy space or come in existence when a value is assigned to it, but in a static language, a variable comes in existence when it is declared.

    null or Python None with Functions

    If a function does not have a return statement, then it returns None object. Example

    def add():
        a = 2 + 3
    # print the return value of add() function 



    Here you can see that there is no return statement in the add() function so the function returns no value or None. <Note> Python print() function also return None.

    Use Python None value

    Often None is used in comparison statements. One of the primary examples is when we want to check if the result is None or not.

    def add():
        a = 2 + 3
        result = add()
    if result == None:
        print("Function add() returns no value")

    Output Function add() returns no value

    None as Falsy

    In Python, None is treated as Falsy value like 0 or False, which means if statement read None as False value. There are many other falsy objects, such as:

    • Empty list
    • Empty dictionary
    • Empty set
    • Empty string
    • 0
    • False


    if None:
        print("The statement is True")
        print("The statement is False")


    The statement is Flase

    The above example is similar to

    if False:
        print("The statement is True")
        print("The statement is False")


    The statement is False


    • None is a keyword that represents no value.
    • If a function has no return statement than it returns None by default.
    • In Python None treated as a false value.

    People are also reading:

    Leave a Comment on this Post