- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Questions & Answers
- Python - Online Quiz
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python - Comparison Operators
Python Comparison Operators
Comparison operators in Python are very important in Python's conditional statements (if, else and elif) and looping statements (while and for loops). The comparison operators also called relational operators. Some of the well known operators are "<" stands for less than, and ">" stands for greater than operator.
Python uses two more operators, combining "=" symbol with these two. The "<=" symbol is for less than or equal to operator and the ">=" symbol is for greater than or equal to operator.
Different Comparison Operators in Python
Python has two more comparison operators in the form of "==" and "!=". They are for is equal to and is not equal to operators. Hence, there are six comparison operators in Python and they are listed below in this table:
< | Less than | a<b |
> | Greater than | a>b |
<= | Less than or equal to | a<=b |
>= | Greater than or equal to | a>=b |
== | Is equal to | a==b |
!= | Is not equal to | a!=b |
Comparison operators are binary in nature, requiring two operands. An expression involving a comparison operator is called a Boolean expression, and always returns either True or False.
Example
a=5 b=7 print (a>b) print (a<b)
It will produce the following output −
False True
Both the operands may be Python literals, variables or expressions. Since Python supports mixed arithmetic, you can have any number type operands.
Example
The following code demonstrates the use of Python's comparison operators with integer numbers −
print ("Both operands are integer") a=5 b=7 print ("a=",a, "b=",b, "a>b is", a>b) print ("a=",a, "b=",b,"a<b is",a<b) print ("a=",a, "b=",b,"a==b is",a==b) print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
Both operands are integer a= 5 b= 7 a>b is False a= 5 b= 7 a<b is True a= 5 b= 7 a==b is False a= 5 b= 7 a!=b is True
Comparison of Float Number
In the following example, an integer and a float operand are compared.
Example
print ("comparison of int and float") a=10 b=10.0 print ("a=",a, "b=",b, "a>b is", a>b) print ("a=",a, "b=",b,"a<b is",a<b) print ("a=",a, "b=",b,"a==b is",a==b) print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of int and float a= 10 b= 10.0 a>b is False a= 10 b= 10.0 a<b is False a= 10 b= 10.0 a==b is True a= 10 b= 10.0 a!=b is False
Comparison of Complex umbers
Although complex object is a number data type in Python, its behavior is different from others. Python doesn't support < and > operators, however it does support equality (==) and inequality (!=) operators.
Example
print ("comparison of complex numbers") a=10+1j b=10.-1j print ("a=",a, "b=",b,"a==b is",a==b) print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of complex numbers a= (10+1j) b= (10-1j) a==b is False a= (10+1j) b= (10-1j) a!=b is True
You get a TypeError with less than or greater than operators.
Example
print ("comparison of complex numbers") a=10+1j b=10.-1j print ("a=",a, "b=",b,"a<b is",a<b) print ("a=",a, "b=",b,"a>b is",a>b)
It will produce the following output −
comparison of complex numbers Traceback (most recent call last): File "C:\Users\mlath\examples\example.py", line 5, in <module> print ("a=",a, "b=",b,"a<b is",a<b) ^^^ TypeError: '<' not supported between instances of 'complex' and 'complex
Comparison of Booleans
Boolean objects in Python are really integers: True is 1 and False is 0. In fact, Python treats any non-zero number as True. In Python, comparison of Boolean objects is possible. "False < True" is True!
Example
print ("comparison of Booleans") a=True b=False print ("a=",a, "b=",b,"a<b is",a<b) print ("a=",a, "b=",b,"a>b is",a>b) print ("a=",a, "b=",b,"a==b is",a==b) print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of Booleans a= True b= False a<b is False a= True b= False a>b is True a= True b= False a==b is False a= True b= False a!=b is True
Comparison of Sequence Types
In Python, comparison of only similar sequence objects can be performed. A string object is comparable with another string only. A list cannot be compared with a tuple, even if both have same items.
Example
print ("comparison of different sequence types") a=(1,2,3) b=[1,2,3] print ("a=",a, "b=",b,"a<b is",a<b)
It will produce the following output −
comparison of different sequence types Traceback (most recent call last): File "C:\Users\mlath\examples\example.py", line 5, in <module> print ("a=",a, "b=",b,"a<b is",a<b) ^^^ TypeError: '<' not supported between instances of 'tuple' and 'list'
Sequence objects are compared by lexicographical ordering mechanism. The comparison starts from item at 0th index. If they are equal, comparison moves to next index till the items at certain index happen to be not equal, or one of the sequences is exhausted. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one.
Which of the operands is greater depends on the difference in values of items at the index where they are unequal. For example, 'BAT'>'BAR' is True, as T comes after R in Unicode order.
If all items of two sequences compare equal, the sequences are considered equal.
Example
print ("comparison of strings") a='BAT' b='BALL' print ("a=",a, "b=",b,"a<b is",a<b) print ("a=",a, "b=",b,"a>b is",a>b) print ("a=",a, "b=",b,"a==b is",a==b) print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of strings a= BAT b= BALL a<b is False a= BAT b= BALL a>b is True a= BAT b= BALL a==b is False a= BAT b= BALL a!=b is True
In the following example, two tuple objects are compared −
Example
print ("comparison of tuples") a=(1,2,4) b=(1,2,3) print ("a=",a, "b=",b,"a<b is",a<b) print ("a=",a, "b=",b,"a>b is",a>b) print ("a=",a, "b=",b,"a==b is",a==b) print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
a= (1, 2, 4) b= (1, 2, 3) a<b is False a= (1, 2, 4) b= (1, 2, 3) a>b is True a= (1, 2, 4) b= (1, 2, 3) a==b is False a= (1, 2, 4) b= (1, 2, 3) a!=b is True
Comparison of Dictionary Objects
The use of "<" and ">" operators for Python's dictionary is not defined. In case of these operands, TypeError: '<' not supported between instances of 'dict' and 'dict' is reported.
Equality comparison checks if the length of both the dict items is same. Length of dictionary is the number of key-value pairs in it.
Python dictionaries are simply compared by length. The dictionary with fewer elements is considered less than a dictionary with more elements.
Example
print ("comparison of dictionary objects") a={1:1,2:2} b={2:2, 1:1, 3:3} print ("a=",a, "b=",b,"a==b is",a==b) print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of dictionary objects a= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a==b is False a= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a!=b is True