Understanding Python Operators: Part 6

Assignment vs. Comparison: Key Differences

Understanding the difference between assignment and comparison is critical in Python. While they might look similar, their purposes and uses are fundamentally different. Let’s break it down.


📝 Assignment (=)

The assignment operator (=) is used to assign a value to a variable.

Example:

A = 5  # Assigns the value 5 to variable A

❓ Comparison (==)

The comparison operator (==) checks if two values are equal and returns a boolean (True or False).

Example:

A = 5
B = 5

A == B  # Returns True because A and B are equal

Using != (Not Equal To)

This operator checks if two values are not equal:

A = 90
B = 95

A != B  # Returns True because A and B are different

⚠️ Common Mistake

Mixing up = and == can lead to errors. For example:

if A = 5:  # Error! '=' is not a comparison operator
    print("A equals 5")

Correct usage:

if A == 5:  # Correct
    print("A equals 5")

🔗 Shorthand for Assignment Operators

Python provides augmented assignment operators, which combine operations with assignment, making the code concise and readable.

Examples:

A = 5
A += 2  # Equivalent to A = A + 2
print(A)  # Output: 7

A -= 2  # Equivalent to A = A - 2
print(A)  # Output: 5

A *= 2  # Equivalent to A = A * 2
print(A)  # Output: 10

A /= 2  # Equivalent to A = A / 2
print(A)  # Output: 5.0

🔍 Logical Operators: AND, OR, NOT

Logical operators are used to combine multiple conditions. They return a boolean (True or False):

AND (and)

Returns True if both conditions are True.

Example:

A = 85
B = 90

A > 80 and B > 80  # Returns True

OR (or)

Returns True if at least one condition is True.

Example:

A = 75
B = 90

A > 80 or B > 80  # Returns True

NOT (not)

Reverses the result of a condition.

Example:

A = 75

not (A > 80)  # Returns True because A > 80 is False

🏫 Practical Example: Admission to Harvard

Imagine the conditions for admission to Harvard:

  • GMAT score greater than 720

  • Interview pass

  • Strong statement of purpose (SOP)

You could represent this in Python as:

GMAT = 750
Interview = True
SOP = True

Admission = GMAT > 720 and Interview and SOP
print(Admission)  # Output: True

If any of these conditions fail, Admission will be False.


📊 Truth Tables for Logical Operators

AND Operator

X

Y

X AND Y

0

0

0

0

1

0

1

0

0

1

1

1

The AND operator only returns True when both conditions are True.


OR Operator

X

Y

X OR Y

0

0

0

0

1

1

1

0

1

1

1

1

The OR operator returns True if either condition is True.


🤔 Combining Logical Operators

You can combine multiple conditions to create complex decision-making logic.

Example: Social Event Decision

FriendBirthday = True
GirlfriendBirthday = False
Breakup = False

GoOut = FriendBirthday or GirlfriendBirthday or Breakup
print(GoOut)  # Output: True

Even though only FriendBirthday is True, the result is True because the OR operator requires just one True condition.


💡 Key Takeaways

  1. Assignment (=) assigns a value, while comparison (==) checks equality.

  2. Augmented assignment operators (e.g., +=, *=) simplify repetitive operations.

  3. Logical operators (AND, OR, NOT) enable complex decision-making.

  4. Boolean logic and truth tables help visualize how conditions are evaluated.


🚀 Next Steps

In Part 7, we’ll explore how to use compound logical expressions to handle more advanced decision-making scenarios in Python. Stay tuned for more insights! 😊

Last updated