Operators in Python Programming language 2
We studies what are the Arithmetic operators, Comparisons
Operators that used in the Python Programming in last blog session Artcafe.
Today we are going to learn what the Logical Operators in Python are.
Logical Operators
If you have knowledge of Logic gates, you can easily
understand this Logical Operators. Logical Operators are an important type of
Operators. There are 3 main logical operators.
- AND
- OR
- NOT
These logical operators are working like as the Logic gates.
So let’s understand about these operators.
AND Operator
I will start with an example for this operator, When you log
into Gmail, Facebook, Twitter.. etc, you have to input your user name and
password. If the username and password are both correct only, you can login to
that accounts. If one input is incorrect, you can’t login there successfully.
This AND Operator also same as that situation. Both conditions should be true
to get the output as true. Please refer below table to get understand.
Username
|
Password
|
Login
|
Incorrect
|
Incorrect
|
False
|
Incorrect
|
Correct
|
False
|
Correct
|
Incorrect
|
False
|
Correct
|
Correct
|
True
|
By connecting the two conditions using the AND, the both
conditions should be true for get output as “TRUE”
I hope, now you have a basic idea about AND Operator, Let’s
see how to do that in Python Programming Mathematical statements.
First Statement
|
First Statement result
|
Second Statement
|
First Statement result
|
Using AND Operator
|
AND Operator result
|
(2>3)
|
false
|
(5>7)
|
False
|
(2>3) and (5>7)
|
False
|
(2>3)
|
False
|
(7>5)
|
True
|
(2>3) and (7>5)
|
False
|
(3>2)
|
True
|
(5>7)
|
False
|
(3>2) and (5>7)
|
False
|
(3>2)
|
True
|
(7>5)
|
True
|
(3>2) and (3>2)
|
True
|
Let’s create a simple python program to test this AND
Operator. Open the Python shell and Code the following codes using Script mode.
Then run the script. If you don’t know
how to run a python script using Script mode, please visit here.
When you run that program it will give outputs like below.
So I hope you got a basic idea about AND Operators in Python.
OR Operator
We take an example to understand this OR operator. We are
stay on a room that have two doors. If that both two doors are open, we can go
out by those two doors. If one door is open, we can go out by one door. But
both doors are closed, we can’t go out. That process is happening in the OR
Operator. Please refer below table to get understand.
First door
|
Second door
|
Go out
|
Closed
|
Closed
|
False
|
Closed
|
Open
|
True
|
Open
|
Closed
|
True
|
Open
|
Open
|
True
|
By connecting the two conditions using the OR, at least one
statement should be TRUE for get output as “TRUE”
I hope, now you have a basic idea about OR Operator, Let’s
see how to do that in Python Programming Mathematical statements.
First Statement
|
First Statement result
|
Second Statement
|
First Statement result
|
Using OR Operator
|
OR Operator result
|
(2>3)
|
false
|
(5>7)
|
False
|
(2>3) or (5>7)
|
False
|
(2>3)
|
False
|
(7>5)
|
True
|
(2>3) or (7>5)
|
True
|
(3>2)
|
True
|
(5>7)
|
False
|
(3>2) or (5>7)
|
True
|
(3>2)
|
True
|
(7>5)
|
True
|
(3>2) or (3>2)
|
True
|
Let’s create a simple python program to test this OR
Operator. Open the Python shell and Code the following codes using Script mode.
Then run the script. If you don’t know
how to run a python script using Script mode, please visit here.
When you run that program it will give outputs like below.
So I hope you got a basic idea about OR Operators in Python.
NOT Operator
The Not operator always provide the opposite value. If we
input 1 the Not Operator provide 0 as the output. If we input 0, the Not
operator provide 1 as the output. This is easy to understand. Please refer
following tables.
Statement
|
not(Statement)
|
True(1)
|
False(0)
|
False(0)
|
True(1)
|
Statement
|
not(Statement)
|
(6>5) = True
|
not(6>5) = False
|
(5>6) = False
|
not(5>6) = True
|
Let’s create a simple python program to test this OR Operator. Open the Python shell and Code the following codes using Script mode. Then run the script.
When you run that program it will give outputs like below.
So I hope you got a basic idea about OR Operators in Python.
I hope you learn a lot about the AND, OR and NOT Operators
in Python Programming. If you
have any problem or error about the lesson or executing commands please write
them on a comment box. I will help you to resolve that problems. As well as if
you have any suggestions or complain please feel free to comment. Thank you. See you in next python lesson.
Excellent post. You have shared some wonderful tips. I completely agree with you that it is important for any blogger to help their visitors. Once your visitors find value in your content, they will come back for more How to Run Python Program In Script Mode
ReplyDelete