PYTHON NULL STATEMENT

In Mathematics, we use null to represent nothing, and in some programming languages such as C and Java, NULL represents the same, but in Python, it is different. Generally, in other programming languages, null is used when a pointer point to nothing or when a variable has nothing/ is empty. It is equivalent to zero. In the place of null, we use 'None' in Python. This article discusses the concept of None with examples.

None is a keyword in Python. It does all the functions that null can, and along with that, there is a whole new concept and functionality behind None.

In languages like C, null is a built-in constant with zero value. But, in Python, None is an object.

  • Like True and False, in Python, None is an immutable keyword with significance in different scenarios.

Output:

<class 'NoneType'>

Represents that None is an object of the class NoneType. The type of an object is the class it belongs to.

  • In functions: return type

Generally, functions are expected to return at least one value. If a function does not return anything, it returns None:

Output:

None

Understanding: The function takes parameters and performs addition but not returning any value. Hence, when we printed the function, None was printed.

  • Interesting fact:

We know that print () is a function in Python. But do you observe that print () prints the given string, but it doesn't return any value? When we try to print a print statement, we can see that None is printed:

Output:

Hello world!
None

Understanding:

First, the string is printed in the first statement, and then the return value is printed-None.

  • As a default value for optional parameters in functions

When we see the syntax of some functions, we observe some parameters we don't even know in the function. Those are the optional parameters. These parameters have some functionality, but we won't use them frequently. We assign an argument to the parameter only when we need them. All the other time, they hold None.

Example:

list. sort () function

Generally, we call it using a list with no arguments, but there are two arguments in the function:

Output:

[1, 3, 5, 6]

The actual syntax of the sort:

Help on method_descriptor:

sort(self, /, *, key=None, reverse=False)

Sort the list in ascending order and return None.

  • Null variables

In statically typed languages like C, the programmer can only use a variable in his program after defining and declaring some value. If we don't declare it with a value, it takes some garbage value or null.

It is not the same in Python. We can assign a variable with None, making it a null variable. But, null variables are not the same as not defined variables.

Output:

Traceback (most recent call last):
  File "C:\Users\Jeevani\untitled0.py", line 7, in 
    print (var)
NameError: name 'var' is not defined

Here, var is not defined but used in the print statement. Var didn't take None as a default value. Hence, it raised an error.

Output:

None

Here, we assigned None to the variable var. When we printed the value, you can see that we got None printed.

  • Attribute Errors

If you have been programming for quite some time, you might have run into these errors:

AttributeError: 'NoneType; object has no attribute 'append'

This error occurs when we try to perform operations on a null value that is not defined. These situations occur unexpectedly due to some error in the code. We think they are normal variables, but they are null variables.

Example:

Output:

Traceback (most recent call last):
  File "C:\Users\Jeevani\untitled0.py", line 11, in 
    function(l). append('hey')
AttributeError: 'NoneType' object has no attribute 'append'

Understanding:

Here, we created a list [3, 4]. We wrote a function to append the value 8 to the list. We called the function but forgot to return the list. As discussed above, if a function doesn't have a return statement, it returns None, and here we tried to append "hey" to a none object, which is impossible. Hence, the attribute error is raised.

  • Comparison and equality

Let us understand this using an example:

Output:

Enter a number to check it is even: 7
7 is not an odd number

Enter a number to check it is even: 4
4 is an even number

Understanding:

We took an integer as input from the user. We wrote a function to check if the number is even or odd. If the number is divisible by 2, True will be returned. If it is not divisible, it is not returning anything, which means None is returned. We used this to check if the number was odd.

Important point:

To check if a variable is None, do not use the comparison operators == and !=. Even if we use ==, it would have given the same output in the above case. But, using == leads to bugs in some cases:

If we use "is", it gives True when both the variables are pointing to the same object but == checks if the objects that both variables are pointing to are the same.

Let us see an example:

Output:

True 
False

Understanding:

When we say obj is None, we check if both obj and None are the same objects. Obj is an object to the class name, and None is a predefined object. They are not the same. When we check obj == None, we check if obj and None are equivalents, i.e., if they have the same value. Both obj and None are holding None. Hence True is printed.

  • True and False in if

When we say None, it means there isn't anything. Generally, if statement will be executed if there is some positive integer or a True value in the statement's condition. None represents nothing which means it represents false. Then, not None represents True:

Output:

She did not eat the food

Understanding:

We gave food None, which means it is a blank-it has nothing. It represents False. Hence, the else statement is executed.

  • As a default argument

Let us understand this using an example:

We are trying to create a function that appends a list with the given element. If no list is given, we want a single-element list with the element we provide.

Output:

[3, 2, 3]
[4]
[4, 4]

Understanding:

When we gave a list and appended it in the first case, the function worked fine. But, it failed when we stopped giving the list and gave only the element. When we call the function without the list, the function updates the list with the appended values as the list is not re-defined as an empty list.

Solution:

Output:

[3, 3, 2]
[4]
[5]

Here, we assigned None as the default value to the parameter. Whenever the function is called, we include a condition to check if it is None and then re-assigned the list as an empty list.

These are some of the uses of None in Python. When we create large programs, the significance of None will be more. It improves the readability and reduces the number of lines of the program, making it a beautiful and user-friendly feature in Python for every programmer.






Latest Courses