Comparison Operators

Python · Operators

Theory

Comparison operators compare two values and always return a bool: == equal, != not equal, > greater, < less, >= greater or equal, <= less or equal. salary > 80000 evaluates to True or False, which you can store, print or use in an if. Python lets you chain them, so 1 < x < 10 means 1 < x and x < 10, with x evaluated only once.

How values compare depends on their type. Numbers compare by value across int and float (1 == 1.0 is True). Strings compare character by character using Unicode code points, so '10' < '9' is True and 'Z' < 'a' is True, a common trap with numbers stored as text. Different types are never equal ('5' == 5 is False), and ordering them raises TypeError (5 < 'a'). Never test floats with ==, because 0.1 + 0.2 == 0.3 is False.

Why we use it

Comparisons are the questions your code asks: is this salary high, is this date past due, does this ID match? Every filter, condition and validation rule starts with one.

When to use it

Use comparisons inside if, while, filters and list comprehensions. Convert text to numbers before comparing numeric data. For floats use math.isclose(); to check for None, use is None instead of == None.

How it works

  1. Python meets salary > 80000
  2. Evaluate both sides: 85000 and 80000
  3. Can these two types be ordered? Yes: Compare them by value. No: Raise TypeError (e.g. int vs str).
  4. Is the comparison true? Yes: Return True. No: Return False.
  5. print shows True

Syntax

==  !=  >  <  >=  <=

Example

salary = 85000
print(salary > 80000)

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