Change language

# Python boolean operators with examples [improvement needed]

Operators are used to perform operations on values ​​and variables. These are special characters that perform arithmetic and logical calculations. The value the operator operates on is known as operand .

Table of Content

## Boolean Operators

In Python, Boolean operators are used in conditional expressions (True or False). They perform logical AND , logical OR and logical NOT .

OPERATOR DESCRIPTION SYNTAX
and Logical AND: True if both the operands are true x and y
or Logical OR: True if either of the operands is true x or y
not Logical NOT: True if operand is false not x

#### Boolean AND operator

Boolean operator returns ` True ` if both operands are True, otherwise it returns ` False `.

Example # 1:

` `

` # Python program for demonstration # boolean and operator   a = 10 b = 10 c = - 10   if a"  0 and b"  0 : print ( "The numbers are greater than 0" )   if a"  0 and b"  0 and c"  0 : print ( "The numbers are greater than 0" ) else : print ( "Atleast one number is not greater than 0 " ) `

` `

Exit :

` The numbers are greater than 0 Atleast one number is not greater than 0 `

Example # 2 :

 ` # Python program for demonstration ` ` # boolean and operator ` ` `  ` a ` ` = ` ` 10 ` ` b ` ` = ` ` 12 ` ` c ` ` = ` ` 0 `   ` if ` ` a ` ` and ` ` b ` ` and ` ` c: ` ` print ` ` (` ` "All the numbers have boolean value as True" ` `) ` ` else ` `: ` ` print ` ` (` ` "Atleast one number has boolean value as False" ) `

Output:

` Atleast one number has boolean value as False `

Note. If the first expression evaluates to false when using the and operator, then subsequent expressions are not evaluated.

#### Logic or operator

Boolean or operator returns True if any of the operands — True.

Example # 1:

 ` # Python program for demonstration ` ` # boolean or operator `   ` a ` ` = ` ` 10 ` ` b ` ` = ` ` - ` ` 10 ` ` c ` ` = ` ` 0 `   ` if ` ` a" ` ` 0 ` ` or ` ` b" ` ` 0 ` `: ` ` print ` ` (` ` "Either of the number is greater than 0" ` `) ` ` else ` `: ` ` print ` ` (` ` "No number is greater than 0 "` `) ` ` `  ` if ` ` b" ` ` 0 ` ` or ` ` c" ` ` 0 ` `: ` ` print ` ` (` ` "Either of the number is greater than 0" ` `) ` ` else ` `: ` ` print ` ` (` ` "No number is greater than 0 "` `) `

Either of the number is greater than 0 No number is greater than 0

Example # 2 :

 ` # Python program for demonstration ` ` # boolean and operator ` `  `` a = 10 b = 12 c = 0   if a or b or c: print ( "Atleast one number has boolean value as True" ) else : print ( "All the numbers have boolean value as False" ) `

Exit:

` Atleast one number has boolean value as True `

Note. If the first expression evaluates to True during use or operator, then further expressions not evaluated.

#### Logical non-operator

Logical non-operator operation with a single boolean value. If boolean ` True ` returns ` False ` and vice versa.

Example :

` `

` # Python program for demonstration # logical non-operator   a = 10   if not a: print ( "Boolean value of a is True" )   if   not (a % 3 = = 0 or a % 5 = = 0 ): print ( "10 is not divisible by either 3 or 5" ) else : print ( " 10 is divisible by either 3 or 5 " ) `

` `

Exit:

10 is divisible by either 3 or 5

## Boolean operator evaluation order

For multiple operators, Python always evaluates the expression from left to right ... You can check this with the example below.

Example :

` `

``` Exit : Method called for value: -1 Method called for value: 5 Atleast one of the number is positive (adsbygoogle = window.adsbygoogle || []).push({}); Shop Best Python online courses for 2022\$FREE Best laptop for Excel\$ Best laptop for Solidworks\$399+ Best laptop for Roblox\$399+ Best computer for crypto mining\$499+ Best laptop for Sims 4\$ Best laptop for Zoom\$499 Best laptop for Minecraft\$590 (adsbygoogle = window.adsbygoogle || []).push({}); Latest questions PythonStackOverflow Common xlabel/ylabel for matplotlib subplots 1947 answers PythonStackOverflow Check if one list is a subset of another in Python 1173 answers PythonStackOverflow How to specify multiple return types using type-hints 1002 answers PythonStackOverflow Printing words vertically in Python 909 answers PythonStackOverflow Python Extract words from a given string 798 answers PythonStackOverflow Why do I get "Pickle - EOFError: Ran out of input" reading an empty file? 606 answers PythonStackOverflow Python os.path.join () method 384 answers PythonStackOverflow Flake8: Ignore specific warning for entire file 360 answers All questions (adsbygoogle = window.adsbygoogle || []).push({}); News 06/06/2023 Enhancing Signal Resilience: Exploring the World of Anti-Jamming Receivers 18/04/2023 Where to Learn Python: Resources for Beginners 15/04/2023 How to get time from rowversion T-SQL Wiki Python functions Python | How to copy data from one Excel sheet to another __main__ Python module Common xlabel/ylabel for matplotlib subplots ast Python module Check if one list is a subset of another in Python code Python module How to specify multiple return types using type-hints __main__ Python module Printing words vertically in Python code Python module Python Extract words from a given string Python functions Cyclic redundancy check in Python Python functions Finding mean, median, mode in Python without libraries ast Python module Python add suffix / add prefix to strings in a list ast Python module Why do I get "Pickle - EOFError: Ran out of input" reading an empty file? Python functions Python - Move item to the end of the list Python functions Python - Print list vertically © 2017—2023 Python Engineering Hub EN | ES | DE | FR | IT | RU | TR | PL | PT | JP | KR | CN | HI | NL Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com Python Loops Counters NumPy NLP Regular Expressions Wiki Tech news Python Wiki StackOverflow PHP JavaScript Books All books Computations Development Cryptography For dummies Big Data # Python program for demonstration # order of logic evaluation # statements   def order (x): print ( "Method called for value:" , x) return True if x"  0 else False   a = order b = order c = order   if a ( - 1 ) or b ( 5 ) or c ( 10 ): print ( "Atleast one of the number is positive" ) document.addEventListener("DOMContentLoaded", () => { let arrayCode = document.querySelectorAll('pre'); arrayCode.forEach(element => { element.classList.add("prettyprint"); }); }); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-Q022WLXW4X'); ```