{"id":6978,"date":"2024-04-11T12:00:39","date_gmt":"2024-04-11T12:00:39","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=6978"},"modified":"2024-04-11T12:00:39","modified_gmt":"2024-04-11T12:00:39","slug":"python-logical-operators","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-logical-operators\/","title":{"rendered":"Python Logical Operators"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-the-logical-operators-in-python\">What is the Logical Operators in Python?<\/a><\/li><li ><a href=\"#what-is-truth-table-for-logical-operators-in-python\">What is Truth Table for Logical Operators in Python?<\/a><\/li><li ><a href=\"#what-is-the-logical-and-operator-in-python\">What is the Logical AND operator in Python?<\/a><\/li><li ><a href=\"#what-is-the-logical-or-operator-in-python\">What is the Logical OR operator in Python?<\/a><\/li><li ><a href=\"#what-is-the-logical-not-operator-in-python\">What is the Logical NOT operator in Python?<\/a><\/li><li ><a href=\"#what-is-order-of-precedence-of-logical-operators\">What is Order of Precedence of Logical Operators?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-logical-operators-fa-qs\">Python Logical Operators- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>The logical operators are used to combine the conditional statements. Let us check out this to learn more about the Python logical operators in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-logical-operators-in-python\">What is the Logical Operators in Python?<\/h2>\n\n\n\n<p>In <a href=\"https:\/\/www.skillvertex.com\/blog\/python-literals\/\" data-type=\"post\" data-id=\"6953\">Python,<\/a> Logical <a href=\"https:\/\/www.skillvertex.com\/blog\/python-operators\/\" data-type=\"post\" data-id=\"6937\">operators<\/a> will function on the conditional statements. They will work on Logical AND, Logical OR, and Logical NOT operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-truth-table-for-logical-operators-in-python\">What is Truth Table for Logical Operators in Python?<\/h2>\n\n\n\n<figure><table><tbody><tr><td>X<\/td><td>Y<\/td><td>X and Z<\/td><td>X or Y<\/td><td>not (X)<\/td><td>not (Y)<\/td><\/tr><tr><td>T<\/td><td>T<\/td><td>T<\/td><td>T<\/td><td>F<\/td><td>F<\/td><\/tr><tr><td>T<\/td><td>F<\/td><td>F<\/td><td>F<\/td><td>F<\/td><td>T<\/td><\/tr><tr><td>F<\/td><td>T<\/td><td>T<\/td><td>F<\/td><td>T<\/td><td>F<\/td><\/tr><tr><td>F<\/td><td>F<\/td><td>F<\/td><td>F<\/td><td>T<\/td><td>T<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-logical-and-operator-in-python\">What is the Logical AND operator in Python?<\/h2>\n\n\n\n<p>The Logical and AND operator will return True if both the operands are True otherwise, it will give the result as False.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to demonstrate \n# logical and operator \na = 10\nb = 10\nc = -10\nif a &gt; 0 and b &gt; 0: \n    print(\"The numbers are greater than 0\") \nif a &gt; 0 and b &gt; 0 and c &gt; 0: \n    print(\"The numbers are greater than 0\") \nelse: \n    print(\"Atleast one number is not greater than 0\")<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The numbers are greater than 0\nAtleast one number is not greater than 0<\/code><\/pre>\n\n\n\n<p>Example 2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to demonstrate \n# logical and operator \na = 10\nb = 12\nc = 0\nif a and b and c: \n    print(\"All the numbers have boolean value as True\") \nelse: \n    print(\"Atleast one number has boolean value as False\")<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Atleast one number has boolean value as False\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-logical-or-operator-in-python\">What is the Logical OR operator in Python?<\/h2>\n\n\n\n<p>The logical <a href=\"https:\/\/www.skillvertex.com\/blog\/operators-in-c-set-2-relational-and-logical-operators\/\" data-type=\"post\" data-id=\"2202\">OR operator<\/a> will return a True value if the operands come true.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to demonstrate \n# logical or operator \na = 10\nb = -10\nc = 0\nif a &gt; 0 or b &gt; 0: \n    print(\"Either of the number is greater than 0\") \nelse: \n    print(\"No number is greater than 0\") \nif b &gt; 0 or c &gt; 0: \n    print(\"Either of the number is greater than 0\") \nelse: \n    print(\"No number is greater than 0\")<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Either of the number is greater than 0\nNo number is greater than 0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-logical-not-operator-in-python\">What is the Logical NOT operator in Python?<\/h2>\n\n\n\n<p>The logical not operator will <a href=\"https:\/\/www.skillvertex.com\/blog\/variadic-functions-in-c\/\" data-type=\"post\" data-id=\"2561\">function<\/a> with a single<a href=\"https:\/\/www.skillvertex.com\/blog\/bool-in-c\/\" data-type=\"post\" data-id=\"1813\"> boolean value<\/a>.  Hence, if the boolean value will come True then it will return as False and Vice versa.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to demonstrate \n# logical not operator \na = 10\n \nif not a: \n    print(\"Boolean value of a is True\") \nif not (a%3 == 0 or a%5 == 0): \n    print(\"10 is not divisible by either 3 or 5\") \nelse: \n    print(\"10 is divisible by either 3 or 5\")<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10 is divisible by either 3 or 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-order-of-precedence-of-logical-operators\">What is Order of Precedence of Logical Operators?<\/h2>\n\n\n\n<p>Python will calculate the <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-is-a-legal-expression-in-sql\/\" data-type=\"post\" data-id=\"1856\">expression<\/a> from left to right in multiple operators.<\/p>\n\n\n\n<p> Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to demonstrate \n# order of evaluation of logical \n# operators \ndef order(x): \n    print(\"Method called for value:\", x) \n    return True if x &gt; 0 else False  \na = order \nb = order \nc = order \nif a(-1) or b(5) or c(10): \n    print(\"Atleast one of the number is positive\")<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Method called for value: -1\nMethod called for value: 5\nAtleast one of the number is positive<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, these Python logical operators provide a straightforward way to make decisions in your code. &#8220;AND&#8221; ensures both conditions must be true for the overall expression to be true, while &#8220;or&#8221; requires at least one condition to be true. <\/p>\n\n\n\n<p>On the other hand, &#8220;not&#8221; negates the truth value of a condition.  Several examples are illustrated in this for more clarity on  Logical And, Logical, Logical Not operators.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-logical-operators-fa-qs\">Python Logical Operators- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1707394469022\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are logic operators?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Logical operators work to compare the logical expressions that show results either as true or false.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707394475398\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.  What is called a logical AND operator?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. This AND logical operator will check if both the operands are true.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707394492320\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the syntax of logical operators?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.Logical OR operator: ||<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The logical operators are used to combine the conditional statements. Let us check out this to learn more about the Python logical operators in Python. What is the Logical Operators in Python? In Python, Logical operators will function on the conditional statements. They will work on Logical AND, Logical OR, and Logical NOT operations. What &#8230; <a title=\"Python Logical Operators\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-logical-operators\/\" aria-label=\"More on Python Logical Operators\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":6983,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[893,72,57],"class_list":["post-6978","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-logical-operator","tag-programming","tag-python","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=6978"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6978\/revisions"}],"predecessor-version":[{"id":8902,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6978\/revisions\/8902"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/6983"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=6978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=6978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=6978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}