Boolean Logic with Boolean and Comparisons – Free Python Tutorial

Boolean Logic, As we all know, there is a type of variables available in Python, which is of Boolean type. A boolean type variable can have only two possible values, ‘True’ and ‘False’. Note that, as Python is a case-sensitive language so if we assign the values as ‘true’ and ‘false’ to a boolean type variable, then we will get an error.

Boolean logic is used to make more complicated conditions for if, elif statements, that rely on more than one conditions. We use Boolean Operators in Python to setup boolean logic. There are three boolean operators in Python –> ‘and’, ‘or’, and ‘not’. These boolean operators take two arguments, evaluates them (evaluation will be done as per the boolean operator is defined) and returns a boolean value (i.e. either ‘True’ or ‘False’). This boolean value is considered by the conditional statements (i.e. either ‘if’ or ‘elif’ statements) and the process is completed accordingly. If the value returned by the boolean operator is True then the conditional statement will be executed, otherwise not.

Let’s take some example of boolean logic using boolean operators:

# and operator

>>>True and True
True

>>>True and False
False

>>>False and False
False

# or operator

>>>True or True
True

>>>True or False
True

>>>False or False
False

# not operator

>>> not True
False

>>>not False
True

Now, there is a point we need to keep in mind. The point is, every non-zero value in Python is considered as ‘True’. If a variable contains any non-zero value, then that variable will also be considered as a ‘True’ value if we pass that variable in any boolean operator.


Let’s talk about an important topic of Boolean operators, which is Booleans & Comparisons.

Booleans & Comparisons in Python

The true boolean values (True and False) can be created by comparing values. For every comparison operation, the result will always be a boolean value (i.e. either True or False). For comparison, we use various operators like,

  • the comparison operator (==),
  • the less-than operator (<),
  • the less-than-equals-to operator (<= ),
  • the greater-than operator (>),
  • the greater-than-equals-to operator (>=), and
  • the not-equals-to operator (!=).

As example:

# comparison operator
#____________________

>>>2==3
False

>>>2==2
True

>>>"Rajesh"=="Rajesh"
True

>>>"Rajesh"=="Padhle"
False
# less-than operator
#___________________

>>>2<3
True

>>>3<2
False

>>> "Rajesh"<"Padhle"
False

>>> "Mango"<"Padhle"
True
# less-than-equals-to operator
#_____________________________

>>>2<=3
True

>>>3<=2
False

>>> "Mango"<="Padhle"
True

>>> "Rajesh"<="Padhle"
False
# greater than operator
#______________________

>>> 2>2.0
False

>>> 3>2.0
True

>>> "Nimbus">"Lambda"
True

>>> "XYZ">"ZXY"
False
# greater-than-equals-to operator
#________________________________

>>>2>=2.0
True

>>>3>=4.0
False

>>> "Doggy">="Eat"
False

>>> "Eagle">="Dog"
True
# not-equals operator
#_____________

>>>2!=3
True

>>>3!=3
False

>>>"Rajesh"!="Rajesh"
False

>>>"Rajesh"!="Padhle"
True

In Python, Strings are compared lexicographically using the numeric equivalents (the result of the built-in function ord() of their characters). Unicode and 8-bit strings are fully interoperable in this behavior.


That was all about booleans and comparisons. I hope you like this tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *