Types of Operators in Python
Python provides us 7 popular types of operators:
·
Arithmetic Operators
·
Relational or Comparison Operators
·
Logical Operators
·
Assignment Operator
· Compound Operators
·
Identity Operators
·
Membership Operators
Today we, discuss Relational or Comparison Operators:
In this post, we are going to discuss Relational or Comparison Operators in Python but before these, if You are not seen our video on this topic please see it first, then you will understand the concepts very well.
•
Relational Operators
•
Relational Operators with Strings
•
Chaining of Relational Operators
•
Special Behavior Of == and !=
Relational operators
are used to compare values.
They either
return True or False according to the
condition.
These operators are:
Operator |
Meaning |
>
|
Greater Than |
<
|
Less Than |
>=
|
Greater Than Equal To |
<=
|
Less Than Equal To |
==
|
Equal To |
!= |
Not Equal To |
The Example of Basic Relational Operators :
a=10
b=4
print("a=",a,"b=",b)
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)
print("a<=b",a<=b)
The Output :
Relational Operators With Strings
Relational Operators
can also work with strings .
When applied on string operands , they compare the Unicode of
corresponding characters and return True or False based on
that comparison.
As discussed, previously , this type of comparison is called
lexicographical comparsion.
Example of Relational Operators With Strings :
a="Ramesh"
b="Rajesh"
print("a=",a,"b=",b)
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)
print("a<=b",a<=b)
The Output :
If we want to check the UNICODE value for a particular letter
, then we can call the function ord().
It is a built in function which accepts only one character as
argument and it returns the UNICODE number of the argument
passed
Example:
ord(‘A’)
65
ord(‘m’)
109
ord(‘j’)
106
Example :
a= "India"
b= "india"
print("a=",a,"b=",b)
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)
print("a<=b",a<=b)
The Output :
Special Behavior of Relational Operators:
Python
allows us to chain multiple relational operators in one
single statement.
For example, the expression 1<2<3 is perfectly valid in
Python
However, when Python evaluates the expression , it returns
True if all individual conditions are true , otherwise it
returns False
Example:
print(7>6>5)
Output:
True
Example:
print(5<6>7)
Output:
False
Special Behavior Of == And !=
==
compares it’s operands for equality and if they are of
compatible types and have same value then it returns
True otherwise it returns False
Similarly != compares it’s operands for
inequality and if they are of incompatible types or
have different value then it returns True otherwise it returns
False
Example:
print(10==10)
Output:
True
Example:
print(15==15.01)
Output:
False
Example:
print(0 != False)
Output:
False
And if you have any doubt regarding this topic then , you can ask your
doubt in the comment section , I will respond you as soon as possible
.
DIGITAL COMMUNITY
We Make IT Happen.
No comments:
Post a Comment
If you have any doubts, let me know.