Top Ad unit 728 × 90

Libreoffice

Libreoffice Writer

Logical Operators in Python

 




Logical Operators in Python


Python provides three logical operators:

 And, Or, and Not. 

These operators are used to combine conditional statements and evaluate expressions based on Boolean logic. 


And operator:


The and operator returns True if both operands are True.

If either or both operands are False, it returns False.

This operator exhibits short-circuiting behavior: if the first operand is False, the second operand is not evaluated.


Python


    x = 5

    y = 10

    print(x > 0 and y < 15)  


Output: True

    

print(x > 0 and y < 5)   

 

Output: False


Or operator:


The or operator returns True if at least one of the operands is True. 

It returns False only if both operands are False.

This operator also exhibits short-circuiting behavior: if the first operand is True, the second operand is not evaluated.


Python


    x = 5

    y = 10

    print(x > 0 or y < 5)    


 Output: True


    print(x < 0 or y < 5)   


Output: False


Not operator:


The not operator is a unary operator, meaning it operates on a single operand.

It reverses the logical state of its operand: if the operand is True, not returns False, and if the operand is False, not returns True.


Python


    is_sunny = True

    print(not is_sunny)      


Output: False


    is_raining = False

    print(not is_raining)    


 Output: True

Precedence:

The order of precedence for logical operators in Python, from highest to lowest, is: not, and, or. This means not operations are evaluated first, followed by and operations, and finally or operations. Parentheses can be used to override this default precedence.

Logical Operators in Python Reviewed by ADcomputercampus on October 05, 2025 Rating: 5

No comments:

Contact Form

Name

Email *

Message *

Powered by Blogger.