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.
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.
Python supports the following relational operators:
Let's explore relational operators in python examples to understand their functionality.
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 |
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 |
This operator checks if the value on the left is greater than the one on the right.
print(6 > 3) # True print(3 > 6) # False |
It determines if the value on the left is less than the value on the right.
print(2 < 5) # True print(5 < 2) # False |
This operator checks if the left value is greater than or equal to the right value.
print(5 >= 5) # True print(4 >= 6) # False |
It assesses if the left value is less than or equal to the right value.
print(3 <= 4) # True print(6 <= 4) # False |
Relational operators play a crucial role in decision-making processes within a program. They are particularly useful in:
Relational operators compare values and return True or False. These comparisons can be used as conditions in if-else statements.
a = 10 b = 20 if a > b: print("a is greater than b") else: print("a is not greater than b") |
x = 5 y = 5 if x == y: print("x and y are equal") else: print("x and y are not equal") |
grade = 85 if grade >= 90: print("A") elif grade >= 80: print("B") else: print("C or below") |
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.
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") |
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") |
When utilizing relational operators, keep the following best practices in mind:
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