Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python OR Operator
Next article icon

Python Logical Operators

Last Updated : 04 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss logical operators in Python definition and also look at some Python logical operators programs, to completely grasp the concept.


Python
# Example: Logical Operators (AND, OR, NOT) with generic variables a, b, c = True, False, True  # AND: Both conditions must be True if a and c:     print("Both a and c are True (AND condition).")  # OR: At least one condition must be True if b or c:     print("Either b or c is True (OR condition).")  # NOT: Reverses the condition if not b:     print("b is False (NOT condition).") 


In Python, Logical operators are used on conditional statements (either True or False). They perform Logical AND, Logical OR, and Logical NOT operations.

OperatorDescriptionSyntaxExample
andReturns True if both the operands are truex and yx>7 and x>10
orReturns True if either of the operands is truex or yx<7 or x>15
notReturns True if the operand is falsenot xnot(x>7 and x> 10)

Truth Table for Logical Operators in Python

Truth Table for Python Logical Operators

AND Operator in Python

The Boolean AND operator returns True if both the operands are True else it returns False. AND Operator in Python

Logical AND operator Examples


Python
a = 10 b = 10 c = -10 if a > 0 and b > 0:     print("The numbers are greater than 0") if a > 0 and b > 0 and c > 0:     print("The numbers are greater than 0") else:     print("Atleast one number is not greater than 0") 

Output

The numbers are greater than 0
Atleast one number is not greater than 0

Example 2: The code checks if all variables a, b, and c evaluate to True, printing a message accordingly.

Python
a = 10 b = 12 c = 0 if a and b and c:     print("All the numbers have boolean value as True") else:     print("Atleast one number has boolean value as False") 

Output

Atleast one number has boolean value as False

Note: If the first expression is evaluated to be false while using the AND operator, then the further expressions are not evaluated.

Python OR Operator

The Boolean OR operator returns True if either of the operands is True.

Python OR Operator

Logical OR operator in Python Examples

Python
a = 10 b = -10 c = 0 if a > 0 or b > 0:     print("Either of the number is greater than 0") else:     print("No number is greater than 0") if b > 0 or c > 0:     print("Either of the number is greater than 0") else:     print("No number is greater than 0") 

Output

Either of the number is greater than 0
No number is greater than 0

Example 2: The code checks if any of the variables a, b, or c has a boolean value as True; if so, it prints “At least one number has boolean value as True”, otherwise, it prints “All the numbers have boolean value as False”.

Python
a = 10 b = 12 c = 0 if a or b or c:     print("Atleast one number has boolean value as True") else:     print("All the numbers have boolean value as False") 

Output

Atleast one number has boolean value as True

Note: If the first expression is evaluated to be True while using or operator, then the further expressions are not evaluated.

Python NOT Operator

The Boolean NOT operator works with a single boolean value. If the boolean value is True it returns False and vice-versa.

Python NOT Operator

Logical NOT Operator Examples

The code checks if a is divisible by either 3 or 5, otherwise, it prints a message indicating that it is not. Let’s look at this Python NOT operator program to understand its working.

Python
a = 10  if not a:     print("Boolean value of a is True") if not (a % 3 == 0 or a % 5 == 0):     print("10 is not divisible by either 3 or 5") else:     print("10 is divisible by either 3 or 5") 

Output

10 is divisible by either 3 or 5

Order of Precedence of Logical Operators

In the case of multiple operators, Python always evaluates the expression from left to right. We can verify Python logical operators precedence by the below example. 

Python
def order(x):     print("Method called for value:", x)     return True if x > 0 else False   a = order b = order c = order if a(-1) or b(5) or c(10):     print("Atleast one of the number is positive") 

Output

Method called for value: -1
Method called for value: 5
Atleast one of the number is positive


Next Article
Python OR Operator

N

nikhilaggarwal3
Improve
Article Tags :
  • Python
  • Python-Operators
Practice Tags :
  • python
  • python-operators

Similar Reads

  • Python Operators
    In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /,
    6 min read
  • Precedence and Associativity of Operators in Python
    In Python, operators have different levels of precedence, which determine the order in which they are evaluated. When multiple operators are present in an expression, the ones with higher precedence are evaluated first. In the case of operators with the same precedence, their associativity comes int
    4 min read
  • Python Arithmetic Operators

    • Python Arithmetic Operators
      Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). OperatorDescriptionS
      5 min read
    • Difference between / vs. // operator in Python
      In Python, both / and // are used for division, but they behave quite differently. Let's dive into what they do and how they differ with simple examples. / Operator (True Division)The / operator performs true division.It always returns a floating-point number (even if the result is a whole number).I
      2 min read
    • Python - Star or Asterisk operator ( * )
      The asterisk (*) operator in Python is a versatile tool used in various contexts. It is commonly used for multiplication, unpacking iterables, defining variable-length arguments in functions, and more. Uses of the asterisk ( * ) operator in PythonMultiplicationIn Multiplication, we multiply two numb
      3 min read
    • What does the Double Star operator mean in Python?
      The ** (double star)operator in Python is used for exponentiation. It raises the number on the left to the power of the number on the right. For example: 2 ** 3 returns 8 (since 2³ = 8)It is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python and is also known as Power Operator. Pr
      2 min read
    • Division Operators in Python
      Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. There are two types of division operators: Float divisionInteger division( Floor division)When an in
      5 min read
    • Modulo operator (%) in Python
      Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/
      4 min read
    • Python Logical Operators

      geeksforgeeks-footer-logo
      Corporate & Communications Address:
      A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
      Registered Address:
      K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
      GFG App on Play Store GFG App on App Store
      Advertise with us
      • Company
      • About Us
      • Legal
      • Privacy Policy
      • In Media
      • Contact Us
      • Advertise with us
      • GFG Corporate Solution
      • Placement Training Program
      • Languages
      • Python
      • Java
      • C++
      • PHP
      • GoLang
      • SQL
      • R Language
      • Android Tutorial
      • Tutorials Archive
      • DSA
      • Data Structures
      • Algorithms
      • DSA for Beginners
      • Basic DSA Problems
      • DSA Roadmap
      • Top 100 DSA Interview Problems
      • DSA Roadmap by Sandeep Jain
      • All Cheat Sheets
      • Data Science & ML
      • Data Science With Python
      • Data Science For Beginner
      • Machine Learning
      • ML Maths
      • Data Visualisation
      • Pandas
      • NumPy
      • NLP
      • Deep Learning
      • Web Technologies
      • HTML
      • CSS
      • JavaScript
      • TypeScript
      • ReactJS
      • NextJS
      • Bootstrap
      • Web Design
      • Python Tutorial
      • Python Programming Examples
      • Python Projects
      • Python Tkinter
      • Python Web Scraping
      • OpenCV Tutorial
      • Python Interview Question
      • Django
      • Computer Science
      • Operating Systems
      • Computer Network
      • Database Management System
      • Software Engineering
      • Digital Logic Design
      • Engineering Maths
      • Software Development
      • Software Testing
      • DevOps
      • Git
      • Linux
      • AWS
      • Docker
      • Kubernetes
      • Azure
      • GCP
      • DevOps Roadmap
      • System Design
      • High Level Design
      • Low Level Design
      • UML Diagrams
      • Interview Guide
      • Design Patterns
      • OOAD
      • System Design Bootcamp
      • Interview Questions
      • Inteview Preparation
      • Competitive Programming
      • Top DS or Algo for CP
      • Company-Wise Recruitment Process
      • Company-Wise Preparation
      • Aptitude Preparation
      • Puzzles
      • School Subjects
      • Mathematics
      • Physics
      • Chemistry
      • Biology
      • Social Science
      • English Grammar
      • Commerce
      • World GK
      • GeeksforGeeks Videos
      • DSA
      • Python
      • Java
      • C++
      • Web Development
      • Data Science
      • CS Subjects
      @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
      We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
      Lightbox
      Improvement
      Suggest Changes
      Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
      geeksforgeeks-suggest-icon
      Create Improvement
      Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
      geeksforgeeks-improvement-icon
      Suggest Changes
      min 4 words, max Words Limit:1000

      Thank You!

      Your suggestions are valuable to us.

      What kind of Experience do you want to share?

      Interview Experiences
      Admission Experiences
      Career Journeys
      Work Experiences
      Campus Experiences
      Competitive Exam Experiences