Bytes
Data SciencePython

What are Relational Operators in Python?

Last Updated: 9th April, 2024
icon

Arunav Goswami

Data Science Consultant at almaBetter

Dive deep into relational operators in Python with this lesson. Learn what they are, the types available, practical examples, and their applications.

Relational operators are the cornerstone of decision-making in programming. In Python, these operators allow you to compare two values, leading to a Boolean outcome: True or False. This article dives deep into the world of relational operators in Python, providing examples and practical applications to enhance your understanding.

What are Relational Operators in Python?

Relational operators in Python are used to establish a relationship between two entities, typically involving comparisons such as equality, inequality, greater than, and less than. These operators evaluate the comparison and return a Boolean value, which is either True or False. They enable the comparison of values, facilitating critical functionalities such as conditional execution, data filtering, validation, and the efficient implementation of algorithms. By allowing the development of complex logical conditions, relational operators enhance both the functionality and interactivity of software, making them fundamental to programming.

Types of Relational Operators in Python

Python supports the following relational operators:

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)

Relational Operators in Python with Examples

Let's explore relational operators in python examples to understand their functionality.

Equal to (==)

This operator checks if the values on both sides are equal. If so, it returns True; otherwise, it returns False.

print(5 == 5# True
print(3 == 4# False

Not Equal to (!=)

It verifies if two values are not equal. If they are indeed unequal, the result is True; otherwise, False.

print(3 != 4# True
print(5 != 5# False

Greater Than (>)

This operator checks if the value on the left is greater than the one on the right.

print(63# True
print(36# False

Less Than (<)

It determines if the value on the left is less than the value on the right.

print(25# True
print(52# False

Greater Than or Equal to (>=)

This operator checks if the left value is greater than or equal to the right value.

print(5 >= 5# True
print(4 >= 6# False

Less Than or Equal to (<=)

It assesses if the left value is less than or equal to the right value.

print(3 <= 4# True
print(6 <= 4# False

Practical Applications of Relational Operators

Relational operators play a crucial role in decision-making processes within a program. They are particularly useful in:

  • Conditional Statements: To execute code based on certain conditions.
  • Loops: To control the execution flow of loops based on comparison outcomes.
  • Sorting Algorithms: To compare elements during the sorting process.
  • Data Filtering: To filter data based on specific criteria.

Relational Operators with if-else Statements

Relational operators compare values and return True or False. These comparisons can be used as conditions in if-else statements.

Example 1: Comparing Two Numbers

a = 10
b = 20
if a > b:
    print("a is greater than b")
else:
    print("a is not greater than b")

Example 2: Checking Equality

x = 5
y = 5
if x == y:
    print("x and y are equal")
else:
    print("x and y are not equal")

Example 3: Using Elif for Multiple Conditions

grade = 85
if grade >= 90:
    print("A")
elif grade >= 80:
    print("B")
else:
    print("C or below")

Using Multiple Relational Operators in Conjunction

Combining relational operators with logical operators (and, or, not) in Python allows for more complex conditions in decision-making structures, enabling the evaluation of multiple criteria simultaneously.

Example 1: Age and Membership Verification for Discount Eligibility

Consider a scenario where a store offers discounts to customers who are either over 60 years old or registered members of the store's loyalty program. To determine if a customer is eligible for a discount, both age and membership status can be evaluated using relational operators combined with the logical OR.

age = 65
is_member = False

if age > 60 or is_member:
    print("Eligible for discount")
else:
    print("Not eligible for discount")

Example 2: Qualifying for a Loan Based on Income and Credit Score

A bank may require an applicant to have a minimum annual income of $30,000 and a credit score of at least 700 to qualify for a loan. This scenario involves checking both conditions using the logical AND to ensure both criteria are met.

income = 35000
credit_score = 720
if income >= 30000 and credit_score >= 700:
    print("Loan application approved")
else:
    print("Loan application denied")

Best Practices When Using Relational Operators

When utilizing relational operators, keep the following best practices in mind:

  • Readability: Write conditions that are easy to read and understand, even if it means breaking complex comparisons into smaller parts.
  • Consistency: Be consistent in your use of relational operators, especially in large codebases where multiple developers are involved.
  • Avoid Overcomplication: Use logical operators (and, or, not) to combine simple conditions instead of creating overly complex single conditions.

Conclusion

Understanding relational operators in Python is fundamental for anyone looking to master programming logic and decision-making. By effectively utilizing these operators in Python, you can perform complex data comparisons and control the flow of your Python scripts efficiently. Experiment with these operators in your code to grasp their full potential and enhance your programming skillset.

Related Articles

Top Tutorials

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter