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 Logical Operators:
In this post, we are going to discuss Logical Operator 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.
·
Logical Operators
·
How Logical Operators Work With Boolean Types?
·
How Logical Operators Work With Non-Boolean Types?
Logical operators
are used to combining two or more conditions and perform
logical operations using Logical and, Logical or and
Logical not.
Operator |
Meaning |
and
|
Greater Than |
or
|
Less Than |
not
|
Greater Than Equal To |
The Example of Basic Relational Operators :
a = 40
b = 20
c = 50
print(a>b and a>c)
The Output:
False
Another Example:
a = 40
b = 20
c = 50
print(a>b and c>a)
The Output:
True
Behavior of Logical Operators With Non Boolean:
Python
allows us to apply logical operators with
non-Boolean types also
But before we understand how these operators work with
non-Boolean types, we must understand some very important
points.
1.
None, 0 , 0.0 ,”” are all False values.
2.
The return value of Logical and & Logical or operators is never
True or False when they are applied to
non-Boolean types.
3.
If the first value is False, then
Logical and returns first value, otherwise it returns the
second value.
4.
If the first value is True, then Logical or returns
first value, otherwise it returns the second value.
5.
When we use not-operator on non-Boolean types, it returns
True if it’s operand is False( in any form) and
False if it’s operand is True ( in any form).
Example:
5 and 6
Output:
6
Example:
5 and 0
Output:
0
Example:
0 and 10
Output:
0
Example:
6 and 0
Output:
0
Example:
10/0 and 0
Output:
ZeroDivisionEror
Example:
not ‘Digital’
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.