# Python boolean operators with examples [improvement needed]

Python Methods and Functions

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 & gt;  0 and b & gt;  0 : print ( "The numbers are greater than 0" )   if a & gt;  0 and b & gt;  0 and c & gt;  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 & gt; ` ` 0 ` ` or ` ` b & gt; ` ` 0 ` `: ` ` print ` ` (` ` "Either of the number is greater than 0" ` `) ` ` else ` `: ` ` print ` ` (` ` "No number is greater than 0 "` `) ` ` `  ` if ` ` b & gt; ` ` 0 ` ` or ` ` c & gt; ` ` 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 Tutorials (adsbygoogle = window.adsbygoogle || []).push({}); Python OpenCV | cv2.putText () method numpy.arctan2 () in Python Python | os.path.realpath () method Python OpenCV | cv2.circle () method Python OpenCV | cv2.cvtColor () method Python - Move item to the end of the list time.perf_counter () function in Python Check if one list is a subset of another in Python Python | os.path.join () method Python | os.path.expanduser () method Replace negative value with zero in Numpy array Python | Convert nested dictionary list to Pandas dataframe TimeField — Django Models Python | os.path.isdir () method Python OpenCV cv2.line () method numpy.poly1d () in Python Numpy.sqrt () in Python Split nested list into two lists in Python Python program to convert camel case string to snake case Python | Pandas DataFrame.reset_index () Binning in Data Mining Python | Add suffix / prefix to strings in a list Dunn index Python Python - Print list vertically Books for developers Python Data Science Handbook 22/08/2021 CEH v11 Certified Ethical Hacker Study Guide 12/08/2021 Data Science from Scratch. First Principles with Python 23/09/2020 Python Workout: 50 TEN-MINUTE EXERCISES 23/09/2020 © 2022 Python.Engineering Best Python tutorials books for beginners and professionals 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 in Italiano Python auf Deutsch Python en Français Python en Español Türk dilinde Python Python: мануалы на русском ITIL v4 Computations Development Cryptography For dummies Machine Learning Big Data Loops Counters NumPy NLP PHP Regular Expressions File Handling Arrays String Variables Knowledge Database X Submit new EBook # Python program for demonstration # order of logic evaluation # statements   def order (x): print ( "Method called for value:" , x) return True if x & gt;  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" ) (adsbygoogle = window.adsbygoogle || []).push({}); \$(document).ready(function () { \$(".modal_galery").owlCarousel({ items: 1, itemsCustom: false, itemsDesktop: [1300, 1], itemsDesktopSmall: [960, 1], itemsTablet: [768, 1], itemsTabletSmall: false, itemsMobile: [479, 1], singleItem: false, itemsScaleUp: false, pagination: false, navigation: true, rewindNav: true, autoPlay: true, stopOnHover: true, navigationText: [ "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/prevCopy.png'>", "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/nextCopy.png'>" ], }); \$(".tel_mask").mask("+9(999) 999-99-99"); }) ```