# Logical Operators Python

04 May 2024
Beginner
539 Views

## Logical Operators in Python: An Overview

Python, renowned for its simplicity and readability, includes a set of logical operators that play a pivotal role in decision-making within programs. Previously we have seen types of operators in Python. In this article, we will learn about the logical operators in Python, exploring their types and providing examples to illustrate their usage.

If want more concepts regarding the Python and operators, you must explore a Python tutorial, which provides you each and every concept of Python in detail with hands on examples.

## What are the Logical Operators in Python?

Logical operators allow developers to define complex conditions based on Boolean values, making it easier to write robust and efficient code. Logical operators are symbols or keywords that are used to alter Boolean values and produce a Boolean result. Python has three logical operators: and, or, and not.

These operators enable programmers to combine and modify Boolean values to make decisions.

#### Syntax

``operand_1 logical_operator operand_2``

### Truth Table for Logical Operators in Python

 Operand1 Operand2 Operand1 AND Operand2 Operand1 OR Operand2 NOT (Operand1) NOT (Operand2) T T T T F F T F F T F T F T F T T F F F F F T T

Read More - 50 Python Interview Questions

## Types of Logical Operators

### 1. AND Operator

AND operator returns True only if both of its operands are true; otherwise, it returns False. It is often used to check multiple conditions simultaneously.

#### Syntax

``operand_1 and operand_2``

#### Example

``````x = True
y = False

result = x and y
print(result)``````

#### Explanation

In this example, the and operator returns False since both x and y must be True for the overall expression to be True. The output of the code will be 'False.'

#### Output

``````False
``````

### 2. OR Operator

The OR operator returns True if at least one of its operands is true; otherwise, it returns False. It provides a flexible way to handle situations where multiple conditions may lead to the same outcome.

#### Syntax

``operand_1 or operand_2``

#### Example

``````a = True
b = False

result = a or b
print(result)``````

#### Explanation

In this example, two Boolean variables a and b are initialized with the values True and False respectively. The code then employs the or logical operator to evaluate the logical disjunction of a and b.
The 'or' operator returns True if at least one of its operands is True, and False only if both operands are False and hence the code will output True.

#### Output

``True``

Read More - Python Developer Salary

### 3. NOT Operator

The not operator is a unary operator that returns the opposite Boolean value of its operand. If the operand is True, not returns False, and vice versa.

#### Syntax

``operand_1 not operand_2``

#### Example

``````z = True

result = not z
print(result)``````

#### Explanation

In this example, a Boolean variable z is assigned the value True. Subsequently, the not operator is applied to the variable z, resulting in the negation of its Boolean value.

#### Output

``False``

## Examples of Logical Operators in Python Compiler

### Example 1: Checking Age Eligibility

``````age = 25

if age >= 18 and age <= 60:
print("You are eligible to vote and work.")
else:
print("You are not eligible to vote or work.")``````

#### Explanation

The example is a simple eligibility check based on age. The variable age is assigned a value of 25, and the code checks whether the age falls within the range of 18 to 60 (inclusive). This example demonstrates a basic use of logical operators (and) to evaluate multiple conditions for decision-making in a Python program.

#### Output

``You are eligible to vote and work.``

### Example 2: Validating User Input

``````username = input("Enter your username: ")

else:

#### Explanation

This example captures user input for a username and password. It then checks whether the entered username is "admin" and the password is "admin@123". If both conditions are met, the program prints "Login successful." Otherwise, it prints "Invalid username or password." This example demonstrates a basic use of logical operators (and).

#### Output

``````Enter your username: admin

### Example 3: Temperature Check

``````temperature = 30

if temperature > 30 or temperature < 10:
print("Extreme temperature detected. Take precautions.")
else:
print("Temperature is within the normal range.")``````

#### Explanation

This Python code snippet in the Python Editor checks the value of the variable temperature to determine if it falls outside the normal temperature range. This code showcases the use of logical operators (or) in a conditional statement to make decisions based on specific temperature conditions.

#### Output

``Temperature is within the normal range.``
##### Summary

Logical operators are indispensable tools in Python programming, enabling developers to create intricate decision structures. The and, or, and not operators empower programmers to build flexible and efficient code, enhancing the overall functionality of Python applications.

This was all about the basics of logical operators in Python, if you want to go deeper into data types then consider enrolling in a Python Certification Course.

## FAQs

### Q1. Can logical operators be used with non-Boolean values?

Yes, Python allows the use of logical operators with non-Boolean values. The results depend on the truthiness or falseness of the values involved.

### Q2. Are there other logical operators in Python?

The primary logical operators in Python are and, or, and not. These operators cover a wide range of logical conditions and using them appropriately is key to effective decision-making in Python programs.

### Q3. What are the logical operators?

The six comparison operators are 1) AND, 2) OR, 3) NOT. They can be used to make decision on different conditions in Python.
Share Article

### Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
 Angular Certification Course Jun 16 SAT, SUN Filling Fast 09:30AM to 11:30AM (IST) Advanced Full-Stack .NET Developer Certification Training Jun 17 MON, WED, FRI Filling Fast 07:00AM to 08:30AM (IST) ASP.NET Core Certification Training Jun 17 MON, WED, FRI Filling Fast 07:00AM to 08:30AM (IST) .NET Microservices Certification Training Jun 23 SAT, SUN Filling Fast 05:30PM to 07:30PM (IST) ASP.NET Core (Project) Jun 23 SAT, SUN Filling Fast 08:30PM to 10:30PM (IST) Azure Developer Certification Training Jun 23 SAT, SUN Filling Fast 07:00AM to 09:00AM (IST) React JS Certification Training | Best React Training Course Jun 30 SAT, SUN Filling Fast 08:30PM to 10:30PM (IST) ASP.NET Core Certification Training Jun 30 SAT, SUN Filling Fast 10:00AM to 12:00PM (IST) Advanced Full-Stack .NET Developer Certification Training Jun 30 SAT, SUN Filling Fast 10:00AM to 12:00PM (IST) Generative AI For Software Developers Jul 14 SAT, SUN Filling Fast 08:30PM to 10:30PM (IST)

Can't find convenient schedule? Let us know

Similar Articles