# Python Logical Operators

10 total views

A logical operator is the one that compares one or more values and return Boolean true or false. Python has a set of logical operators that are used in conditionals and loops. The python programming language supports following logical operators:

1. Relational operators
2. Set membership operators
3. Boolean operators

Contents

### Relational Operators

Lets us list all the logical operators.

The above is the list of python relational operators. A relational operator in python compares both numeric values and non-numeric values and return True or False. Let us see few examples.

In the following examples, we will check equality of two numbers and print the results.

# Checking equality of two numbers
print(10 == 10)
print(23 == 12)
print(45 != 56)
# Checking equality of two variable containing numbers
num1 = 200
num2 = 100
print(num1 == num2)

The program above will print following results.

= RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Numeric-Equalilty.py
True
False
True
False
>>> 

The value of variable num1 does not match with num2, therefore, the output is False. The same is tested for other equality. In python, you can also check the equality of a string which is a non-numeric value.

In the example below, we will check the equality of two strings.

str1 = "IT Employee"
str2 = "IT Employee"
str3 = "It Employee"
print(str1 == str2)
print(str2 == str3)

The python code above compares each character of both strings at corresponding location. It is going to be True, if

• The length of the strings match
• Each of the characters match at each position starting with 0.

Output of the above code is given below.

= RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Non-Numeric_Quality.py
True
False
>>> 

In the case, print(str1 == str2 ), both string length and characters are equal, therefore, the program returns True. In the second case, print(str2 == str3) is different by one character, therefore, program returns False. Let us now check the relational operator for greater than or less that another number.

# Check if number is grater than another number
number1 = 35
number2 = 44
print(number1 < number2)
# Check if string is greater than another string
str1 = "Hello"
str2 = "hello"
print(str1 > str2)

In the above example, print(number1 < number2) is true because 35 < 44.But, print(str1 > str2) is false. You need to understand why str1 > str2 is false. The different between str1 and str2 is letter “H” and “h” respectively. In python, letters are in following sequence.

A B C D E F G H ... a b c d e.. 

Since, “H” comes after “h”, therefore, “h” is greater than “H”. Also, consider another capital after “H” such as “J”. The letter “J” is also greater than “H” because it comes after “H”.

### Set Membership Operators

The set membership operators check if a number belong to a particular set. If it belongs then return true, else return false. Here is the list of set membership operators.

In the example, we will check a list of fruits for ‘apple’ and output our result.

my_list = ['oranges','grapes','apple','banana']
print('apple' in my_list)
print('mango' not in my_list)

The program checks for ‘apple’ in the list and also, checks if ‘mango’ is not in the list. The output of the program is as follows.

= RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Set_Membership_operators.py
True
True
>>> 

The output is true for both the statement involving set operators.

### Boolean Operators

The Boolean operators are for complex conditions, when there are multiple conditions that need to be checked before a block of code executes in python. Here is a list of Boolean operators.

Logical AND – compares two Boolean values and if both of them are true then return true, otherwise, return false. Here is an example.

exp = 3 > 2
exp2 = 5 > 3
if exp and exp2:
print("condition is true")
else:
print("condition is false")

The above program checks both exp and exp2. If both are true, only then it prints “condition is true”. Otherwise, it will prints “condition is false“. The output is given below.

>>>
== RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/Logical AND.py =
condition is true
>>> 

Logical OR – compares two Boolean values and if any one of them is true, it returns a true. Otherwise, false.

exp = 3 > 7
exp2 = 5 > 3
if exp or exp2:
print("condition is true")
else:
print("condition is false")

The exp is false, but the exp2 is true which is enough to make the condition true and it prints “condition is true” as output. The output is given below.

== RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/logical OR.py ==
condition is true
>>> 

Truth table for the logical OR is given below.

### Logical NOT

This operator is also known as negation operator. it will negate any true statement into false and false statement into true. Consider the following example.

# The my_exp is false at the beginning
my_exp = 233 > 555
# Convert my_exp into true
if not(my_exp):
print("This statement is true now!")
print("Done execution")

The if block will never execute unless not operator convert the condition into true. The output is given below.

== RESTART: C:/PROGRAMMING/PythonExercises/Relational Operators/logical NOT.py =
This statement is true now!
Done execution
>>> 

Let us look at the truth table for logical NOT.

Note: since, there is only one variable, the number of row is 21 = 2 , if variable is 2, then row = 22 = 4.

Bestseller

Python Programming: Using Problem Solving Approach

Python Programming is designed as a textbook to fulfil the requirements of the first-level course in Python programming. It is suited for undergraduate degree students of computer science engineering, information technology as well as computer applications. This book will enable students to apply the Python programming concepts in solving real-world problems. The book begins with an introduction to computers, problem solving approaches, programming languages, object oriented programming and Python programming. Separate chapters dealing with the important constructs of Python language such as control statements, functions, strings, files, data structures, classes and objects, inheritance, operator overloading and exceptions are provided in the book. Each chapter ends with objective-type questions, review questions, programming and debugging exercises to facilitate revision and practice of concepts learnt.