Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
operator
communitycreator

What are the logical operators "and", "or", and "not" in Python?

harris910

A logical operator is a symbol or word that connects two or more expressions so that the value of the produced expression created is solely determined by the value of the original expressions and the operator’s meaning.

The following are the logical operators available in Python:

  • and
  • or
  • not

Description

The table below explains each of the logical operators in Python.

Operator Symbol

Operator Name

Example

Description

and

Logical And

Operand-A and Operand-B

It returns True, when both the operand-A and operand-B is True, otherwise it returns False.

or

Logical Or

Operand-A or Operand-B

It returns True, when either the operand-A or operand-B is True, otherwise it returns False.

not

Logical Not

not Operand-A

It returns the operand's logical state in reverse.

Figure

The following figure is the visual representation of the logical operators in Python.

Visual representation of Logical Operators in Python

Code

The code below illustrates how to use these logical operators in Python.

A=10
B=20
C=30

#And Operator
#Both True
print "The value of (A<B and B<C): ",(A<B and B<C);
#First operand: True		Second Operand: False
print "The value of (A<B and B>C): ",(A<B and B>C);
#Both False
print "The value of (A>B and B>C): ",(A>B and B<C);

#Or Operator
#Both True
print "The value of (A<B or B<C): ",(A<B or B<C);
#First operand: True		Second Operand: False
print "The value of (A<B or B>C): ",(A<B or B>C);
#Both False
print "The value of (A>B or B>C): ",(A>B or B>C);

#Not operator
D=True
#reverse of true logical state
print "The value of not(D): ",not(D);

RELATED TAGS

python
operator
communitycreator
RELATED COURSES

View all Courses

Keep Exploring