Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python NONE – All You Need To Know About the NONE Object

PYTHON NONE

Hey, folks! In this article, we will be focusing on Python NONE keyword.


Working of Python NONE object

Python NONE is an object in the world of Python – Object-Oriented Programming. You can think of it as ‘NULL’ value in other programming languages such as PHP, JAVA, etc.

The NONE object is of data type ‘NoneType’ and hence, it can not be considered as a value of certain primitive data types or boolean values.

Thus, we can assign NONE as value to any variable in use. Let us understand the need of NONE with example.

Consider a login form, with the backend language as Python to connect to a database. If we wish to check whether we have formed a connection to the specified database, we can assign NONE to the database connection object and verify if the connection is secured or not.

Now, let us understand the structure of Python NONE object in the below section.


Syntax of Python NONE object

NONE object does not follow the considerations of the normal data types.

Syntax:

variable = NONE

Moreover, by assigning a variable to NONE, it depicts that no-value or a null value is represented by the specific variable.

Let us now implement Python NONE object through the below examples.


Implementing NONE through examples

Let us have a look at the below example. Here, we have assigned NONE value to the variable ‘var’.

Example 1: Assigning NONE object to a Python variable

var = None
print("Value of var: ",var)

When we try to print the value stored in the variable, it shows the below output. Thereby, making it clear that NONE object represents NONE value which can be considered as a NULL value.

Output:

Value of var:  None

In the below example, we have tried to check whether Python NONE object represents an equivalent boolean value.

Example: Boolean Check against NONE object

var = None
print("Boolean Check on NONE keyword:\n")
if var:
  print("TRUE")
else:
  print("FALSE")

As seen below, the outcome is FALSE. Thus, this example makes it clear that Python NONE object is not similar to boolean or other primitive type object values.

Output:

Boolean Check on NONE keyword:
FALSE

Now, let us try to club primitive type and NoneType values to Python data structures such as Set, Lists, etc.

Example: Python NONE with Set

When we pass other primitive type values along with NONE to data structures such as sets, lists, etc, we observe that the NONE value returns ‘NONE’ as the value on printing them.

var_set = {10,None,30,40,50}
for x in var_set:
    print(x)

Output:

40
50
10
30
None

Example: Python NONE with List

var_lst = [10,None,30,40,50]
for x in var_lst:
    print(str(x))

Output:

10
None
30
40
50

Conclusion

By this, we have come to the end of this topic. Please feel free to comment below in case you come across any doubt.

Happy Learning!!


References