Python Basic – Boolean logic and Operators Precedence in Python

Home /

Table of Contents

Boolean logic in Python

Boolean logic is used to create complex conditions for the if statement. That is, if an if statement depends on more than one condition, then we can use Boolean logic. As mentioned earlier, Python has three types of boolean operators: and, or, and not.

Boolean Logic: and

This operator verifies two arguments and is true when both arguments are true.

   >>> 8 == 8 and 3 == 3
   >>> 3 == 8 and 3 == 8

Here 8 == 8 is true and 3 == 3 is true. So and the output of the operator is True. One thing to note is that in other programming languages such AND, OR or NOT operators are usually referred to as &&, ||,! Is expressed with where Python is written in word form.

Boolean Logic: or

Like the and operator mentioned above, or has two arguments but it is true if any one of the two arguments is true. That is, in the following statement,

   >>> 8 == 8 or 3 == 3
   >>> 3 == 8 or 3 == 8
   >>> 3 == 8 or 3 == 3

Here the logic on the left side of or is true but not on the right side. Even then the output of or is True.

Boolean Logic: not

Not like the other two operators do not work with two arguments. Rather, one argument is enough. It checks whether there is any logic. The following example clearly shows

   >>> not 3 == 3
   >>> not 3 == 8

We all know that 3 == 3 of the first statement. In front of this, I am trying to see that not 3 == 3. But this is not true, 3 and 3 are equal. And so the output of not 3 == 8 is coming False.

   P = 3
   Q = 3
   W = 8
   V = 10
   if P == Q and W < V:
       print("EnableGeek is awesome!")

Output  :

EnableGeek is awesome!

Operator Precedence in Python

In general mathematics, such as the addition and distribution of numbers before addition or subtraction, there is also a progressive rule for this operator. The other rule is that a statement set operator meeting will take place. This changes with the rules of simplification of mathematics elsewhere – the work of BRAC, then dot/exponent, then multiplication and division, and division addition and subtraction.

Apart from addition, subtraction, multiplication, and division, there are some other operators in plating, so it is necessary to keep division at the forefront. Such as the spread of contradictory statements,

   >>> False == False or True
   >>> False == (False or True)

In the first statement above, the priority of == is greater than or equal to. And in the following statement or operation, it is preferred because it is located in a bracket. These operator precedences are dependent on boolean logic

Table: Preference for different operators (top to bottom – more or less)

* / % //
>> <<
<= < > >=
< > == !=
= %= /= //= -= += *= **=
is is not
in not in

Boolean logic is a fundamental concept in computer science and programming, and it plays a crucial role in Python programming as well. In Python, Boolean values are used to represent logical truths and falsehoods, with the two possible values being True and False. Boolean logic is important in programming because it allows us to make decisions based on conditions, which is a crucial part of writing efficient and effective code.

In Python, Boolean logic is often used in control structures such as if-else statements and while loops, where we need to test whether a particular condition is true or false. We can also use Boolean operators such as and, or, and not to combine multiple conditions and create more complex logical expressions.

Furthermore, many built-in functions and methods in Python return Boolean values, which can be used to perform further operations based on the result. For example, the in operator returns True if a value is present in a sequence or container, and False otherwise. Similarly, the isinstance() function returns True if an object is an instance of a specified class, and False otherwise.

Overall, Boolean logic is a crucial part of programming in Python, and mastering it is essential for writing clear, concise, and efficient code.

Share The Blog With Your Friends
Other Recommended Article