6. Comparison and logical Operators

Quiz: 0/5

In programming, we often need to compare values or decide based on multiple conditions. Python provides comparison operators to compare values and logical operators to combine conditions for complex decision-making.

Comparison operators

Comparison operators evaluate two values and return a boolean result: True or False. These are essential for implementing decision-making logic.

Operator Description Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 8 > 5 True
< Less than 3 < 6 True
>= Greater than or equal to 7 >= 7 True
<= Less than or equal to 4 <= 4 True

Practical examples

Equality ==: Verify user input matches a stored value.

password = "secure123"
print(input_password == password)  # True if passwords match

Inequality !=: Filter out unwanted data.

data = 42
print(data != 0)  # True if data is not zero

Greater or Less Than (> or <): Compare scores or thresholds.

score = 85
print(score > 70)  # True if the score exceeds 70

Logical operators

Logical operators combine multiple conditions into one, enabling more complex logic.

Operator Description Example Result
and True if both conditions are true (5 > 3) and (10 > 7) True
or True if at least one condition is true (5 > 3) or (10 < 7) True
not Reverses the result of a condition not (5 > 3) False

Practical examples

and: Check if a number is within a range.

age = 25
print((age > 18) and (age < 65))  # True if age is between 18 and 65

or: Identify acceptable options.

day = "Saturday"
print((day == "Saturday") or (day == "Sunday"))  # True for weekends

not: Validate a negative condition.

logged_in = False
print(not logged_in)  # True if not logged in

Combining operators

You can mix comparison and logical operators to handle complex logic.

Example: Admission check

age = 20
has_ID = True
can_enter = (age >= 18) and has_ID
print(can_enter)  # True if age is at least 18 and ID is present

Real-World applications

  • Verify access to restricted content.
  • Validate multi-factor authentication criteria.

Operator precedence

When combining operators, Python evaluates them in a specific order, similar to PEMDAS in arithmetic:

  1. Parentheses ()
  2. Comparison operators ==, !=, >, <, >=, <=
  3. Logical NOT not
  4. Logical AND and
  5. Logical OR or

Example

x = 5
y = 10
z = 15
result = (x < y) and (y < z) or not (x == 5)
print(result)  # Output: True

Conclusion

Comparison and logical operators are vital tools for building decision-making logic in Python. They allow you to evaluate conditions, combine multiple criteria, and implement real-world scenarios like access control or data validation effectively.

Hands-On Practise

Exercise: Evaluate eligibility with logical expressions

Write a Python script that evaluates a person’s eligibility for a special offer based on predefined conditions using only comparison operators and logical operators. The output should be a boolean value (True or False).

Task description

Given the following variables:

  • age = 20 (integer): The person's age.
  • balance = 40.0 (float): The person's account balance.
  • is_student = True (boolean): Whether the person is a student.

Evaluate the person's eligibility using the following criteria:

  1. The person must be 18 years or older.
  2. The person must have at least $50 in their account if not a student.
  3. If the person is a student, they must have at least $30 in their account.

Your task:

  1. Use a single logical expression to compute a variable eligible (boolean) that determines if the person qualifies.
  2. Print the value of eligible.
Output:

Quizzes: 0/5

Question 1:

Which comparison operator checks if two values are not equal?

  • ==
  • !=
  • >
  • <=

Question 2:

What will be the output of the expression (5 > 3) and (3 < 2)?

  • True
  • False
  • None
  • Error

Question 3:

What is the result of the expression not (4 == 4)?

  • True
  • False
  • None
  • Error

Question 4:

Which logical operator returns True if at least one condition is True?

  • and
  • or
  • not
  • xor

Question 5:

What is the output of (10 >= 10) or (5 < 3)?

  • True
  • False
  • None
  • Error

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonArithmetic OperatorsNext lessonConditional statements