{"id":18334,"date":"2021-06-22T16:56:29","date_gmt":"2021-06-22T16:56:29","guid":{"rendered":"https:\/\/www.askpython.com\/?p=18334"},"modified":"2023-04-10T11:02:35","modified_gmt":"2023-04-10T11:02:35","slug":"python-null","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/oops\/python-null","title":{"rendered":"Python NULL &#8211; How to identify null values in Python?"},"content":{"rendered":"\n<p>In many programming languages, &#8216;null&#8217; is used to denote an empty variable or a pointer that does not point to anything. &#8216;null&#8217; is basically equal to 0. Whereas in Python, there is no &#8216;null&#8217; keyword available. Instead, &#8216;None&#8217; is used for this purpose, which is an object.<\/p>\n\n\n\n<p>This tutorial will provide you with a comprehensive guide on null in Python and we will also learn how to declare a null variable in Python. Additionally, we will also see how to check if a variable is None. Let&#8217;s start with an introduction to null in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Null in Python<\/h2>\n\n\n\n<p>Null is Python practically does not exist, it uses None instead. Whenever a function doesn&#8217;t have anything to return i.e., it does not contain the return statement, then the output will be None.  <\/p>\n\n\n\n<p>In simpler words, the None keyword is used to define a null variable or null object.  <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef func_no_return():\n    a = 5\n    b = 7\nprint(func_no_return())\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nNone\n<\/pre><\/div>\n\n\n<p><strong>NOTE:<\/strong> Whenever we assign None to a variable, all the variables that are assigned to it point to the same object. No new instances are created.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Data Type of None<\/h3>\n\n\n\n<p>In Python, unlike other languages, null is not just the synonym for 0, but is an object in itself.<\/p>\n\n\n\n<p>We can use the type() method which returns the class type of the object passed to it. It accepts only one argument. To see the None type, we pass None as an argument to this method.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(type(None))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;class &#039;NoneType&#039;&gt;\n<\/pre><\/div>\n\n\n<p>In the above output, we can see that type()  method returns <code>&lt;class 'NoneType'&gt;<\/code> which means, None is an object in Python, and the data type of the class is NoneType.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Declaring null variables in Python<\/h2>\n\n\n\n<p>Null variables in Python are not declared by default. That is, an undefined variable will not be the same as a null variable. To understand, all the variables in Python come into existence by assignment only. Have a look at the code below: <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"383\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/code-example.png\" alt=\"Code Example\" class=\"wp-image-18341\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/code-example.png 725w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/code-example-300x158.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><\/figure>\n\n\n\n<p>The above code shows the difference between an undefined variable and a None variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to check if a variable is None in Python?<\/h2>\n\n\n\n<p>You can check whether a variable is None or not either using the <a href=\"https:\/\/www.askpython.com\/python\/examples\/difference-between-is-and-double-equals\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/examples\/difference-between-is-and-double-equals\" rel=\"noreferrer noopener\">is operator<\/a> or the <a href=\"https:\/\/www.askpython.com\/python\/examples\/difference-between-equal-to-and-is\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/examples\/difference-between-equal-to-and-is\" rel=\"noreferrer noopener\"> == operator<\/a> as shown below <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using the &#8216;is&#8217; operator<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#declaring a None variable\na = None\n\nif a is None:                   #checking if variable is None\n    print(&quot;None&quot;)\nelse:\n    print(&quot;Not None&quot;)\n<\/pre><\/div>\n\n\n<p>Here we are checking if<code> a is none<\/code> which returns true then print &#8220;None&#8221;, returns false then prints &#8220;Not None&#8221;.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nNone\n<\/pre><\/div>\n\n\n<p>We got None, which means the &#8216;is&#8221; operator successfully identified it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using the &#8216;==&#8217; operator<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#declaring a None variable\na = None\n\nif (a == None):                   #checking if variable is None\n    print(&quot;The value of the variable is None&quot;)\nelse :\n    print(&quot;The value of variable is Not None&quot;)\n<\/pre><\/div>\n\n\n<p>Here we are checking if a is None using the equality operator.  If the equality operator returns true then print &#8220;The value of the variable is None&#8221;, else print &#8220;The value of variable is Not None&#8221;.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nThe value of variable is None\n<\/pre><\/div>\n\n\n<p>Here we got the expected result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we have learned that the None keyword is used to define a null variable, None is of immutable type and can be used to mark missing values and default parameters. We have also learned to check if a variable is None in Python using the &#8216;is&#8217; operator and the &#8216;==&#8217; operator, which can be used accordingly. Hope you find this tutorial useful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/19473185\/what-is-a-none-value\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/19473185\/what-is-a-none-value<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In many programming languages, &#8216;null&#8217; is used to denote an empty variable or a pointer that does not point to anything. &#8216;null&#8217; is basically equal to 0. Whereas in Python, there is no &#8216;null&#8217; keyword available. Instead, &#8216;None&#8217; is used for this purpose, which is an object. This tutorial will provide you with a comprehensive [&hellip;]<\/p>\n","protected":false},"author":34,"featured_media":18643,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-18334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oops"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/18334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=18334"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/18334\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/18643"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=18334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=18334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=18334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}