Equality and Inequality Testing

🏷️ Conditional Logic and Decision Making / Comparison Operators

Making decisions in code often starts with checking whether two values are the same or different. Equality and inequality testing are the most fundamental comparison operations you will use when writing conditional logic.


🧠 What Are Equality and Inequality Tests?

Equality testing checks if two values are identical. Inequality testing checks if two values are not identical. These tests return either True or False, which then drives the flow of your program.

  • Equality asks: Are these two things the same?
  • Inequality asks: Are these two things different?

⚙️ The Equality Operator: ==

The double equals sign == is used to test whether two values are equal. Do not confuse this with a single equals sign =, which is used for assignment (giving a value to a variable).

  • 5 == 5 evaluates to True
  • 5 == 3 evaluates to False
  • "hello" == "hello" evaluates to True
  • "hello" == "world" evaluates to False

You can test equality across different data types, but be careful. A string "5" is not the same as the integer 5.

  • "5" == 5 evaluates to False

🛠️ The Inequality Operator: !=

The exclamation mark followed by an equals sign != is used to test whether two values are not equal.

  • 5 != 3 evaluates to True (they are different)
  • 5 != 5 evaluates to False (they are the same)
  • "apple" != "orange" evaluates to True
  • "apple" != "apple" evaluates to False

Inequality is essentially the opposite of equality. If == returns True, then != returns False, and vice versa.


🕵️ Using Equality and Inequality in Conditional Statements

These operators become powerful when used inside if statements. The result of the comparison determines which block of code runs.

  • if status == "active": runs the indented code only when the variable status holds the exact string "active"
  • if status != "inactive": runs the indented code only when status is anything other than "inactive"

You can also use these tests with variables directly.

  • if user_input == expected_value: checks if the user's input matches what you were expecting
  • if error_code != 0: checks if an error has occurred (assuming 0 means no error)

📊 Equality and Inequality at a Glance

Operator Meaning Example Result
== Equal to 10 == 10 True
== Equal to 10 == 5 False
!= Not equal to 10 != 5 True
!= Not equal to 10 != 10 False

⚠️ Common Pitfalls for Beginners

  • Using = instead of == is the most frequent mistake. Remember: = assigns a value, == compares values.
  • Mixing data types can lead to unexpected results. A string "100" is not equal to the number 100.
  • Case sensitivity matters in string comparisons. "Yes" == "yes" evaluates to False. Always be consistent with capitalization.

💡 Practical Examples in Context

Imagine you are writing a script that checks the status of a service.

  • if service_status == "running": proceed with monitoring
  • if service_status != "running": trigger a restart

Or perhaps you are validating configuration values.

  • if config_version == expected_version: skip the update
  • if config_version != expected_version: download the new configuration

These simple comparisons form the backbone of decision-making in your code. Master them first, and everything else becomes easier.


✅ Key Takeaways

  • Use == to test if two values are equal
  • Use != to test if two values are not equal
  • Both operators return True or False
  • Always use double equals for comparison, never single equals
  • Be mindful of data types and case sensitivity when comparing strings

Equality and inequality testing compares two values and returns True if they match or False if they do not, using == and != operators.

🔧 Example 1: Basic equality test with integers

This example checks if two integer values are equal.

a = 5
b = 5
result = (a == b)
print(result)

📤 Output: True


🔧 Example 2: Basic inequality test with integers

This example checks if two integer values are not equal.

x = 10
y = 20
result = (x != y)
print(result)

📤 Output: True


🔧 Example 3: Equality test with strings

This example compares two text strings for an exact match.

name_1 = "engineer"
name_2 = "Engineer"
result = (name_1 == name_2)
print(result)

📤 Output: False


🔧 Example 4: Inequality test with floats

This example checks if two decimal numbers are different.

temperature_1 = 98.6
temperature_2 = 100.2
result = (temperature_1 != temperature_2)
print(result)

📤 Output: True


🔧 Example 5: Practical sensor reading comparison

This example checks if a sensor reading matches an expected safe value.

expected_voltage = 12.0
sensor_reading = 12.0
is_safe = (sensor_reading == expected_voltage)
print(is_safe)

📤 Output: True


🔧 Example 6: Checking if a status code is not an error

This example uses inequality to confirm a system status is not an error code.

status_code = 200
error_code = 500
is_ok = (status_code != error_code)
print(is_ok)

📤 Output: True


🔧 Example 7: Comparing boolean values

This example tests equality between two boolean (True/False) values.

system_online = True
system_ready = True
both_ready = (system_online == system_ready)
print(both_ready)

📤 Output: True


Comparison Table

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
== Equal to (string) "hi" == "hi" True
!= Not equal to (float) 1.5 != 1.5 False
== Equal to (boolean) True == False False

Making decisions in code often starts with checking whether two values are the same or different. Equality and inequality testing are the most fundamental comparison operations you will use when writing conditional logic.


🧠 What Are Equality and Inequality Tests?

Equality testing checks if two values are identical. Inequality testing checks if two values are not identical. These tests return either True or False, which then drives the flow of your program.

  • Equality asks: Are these two things the same?
  • Inequality asks: Are these two things different?

⚙️ The Equality Operator: ==

The double equals sign == is used to test whether two values are equal. Do not confuse this with a single equals sign =, which is used for assignment (giving a value to a variable).

  • 5 == 5 evaluates to True
  • 5 == 3 evaluates to False
  • "hello" == "hello" evaluates to True
  • "hello" == "world" evaluates to False

You can test equality across different data types, but be careful. A string "5" is not the same as the integer 5.

  • "5" == 5 evaluates to False

🛠️ The Inequality Operator: !=

The exclamation mark followed by an equals sign != is used to test whether two values are not equal.

  • 5 != 3 evaluates to True (they are different)
  • 5 != 5 evaluates to False (they are the same)
  • "apple" != "orange" evaluates to True
  • "apple" != "apple" evaluates to False

Inequality is essentially the opposite of equality. If == returns True, then != returns False, and vice versa.


🕵️ Using Equality and Inequality in Conditional Statements

These operators become powerful when used inside if statements. The result of the comparison determines which block of code runs.

  • if status == "active": runs the indented code only when the variable status holds the exact string "active"
  • if status != "inactive": runs the indented code only when status is anything other than "inactive"

You can also use these tests with variables directly.

  • if user_input == expected_value: checks if the user's input matches what you were expecting
  • if error_code != 0: checks if an error has occurred (assuming 0 means no error)

📊 Equality and Inequality at a Glance

Operator Meaning Example Result
== Equal to 10 == 10 True
== Equal to 10 == 5 False
!= Not equal to 10 != 5 True
!= Not equal to 10 != 10 False

⚠️ Common Pitfalls for Beginners

  • Using = instead of == is the most frequent mistake. Remember: = assigns a value, == compares values.
  • Mixing data types can lead to unexpected results. A string "100" is not equal to the number 100.
  • Case sensitivity matters in string comparisons. "Yes" == "yes" evaluates to False. Always be consistent with capitalization.

💡 Practical Examples in Context

Imagine you are writing a script that checks the status of a service.

  • if service_status == "running": proceed with monitoring
  • if service_status != "running": trigger a restart

Or perhaps you are validating configuration values.

  • if config_version == expected_version: skip the update
  • if config_version != expected_version: download the new configuration

These simple comparisons form the backbone of decision-making in your code. Master them first, and everything else becomes easier.


✅ Key Takeaways

  • Use == to test if two values are equal
  • Use != to test if two values are not equal
  • Both operators return True or False
  • Always use double equals for comparison, never single equals
  • Be mindful of data types and case sensitivity when comparing strings

Interactive Views

You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.

Equality and inequality testing compares two values and returns True if they match or False if they do not, using == and != operators.

🔧 Example 1: Basic equality test with integers

This example checks if two integer values are equal.

a = 5
b = 5
result = (a == b)
print(result)

📤 Output: True


🔧 Example 2: Basic inequality test with integers

This example checks if two integer values are not equal.

x = 10
y = 20
result = (x != y)
print(result)

📤 Output: True


🔧 Example 3: Equality test with strings

This example compares two text strings for an exact match.

name_1 = "engineer"
name_2 = "Engineer"
result = (name_1 == name_2)
print(result)

📤 Output: False


🔧 Example 4: Inequality test with floats

This example checks if two decimal numbers are different.

temperature_1 = 98.6
temperature_2 = 100.2
result = (temperature_1 != temperature_2)
print(result)

📤 Output: True


🔧 Example 5: Practical sensor reading comparison

This example checks if a sensor reading matches an expected safe value.

expected_voltage = 12.0
sensor_reading = 12.0
is_safe = (sensor_reading == expected_voltage)
print(is_safe)

📤 Output: True


🔧 Example 6: Checking if a status code is not an error

This example uses inequality to confirm a system status is not an error code.

status_code = 200
error_code = 500
is_ok = (status_code != error_code)
print(is_ok)

📤 Output: True


🔧 Example 7: Comparing boolean values

This example tests equality between two boolean (True/False) values.

system_online = True
system_ready = True
both_ready = (system_online == system_ready)
print(both_ready)

📤 Output: True


Comparison Table

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
== Equal to (string) "hi" == "hi" True
!= Not equal to (float) 1.5 != 1.5 False
== Equal to (boolean) True == False False