AppDividend
Latest Code Tutorials

Python Logical Operators AND OR NOT Tutorial With Example

0

Python Logical Operators And Or Not Tutorial With Example is today’s topic. A logical operator takes one or more boolean arguments and operates on them and gives the result. In Python, the primary logical operators are And, Or, and Not. A boolean expression or valid expression evaluates to one of two states True or False. Python provides the boolean type that can be either set to False or True. Many functions and operations return boolean objects.

Python Logical Operators

Operators are used to performing operations on values and variables. Operators can manipulate individual items and returns a result. Let’s see one by one logical operator.

Logical AND Operator in Python

Let’s see the following example. Write the following code inside the app.py file.

# app.py

isSunday = True
isHoliday = True

if isHoliday and isSunday:
    print('Sunday is a Funday!!')
else:
    print('Not holiday bro!! Please start working :(')

Above code has two variables and both are boolean, and both have values True.

The step is we have taken the if statement and check the condition that, if both have True values then we will print the Sunday is Funday statement. Otherwise, it will execute else block.

Go to the terminal and run the above file and see the output.

 

Python Logical Operators And Or Not Tutorial With Example

Now, let’s change the isHoliday value to False and again run the above code.

# app.py

isSunday = True
isHoliday = False

if isHoliday and isSunday:
    print('Sunday is a Funday!!')
else:
    print('Not holiday bro!! Please start working :(')

Now, run the code and see the output.

 

Logical And Operator in Python

See now, else block is executed because one of the conditions of if statement is False.

That means if we are using And operator then each condition must be true otherwise it will take as a False.

If we connect the two boolean values with the And operator then it both values must hold the True otherwise it will give us the False result.

Logical OR Operator in Python

In the above example, replace the And operator with the Or operator and see the output.

# app.py

isSunday = True
isHoliday = False

if isHoliday or isSunday:
    print('Sunday is a Funday!!')
else:
    print('Not holiday bro!! Please start working :(')

In the above code, we have taken isSunday = True and isHoliday = False. 

In the if statement, we have used the OR operator. That means if one of the variable’s value is True, the statement holds, and we will see the output Sunday is Funday!! See the output below.

 

Logical OR Operator in Python

So, if one of the variable’s value is True, the whole condition becomes True, and we will see the output accordingly.

Logical NOT Operator in Python

Inside the if statement, NOT operator is kind of tricky business. It is beneficial when we need to apply the adverse condition to get a positive output. Let’s see the following example.

# app.py

isSunday = True
isHoliday = False

if not isHoliday:
    print('Please start working, it is not holiday')
else:
    print('Today is holiday pal!!')

In the above code,  we have defined isSunaday = True and isHoliday = False. 

Now, we are checking the condition inside the if statement that if isHoliday is False then if statement becomes True and it will execute the if block. Otherwise, it will execute the else block.

We have already defined that isHoliday = False and also we are checking the isHoliday variable to be False. That is why it becomes True for if condition and it will execute the code. 

The output of the above code is following.

 

Logical NOT Operator in Python

The simple table for the main Python Logical Operators is following.

Operator Meaning Example
and True if both the operands are true x and y
or True if either of the operands is true x or y
not True if an operand is false (complements the operand) not x

 

Finally, Python Logical Operators AND OR NOT Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.