GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — Relational or Comparison Operators in Python | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
Python Programming Tutorial
  • Python Basic Concepts
    • Introduction
    • Variable
    • Data Types
    • Naming Conventions
    • Typecasting
  • Operators in Python
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Identity Operators
    • Membership Operators
    • Operator Precedence
  • Input and Output in Python
    • Environment Setup
    • print() Function
    • input() Function
    • Comments
  • Decision Making in Python
    • Decision Making
    • if Statement
    • if else Statement
    • if elif Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in Python
    • Loops
    • for Loop
    • while Loop
  • String in Python
    • String
    • String Methods
  • Built-in Data Structures in Python
    • Lists
    • Tuples
    • Sets
    • Dictionaries
  • Arrays in Python
    • Arrays
  • Searching in Python
    • Linear Search
    • Binary Search
  • Sorting in Python
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Function in Python
    • Function
    • Recursive Function
    • Lambda Function
    • Closures
    • Decorators
    • Generators
  • OOPs in Python
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Class Variables
    • Static Methods
    • Inheritance
    • Types of Inheritance
    • MRO
    • Polymorphism
    • Abstract Classes
  • User-Defined Data Structures in Python
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • Modules in Python
    • Modules
    • Time Module
    • Datetime Module
    • OS Module
    • Shutil Module
    • Sys Module
    • JSON Module
    • Random Module
  • Regular Expression in Python
    • Regular Expression
  • Exceptions in Python
    • Exception Handling
  • File Handling in Python
    • Text File Handling
    • Binary File Handling
    • CSV File Handling

bookmark
Table of Contents

    Relational or Comparison Operators in Python

    In this lesson, we will learn what is the relational or comparison operator and how it works in Python programming with some examples.

    What is Relational Operators

    Relational operators in Python, also known as comparison operators, are used to compare the values of two operands. For example, they check if one operand is equal to the other, greater than, or less than the other, and so on.

    Video Explanation

    Click the button below to watch the explanation.

    Please wait while the video is loading....
    video image

    There are 6 types of relational operators in Python and they are:

    • == Equality
    • > Greater than
    • < Less than
    • >= Greater than or equal to
    • <= Less than or equal to
    • != Not equal to

    Now let's see the examples of all the relational operators one by one for more understanding.

    Equality ==

    The equality operator == is used to check if the values of two operands are the same. The result of the == operator is True if the values are equal; otherwise, the result is False if they are not equal.

    Example
    x=5
    y=5
    z=6
    a = (x == y)   # result of a is True because x=5 is equal to y=5 so relation is true
    b = (x == z)   # result of b is False because x=5 is not equal to z=6 so relation is false

    In the example above, the result of a is True because the value of x and y is the same so, the relation is correct. On the other hand, the result of b is False because the value of x and z is not the same so the relationship is wrong.

    Greater than >

    The greater than > relational operator is used to check if the value of the left operand is greater than the value of the right operand. The result of the > operator is True if the left operand is greater; otherwise, the result is False if it is not.

    Example
    x=8
    y=5
    z=10
    a = (x > y)   # result of a is True because x=8 is greater than y=5 so relation is true
    b = (x > z)   # result of b is False because x=8 is not greater than z=10 so relation is false

    In the example above, the result of a is True because the value of x is greater than the value of y so, the relation is correct. On the other hand, the result of b is False because the value of x is not greater than the value of z so the relationship is wrong.

    Less than <

    The less than < relational operator is used to check if the value of the left operand is less than the value of the right operand. The result of the < operator is True if the left operand is less; otherwise, the result is False if it is not.

    Example
    x=5
    y=8
    z=3
    a = (x < y)   # result of a is True because x=5 is less than y=8 so relation is true
    b = (x < z)   # result of b is False because x=5 is not less than z=3 so relation is false

    In the example above, the result of a is True because the value of x is less than the value of y so, the relation is correct. On the other hand, the result of b is False because the value of x is not less than the value of z so the relationship is wrong.

    Greater than or equal to >=

    The greater than or equal to >= relational operator is used to check if the value of the left operand is greater than or equal to the value of the right operand. The result of the >= operator is True if the condition is met; otherwise, the result is False if it is not.

    Example
    w=5
    x=5
    y=3
    z=10
    a = (w >= x)   # result of a is True because w=5 is equal to x=5 so relation is true
    b = (x >= y)   # result of b is True because x=5 is greater than y=3 so relation is true
    c = (y >= z)   # result of c is False because y=5 is neither greater than z=10 nor equal to z=10 so relation is false

    In the example above, the result of a is True because the value of w is not greater but equal to the value of x so, the relation is correct. Next, the result of b is True because the value of x is greater than the value of y so, the relationship is correct. On the other hand, the result of c is False because the value of y is neither greater nor equal to the value of z so the relationship is wrong.

    Less than or equal to <=

    The less than or equal to <= relational operator is used to check if the value of the left operand is less than or equal to the value of the right operand. The result of the <= operator is True if the condition is met; otherwise, the result is False if it is not.

    Example
    w=5
    x=5
    y=7
    z=3
    a = (w <= x)   # result of a is True because w=5 is equal to x=5 so relation is true
    b = (x <= y)   # result of b is True because x=5 is less than y=7 so relation is true
    c = (y <= z)   # result of c is False because y=7 is neither less than z=3 nor equal to z=3 so relation is false

    In the example above, the result of a is True because the value of w is not less but equal to the value of x so, the relation is correct. Next, the result of b is True because the value of x is less than the value of y so, the relationship is correct. On the other hand, the result of c is False because the value of y is neither less nor equal to the value of z so the relationship is wrong.

    Not equal to !=

    The not equal to != relational operator is used to check if the value of the left operand is not equal to the value of the right operand. The result of the != operator is True if the values are not equal; otherwise, the result will be False if the values are equal.

    Example
    x=5
    y=8
    z=5
    a = (x != y)   # result of a is True because x=5 is not equal to y=8 so relation is true
    b = (x != z)   # result of b is False because x=5 is equal to y=5 so relation is false

    In the example above, the result of a is True because the value of x is not equal to the value of y so, the relation is correct. On the other hand, the result of b is False because the value of x is equal to the value of z so the relationship is wrong.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy.

    Cookie Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.