Monday, 1 July 2019

Relational Operator

Relational Operators:

a=10
b=20
print("a>b",a>b)
print("a>=b",a>=b)
print("a<b",a<b)
print("a<=b",a<=b)
print("a==b",a==b)

"""output
a>b False
a>=b False
a<b True
a<=b True
a==b False
>>> """

Definition:
Relational Operators compares two operands and returns true or false (1 or 0).
We can understand whether two values are same or bigger or smaller, using these operators.

print(True>True)#False
print(True>=True)#True
print(1>True)#False
print(1>=True)#True
print(0==False)#True

"""output

True>Trueà1>1àFalse
True>=Trueà1>=1-->True
10>True->10>1-->Talse
1>=True:1>=1-->True
0==False-->0==0-->True"""



a=10
b=20


Operator
Meaning
Example
Result
> 
Greater than: If the value of left operand is greater than the value of right operand, it gives True or false
a>b
False
>=
Greater than or equal operator: If the value of left operand is greater or equal than that of right operand, it gives True or False
a>=b
False
< 
Less than operator. If the value of left operand is less than the value of right operand, it gives True or false
a<b
True
<=
Less than or equal operator: If the value of left operand is lesser or equal than that of right operand, it gives True or false
a<=b
True
==
Equal operator: if the value of left operand is equal to the value of right operand, it gives True or False
a==b or b==a
False
!=
Not equal to operator: if the value of the left operand is not equal to the value of right operand, it returns True or false
a!=b
True

Strings comparision by using Relational Operators:

a="ravi"
b="Ravi"
print(a>b)
print(a>=b)
print(a==b)
print(a!=b)

"""output
True
True
False
True
""""
Python string comparison is performed using the characters in both strings. The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller.

Ex: 3
a=30
b=20
if(a>b):
    print("a is greater than b")
else:
    print("a is not greater than b")

"""output
a is greater than b

"""
You can observe the 3rd statement i.e if(a>b), this is called 'if' condition statement, this condition becomes 'True' i.e if(True) so "a is greater than b" is executed, 'else' part statement is not executed.

Chaining of Relational Operators:

The meaning of Chaining of relational operators means a single expression can hold more than one relational operator.

Example 1:
>>> a=10
>>> 5<a<20
True
>>> 

Here, 5 is less than 10 i.e 5<a so True, and then 10 is less than 20 i.e a<20 so True. Since both the conditions are True, Hence Result is True

Example 2:
>>> a=10
>>> 5>=a<20
False
>>> 

Here 5 is greater than or equal to 10 is False. But 10 is less than 20 is True. Since we get false and True, the result will be false.
5>=aà5>=10àFalse
a<20à10<20àTrue

False and True so the result is False

Example 3:
>>> a=10
>>> 10<a>20

False


>>> a=15
>>> 10<a>20
False

Here 10 is less than 15 is 'True'. But 15 is greater than 20 is 'False'.
Since we are getting True and False, The result will be False. So, the point is this, in the chain of relational operators, if we get all True, then only the final result will be True, if any comparison is 'False', then we get 'False' as the final result.
Steps:
10<a>20
à10<15 (True) and 15>20 (False)

àTrue and False àFalse

Related Video: https://youtu.be/N-rWkVn9Fqs

Next Topic: Logical Operators  https://youtu.be/SQxypOkKWyM
Previous TopicExample programs on Arithmetic Operators
Arithmetic Operators

You may like the following topics:


No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...