Relational Operators

Nerd Cafe

Step 1: What Are Relational Operators?

Relational operators (also known as comparison operators) are used to compare two values. The result of a comparison is always a Boolean:

  • True or

  • False

Step 2: List of Relational Operators in Python

Operator
Description
Example

==

Equal to

a == b

!=

Not equal to

a != b

>

Greater than

a > b

<

Less than

a < b

>=

Greater than or equal to

a >= b

<=

Less than or equal to

a <= b

Step 3: Try Simple Examples

a = 10
b = 20

print(a == b)  # False
print(a != b)  # True
print(a > b)   # False
print(a < b)   # True
print(a >= b)  # False
print(a <= b)  # True

Note: Always remember that relational operations return True or False.

Step 4: Use in Real-world Examples

Example 1: Age Check System

Output: "You're eligible to vote."

Example 2: Comparing Strings (Alphabetical Order)

Note: String comparison is based on Unicode (ASCII) order, not dictionary order.

Step 5: Practical Use in Machine Learning

Relational operators are heavily used in ML tasks such as:

  • Filtering datasets

  • Conditional logic

  • Feature selection

  • Performance evaluation

Example 3: Thresholding Predictions

Suppose you’re doing binary classification, and you want to classify predictions above 0.5 as class 1:

Output: Class 1

Output:

Note: Relational operators work element-wise in Pandas.

Step 6: Combine with Logical Operators

Output: "Good score"

This is common in machine learning when checking conditions like:

Step 7: Use with NumPy Arrays (Very Important for ML)

Use case: This is used for filtering predictions, feature values, and more.

Summary Table in Code

Keywords

Relational operators, Python comparison, Python boolean, equality operator, greater than, less than, Python >=, Python <=, Python !=, boolean logic, machine learning conditions, pandas filtering, numpy comparison, thresholding predictions, conditional logic, Python operators, Python data science, binary classification, data filtering, logical comparisons, ned cafe

Last updated