{"id":14161,"date":"2023-03-10T10:34:53","date_gmt":"2023-03-10T10:34:53","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=14161"},"modified":"2023-07-02T18:57:24","modified_gmt":"2023-07-02T18:57:24","slug":"python-not-operator","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/python-not-operator\/","title":{"rendered":"Python Not Operator"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg\" alt=\"\" \/><\/p>\n<p>Python not operator is one of the many operators provided by a python that can be used to manipulate and compare values in your code. Python not operator allows you to negate a boolean expression. While moving further in this blog we will take a closer at the not operator in Python, how it works, and how it can be used in your code.<\/p>\n<h2>Understanding Boolean Logic in Python<\/h2>\n<p>George Boole invented Boolean algebra, which uses True and False values. It also defines the Boolean operations AND, OR, and NOT. These Boolean elements and operators are useful in programming because they allow you to choose the path your programs should take. The Python bool data type is a subclass of the Python int object.<br \/>\n*<em>Code Implementation <\/em><\/p>\n<h3>Python program to show that boolean objects are a subclass of the int object<\/h3>\n<h3>Checking if bool is a subclass of int<\/h3>\n<pre><code>print(issubclass(bool, int))  <\/code><\/pre>\n<h1>Getting details of bool<\/h1>\n<p>help(bool)  <\/p>\n<p>Output<\/p>\n<pre><code>True\nHelp on class bool in module builtins:<\/code><\/pre>\n<p>class bool(int)<br \/>\n|  bool(x) -&gt; bool<\/p>\n<h2>What is the Not Operator in Python?<\/h2>\n<p>The not operator is a logical operator in Python that is used to invert the value of a Boolean expression. It is a unary operator, which means it operates on a single operand. When used with a Boolean expression, the not operator returns the opposite of the expression&#8217;s value. If the expression is true, not return false, and if the expression is false, not return true.<\/p>\n<h3>Syntax of Python Not Operator<\/h3>\n<p>The syntax for the not operator is simple. It is represented by the keyword not, followed by the expression that you want to invert. Here is an example:<\/p>\n<pre><code>not x<\/code><\/pre>\n<p><strong>Explanation of the syntax of python not operator<\/strong><br \/>\nThe Python not operator applies logical negative to a Boolean statement.<\/p>\n<p>You must then specify an expression or a variable that is linked to a boolean result. Not will assess a check as False if it is True and vice versa.<br \/>\nAlong with the AND and OR operators, the not operator in Python is regarded as one of the language&#8217;s fundamental operators.<br \/>\nSimilar to the not gate used in digital electronics, it serves the same purpose.<\/p>\n<h2>How to use Python Not Operator?<\/h2>\n<p>The not operator is the sole unary Boolean operator available in Python. To put it another way, the processor only takes one input. In Python, you write not x to apply the not operator to the argument x.<\/p>\n<p>Depending on the meaning of the input, the not operator in Python can return either Yes or False.<br \/>\nYou can understand the python not operator by the following table:<\/p>\n<table>\n<thead>\n<tr>\n<th>If the expression returns<\/th>\n<th>Not operator returns<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>True<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<td>False<\/td>\n<td>True<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Examples of Python Not Operator<\/h3>\n<p>In this section of the blog, we will discuss various examples of python not operator with code and implementations.<\/p>\n<p><strong>Example 1 of Python not Operator: Inverting the input<\/strong><br \/>\nBelow is the implementation of the above-mentioned example.<\/p>\n<p><strong>Code Implementation<\/strong><\/p>\n<pre><code>x = True\nprint(not x)<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code>False<\/code><\/pre>\n<p><strong>Explanation of the above example<\/strong><br \/>\nIn this example, the value of x is True. The not operator is used to invert the value of x, so the output is False.<\/p>\n<p><strong>Example 2 of Python not operator: Using if statements<\/strong><br \/>\nThe not operator is often used in conjunction with if statements to test for the opposite of a condition. For example, if you want to test if a variable is not equal to a certain value, you can use the not operator to invert the condition.<\/p>\n<p><strong>Code Implementation<\/strong><\/p>\n<pre><code>x = 5\nif not x == 10:\nprint(\"x is not equal to 10\")<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code>x is not equal to 10<\/code><\/pre>\n<p><strong>Explanation of the above example<\/strong><br \/>\nIn this example, the if statement tests whether x is not equal to 10. The not operator is used to invert the equality condition, so the statement is true when x is not equal to 10.<\/p>\n<p><strong>Example 3 of Python not operator: Using while loop<\/strong><br \/>\nWe will see the code and implementation of the python not operator by using while loops.<\/p>\n<p><strong>Code Implementation<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_14031 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_14031 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_14031 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_14031 .wpsm_nav-tabs > li.active > a, #tab_container_14031 .wpsm_nav-tabs > li.active > a:hover, #tab_container_14031 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_14031 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_14031 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_14031 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_14031 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_14031 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_14031 .wpsm_nav-tabs > li > a:hover , #tab_container_14031 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_14031 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_14031 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_14031 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_14031 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_14031 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_14031 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_14031 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_14031 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_14031 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_14031 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_14031 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_14031\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_14031\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_14031_1\" aria-controls=\"tabs_desc_14031_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_14031\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_14031_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">i = 0\r\nwhile not i == 5:\r\n    print(i)\r\n    i += 1<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_14031 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_14031 a\"),jQuery(\"#tab-content_14031\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>0\n1\n2\n3\n4<\/code><\/pre>\n<p><strong>Explanation of the above example<\/strong><br \/>\nIn this example, the not operator is used with a while loop to print the values of i until it reaches 5. The condition for the while loop is &quot;not i == 5&quot;, which means the loop will continue until i is equal to 5.<\/p>\n<p><strong>Example 4 of Python not Operator: Using in a non-Boolean context<\/strong><br \/>\nWe can also use python not operator in a non-boolean context we will see the code and implementation of the same below:<\/p>\n<p><strong>Code Implementation<\/strong><\/p>\n<pre><code>a = 0\nb = not a\nprint(b)<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code>True<\/code><\/pre>\n<p><strong>Explanation of the above example<\/strong><br \/>\nIn this example, the not operator is used in a non-Boolean context. The variable a is set to 0, which is equivalent to False in a Boolean context. The variable b is then set to the opposite of a using the not operator. Since not False is True, the value of b is True.<\/p>\n<p><strong>Example 5 of Python not Operator: Using not with lists and tuples<\/strong><br \/>\nWe can use the python not operator with list or tuple while performing operations on them. We will see the implementation of the example of the same below:<\/p>\n<p><strong>Code Implementation<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_14032 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_14032 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_14032 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_14032 .wpsm_nav-tabs > li.active > a, #tab_container_14032 .wpsm_nav-tabs > li.active > a:hover, #tab_container_14032 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_14032 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_14032 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_14032 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_14032 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_14032 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_14032 .wpsm_nav-tabs > li > a:hover , #tab_container_14032 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_14032 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_14032 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_14032 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_14032 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_14032 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_14032 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_14032 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_14032 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_14032 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_14032 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_14032 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_14032\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_14032\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_14032_1\" aria-controls=\"tabs_desc_14032_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_14032\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_14032_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">my_list = [1, 2, 3, 4, 5]\r\nmy_tuple = (6, 7, 8, 9, 10)\r\n\r\nif not 6 in my_list:\r\n    print(\"6 is not in my_list\")\r\n\r\nif not 1 in my_tuple:\r\n    print(\"1 is not in my_tuple\")<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_14032 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_14032 a\"),jQuery(\"#tab-content_14032\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>6 is not in my_list\n1 is not in my_tuple<\/code><\/pre>\n<p><strong>Explanation of the abvove example<\/strong><br \/>\nIn this example, the not operator is used with lists and tuples to test whether a certain value is not in the sequence. In the first, if statement, the not operator is used to test whether 6 is not in the list my_list. Since 6 is not in my_list, the statement is true, and &quot;6 is not in my_list&quot; is printed. In the second if statement, the not operator is used to test whether 1 is not in the tuple my_tuple. Since 1 is not in my_tuple, the statement is true, and &quot;1 is not in my_tuple&quot; is printed.<\/p>\n<h2>Applications of Python Not Operator<\/h2>\n<p>The python not operator is used to perform logical negation on a Boolean value, which means it inverts the truth value of an expression. This operator can be used in a wide range of applications, such as:<\/p>\n<ul>\n<li><strong>Checking for the absence of a value or condition:<\/strong> The not operator can be used to check if a value or condition is not present or is false. For example, if you want to check if a list is empty, you can use the not operator as follows:<\/li>\n<li><strong>Error handling:<\/strong> The not operator can be used in error handling to check if an exception is not raised. For example, if you want to catch all exceptions except a specific one, you can use the not operator in the except clause.<\/li>\n<li><strong>Boolean expressions:<\/strong> The not operator can be used to negate the truth value of a Boolean expression. For example, if you have a Boolean variable is_valid that represents whether a certain condition is true or false, you can use the not operator to check if the condition is false.<\/li>\n<li><strong>Looping:<\/strong> The not operator can be used in loops to continue looping until a condition becomes true.<\/li>\n<\/ul>\n<p><strong>Conclusion<\/strong><br \/>\nThe not operator is a useful tool in Python that allows you to invert the value of a Boolean expression. It can be used in a variety of situations, such as in if statements and with and and, or operators. It can also be used with functions that return Boolean values. By understanding how the not operator works and how it can be used, you can write more efficient and concise code in Python.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<p>Here are some of the frequently asked questions and answers about python not operator.<\/p>\n<p><strong>1. How is the not operator used in conjunction with if statements?<\/strong><br \/>\nThe not operator is often used in conjunction with if statements to test for the opposite of a condition. For example, if you want to test if a variable is not equal to a certain value, you can use the not operator to invert the condition.<\/p>\n<p><strong>2. Can the not operator be used with and and or operators?<\/strong><br \/>\nYes, the not operator can be used in conjunction with the and and or operators to create complex Boolean expressions. For example, you can use the not operator to negate the result of an and or or operation.<\/p>\n<p><strong>3. Can the not operator be used with functions that return Boolean values?<\/strong><br \/>\nYes, the not operator can be used with functions that return Boolean values. For example, you can use the not operator to test the opposite of a function&#8217;s return value.<\/p>\n<p><strong>4. What is the difference between the not operator and the != operator in Python?<\/strong><br \/>\nThe not operator and the != operator both test for inequality, but they do it in different ways. The != operator tests whether two values are not equal to each other, while the not operator tests the opposite of a Boolean expression.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python not operator is one of the many operators provided by a python that can be used to manipulate and compare values in your code. Python not operator allows you to negate a boolean expression. While moving further in this blog we will take a closer at the not operator in Python, how it works, [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[154],"tags":[],"class_list":["post-14161","post","type-post","status-publish","format-standard","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Not Operator<\/title>\n<meta name=\"description\" content=\"Python not operator is a logical operator that is used to invert the value of a Boolean expression. It is a unary operator, which operates on a single operand.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/python-not-operator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Not Operator\" \/>\n<meta property=\"og:description\" content=\"Python not operator is a logical operator that is used to invert the value of a Boolean expression. It is a unary operator, which operates on a single operand.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/python-not-operator\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-10T10:34:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-02T18:57:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Python Not Operator\",\"datePublished\":\"2023-03-10T10:34:53+00:00\",\"dateModified\":\"2023-07-02T18:57:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/\"},\"wordCount\":1428,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/python-not-operator\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/\",\"name\":\"Python Not Operator\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg\",\"datePublished\":\"2023-03-10T10:34:53+00:00\",\"dateModified\":\"2023-07-02T18:57:24+00:00\",\"description\":\"Python not operator is a logical operator that is used to invert the value of a Boolean expression. It is a unary operator, which operates on a single operand.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/python-not-operator\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/python-not-operator\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Not Operator\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Not Operator","description":"Python not operator is a logical operator that is used to invert the value of a Boolean expression. It is a unary operator, which operates on a single operand.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/python-not-operator\/","og_locale":"en_US","og_type":"article","og_title":"Python Not Operator","og_description":"Python not operator is a logical operator that is used to invert the value of a Boolean expression. It is a unary operator, which operates on a single operand.","og_url":"https:\/\/prepbytes.com\/blog\/python-not-operator\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-03-10T10:34:53+00:00","article_modified_time":"2023-07-02T18:57:24+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Python Not Operator","datePublished":"2023-03-10T10:34:53+00:00","dateModified":"2023-07-02T18:57:24+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/"},"wordCount":1428,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/python-not-operator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/","url":"https:\/\/prepbytes.com\/blog\/python-not-operator\/","name":"Python Not Operator","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg","datePublished":"2023-03-10T10:34:53+00:00","dateModified":"2023-07-02T18:57:24+00:00","description":"Python not operator is a logical operator that is used to invert the value of a Boolean expression. It is a unary operator, which operates on a single operand.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/python-not-operator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1678187960795-Python%20NOT%20Operator.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/python-not-operator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/prepbytes.com\/blog\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Not Operator"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/14161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=14161"}],"version-history":[{"count":3,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/14161\/revisions"}],"predecessor-version":[{"id":17051,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/14161\/revisions\/17051"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=14161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=14161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=14161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}