Go to the Previous Python Tutorial

Read Previous Tutorial

In this tutorial, we will learn about Boolean Datatypes, Logical and Relational Operators. These operators play a very keen role, in building the logic of a program, and  I can say, without these nothing would be possible in Programming.

 

Boolean Datatype

 

Well, we haven’t discussed about this datatype in the first lesson. However, we will take a look at these datatypes in this lesson. Boolean datatypes are very easy to understand. In simple words, boolean variables can hold either ‘True‘ or ‘False’. They can’t store numbers, or characters or set of characters. Booleans are meant to store only either ‘True’ or ‘False’. For example, 

a = True #Now the variable ‘a’ is a boolean, since it has a value True
print(a) #Now, the value of ‘a’ which is True is printed.

 

One important note is, when you are writing True or False in your python script, make sure that the first letter ‘T’ in True or ‘F’ in False is always capital.

 

Logical Operators in Python

 

Logical Operators in python are linked with boolean datatypes, because logical operators can only be performed between boolean values or variables, and also, the result obtained after performing logical operators is going to be a boolean datatype.

The Logical Operators in Python are listed below,

• and

• or

• not

 

Let us now, take a look at each of the logical operators listed above.

 

1. and operator

 

When and operator is performed between two booleans, the result is going to be True only if both the booleans are True. In all the other cases, the result is going to be False. 

For example, assume that there are two boolean varibles and b. If we now perform a logical and operator between and b, let us look at all the possible results.

a = True  #Declaring a as True
b = False  #Declaring b as False
c = a and b #Performing logical and between a and b, and storing the result in c
print(c) #The value of c is going to be printed. The value of c will be False since b=False

 

a = False  #The value of a is False
b = True  #The value of b is True
c = a and b 
#Performing and between a and b.
print(c)  #The value of c is going to be False, since a=False

 

a = False  #The value of a is False
b = False  #The value of b is False
c = a and b  #Performing and between a and b.
print(c)  #The value of c is going to be False, since both a and b are False

 

a = True  #The value of a is True
b = True  #The value of b is True
c = a and b  #Performing and between a and b.
print(c)  #The value of c is going to be True, since both a and b are True
#Performing logical and between a and b, and storing the result ina

 

2. or operator

 

When or operator is performed between two booleans, the result is going to be False only if both the booleans are False. In all the other cases, the result is going to be True. 

Once again, lets take a look at the possible outcomes by applying the or operator between two booleans.

a = True  #Declaring a as True
b = False  #Declaring b as False
c = a or b #Performing logical or between a and b, and storing the result in c
print(c) #The value of c is going to be printed. The value of c will be True since a=True

 

a = False  #The value of a is False
b = True  #The value of b is True
c = a or b 
#Performing or between a and b.
print(c)  #The value of c is going to be True, since b=True

 

a = False  #The value of a is False
b = False  #The value of b is False
c = a or b  #Performing or between a and b.
print(c)  #The value of c is going to be False, since both a and b are False

 

a = True  #The value of a is True
b = True  #The value of b is True
c = a or b  #Performing or between a and b.
print(c)  #The value of c is going to be True, since both a and b are True
#Performing logical and between a and b, and storing the result ina

 

3. not operator

 

not operator is used to reverse the value of a boolean. For example, if the value of is True, and if we perform a not operator on the variable a, then the value of will be reversed to False.

print(not(True))  #not is performed on True, so the result is going to be false, and it is printed

print(not(True and False))  #not is performed between (True and False). So the result will be not(False) which is True
#Performing logical and between a and b, and storing the result ina

Relational Operators in Python

 

Relational operators are used to check the relation between two values. They are also linked with booleans, because, the result obtained after performing relational operations is always a boolean.

The Relational Operators in Python are listed below (separated by comas)
> , < , >= , <= , == , !=

• The operator is used to check if one value is greater than the other (just like in Maths)

• The operator is used to check if one value is lesser than the other (just like in Maths)

• The >= operator is used to check if one value is greater than or equal to the other value

• The <= operator is used to check if one value is lesser than or equal to the other value

• The == operator is used to check if one value is equal to the other value

• The != operator is used to check if one value is not equal to the other

a = 2  #a is assigned the value 2
b = 3  #b is assigned the value 3
c = (a>b)  # relational operation (a>b) is done, and the result is stored in c
print(c)  # c is printed. Value of c will be False, since a>b i.e., 2>3 is False.

print(3>=2) #True is going to be printed, because 3>=2 is True

print(5==5) #True is printed because 5 is equal to 5

print(5!=5) # False is printed because 5 is equal to 5, and we are checking for inequality between them

#Performing logical and between a and b, and storing the result i

Linking Logical and Relational Operators in Python

We can link Logical and Relational Operations in Python, as per the demand of our logic. And the result obtained when we link relation and logical operators, is once again going to be a boolean , that is, either True or False.

Let us look at some of the examples,

a = 2  #a is assigned the value 2
b = 3  #b is assigned the value 3
d = 5
e = 3
c = (a>b) and (d>e)  #Performing relational operation between d and e, and a and b individually, and then performing a logical and operation between them
print(c)  # c is printed. Value of c will be False, because 2>3 is False and 5>3 is True, so (False and True) is going to be False

print(not(3>=2)) #False is going to be printed, because 3>=2 is True, and not(True) is False

print((5==5) and (2==2)) #True is printed, because both sides of the logical and are True

#Performing logical and between a and b, and storing the result i

Share This

Share This

Share this post with your friends!

Subscribe To Our Newsletter and Never Miss an Update!

Subscribe To Our Newsletter and Never Miss an Update!

Leave us your Email ID and we will notify you whenever we release new Videos, Articles or Apps.

You have Successfully Subscribed!