Python Logical Operators

Logical operator in python

Logical Operators in Python.

To perform certain logical operations or to combine conditional statements, the logical operator is used. In control statements such as if, else, and elif, primarily logical operators are used where we can verify more conditions together by using these operators alone. Real or FALSE values are often returned by logical operators.

Logical Operators in Python

Logical operators are used to perform logical operations on Boolean values (True or False) or expressions. These operators allow you to combine and manipulate Boolean values to make decisions in your programs. All  logical operators in python are discussed below  in detail with their syntax and proper example:

1. And

It compares whether both the operands are logically True or not. If the values are found True then it returns TRUE else it returns FALSE.

Syntax

Logic1 and Logic2

Example

>>> a = 10
>>> b = 20
>>> a>2 and b<25
True
>>> 10 == 10 and 10<11
False
>>> a != 10 and  b==a
False

2. OR

It returns True if any of the conditions provided is True. It only returns False if all the conditions provided are False.

Syntax

Logic1 OR Logic2

Example

>>> a = 10
>>> b = 20
>>> a==10 OR b==20
True
>>> 10!=10 OR 10==10
True
>>> a!=10 OR 10<2
False

3.Not

It returns True if the condition provided is False and returns False if the condition provided is True.

Syntax

NOT Logic1 

Example

>>> a = 10
>>> b = 20
>>> NOT a==10
False
>>> NOT (10!=10 OR 10==10)
False
>>> NOT(a!=10 OR 10<2)
True

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription