Logical Operators

Python · Operators

Theory

Logical operators combine conditions: and is true only when both sides are true, or is true when at least one side is true, and not flips a value. age >= 18 and country == 'IN' is True only when both checks pass. Precedence is not first, then and, then or, so a or b and c means a or (b and c); add parentheses to make intent clear.

Two details make Python special. First, and and or short-circuit: they stop as soon as the answer is known, so in x != 0 and 10 / x > 1 the division never runs when x is 0. Second, they return one of the original operands, not necessarily True or False: 0 or 'default' returns 'default', and 3 and 4 returns 4. This powers idioms like name = user_input or 'Guest'. Do not confuse them with & and |, which are bitwise operators.

Why we use it

Real rules rarely depend on one check. Logical operators let you express 'adult AND in India' or 'VIP OR order above 5000' in one readable expression, and short-circuiting lets you guard risky checks safely.

When to use it

Use and/or/not in if and while conditions with plain Python values. Use & and | (with parentheses around each condition) for pandas or NumPy boolean masks instead, because and/or do not work element-wise on columns.

How it works

  1. Evaluate A and B
  2. Evaluate A first
  3. Is A falsy? Yes: Return A immediately; B never runs. No: Evaluate B and return it.
  4. For A or B: if A is truthy return A, else evaluate and return B
  5. not A always returns a real True or False

Syntax

expr1 and expr2
expr1 or expr2
not expr

Example

age = 20
country = "IN"
print(age >= 18 and country == "IN")

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic