"""
conditionals_demo_basic.py
--------------------------
Intro demo of:
- Number comparisons: ==, !=, <, <=, >, >=
- String comparisons (case sensitivity, lexicographic order) and "in" membership
- Logical operators used directly: and, or, not
- Precedence and parentheses
- Short-circuit idea without defining custom functions
Note: We avoid assigning boolean expressions to variables; we use them directly.
We also avoid f-strings for compatibility with some classroom environments.
"""
print("=" * 70)
print("1) COMPARISONS ON NUMBERS")
print("=" * 70)
age = 19
speed = 72
limit = 70
print("age =", age, " speed =", speed, " limit =", limit)
# Basic comparisons
print("age >= 18 ->", age >= 18) # True
print("age == 21 ->", age == 21) # False
print("speed > limit ->", speed > limit) # True
print("speed <= limit ->", speed <= limit) # False
# Chained comparison reads like English (optional but nice to see)
print("55 <= speed <= 70 ->", 55 <= speed <= 70)
# Directly using logical operators (no boolean temp variables)
print("(age >= 18) and (speed > limit) ->", (age >= 18) and (speed > limit))
print("(age < 16) or (speed <= limit) ->", (age < 16) or (speed <= limit))
print("not (speed > limit) ->", not (speed > limit))
print("\n" + "=" * 70)
print("2) COMPARISONS ON STRINGS")
print("=" * 70)
name1 = "Alice"
name2 = "alice"
a = "apple"
b = "banana"
caps = "Zoo"
lower = "apple"
print('name1 = "' + name1 + '" name2 = "' + name2 + '"')
# Equality / inequality are case-sensitive
print('name1 == name2 ->', name1 == name2) # False (A vs a)
print('name1 != name2 ->', name1 != name2) # True
# Case-insensitive equality (common pattern)
print('name1.lower() == name2.lower() ->', name1.lower() == name2.lower()) # True
# Lexicographic (dictionary-like) order
print('"' + a + '" < "' + b + '" ->', a < b) # True: "apple" < "banana"
# Uppercase letters generally come before lowercase in ASCII/Unicode order
print('"' + caps + '" < "' + lower + '" ->', caps < lower)
# Substring membership
phrase = "concatenate"
print('"cat" in "' + phrase + '" ->', "cat" in phrase) # True
print("\n" + "=" * 70)
print("3) AND / OR / NOT IN IF-STATEMENTS (USED DIRECTLY)")
print("=" * 70)
email = "studentexample.com" # missing '@' on purpose
# Validate email contains '@' using 'in' and 'not'
if "@" in email:
print('Email "' + email + '" looks OK (has "@").')
else:
print('Email "' + email + '" is missing "@".')
# Combine conditions directly with and/or
# Example: free entry if (student AND under 25) OR (today is holiday)
is_holiday = True # demo values
student_age = 20
if (student_age < 25 and student_age >= 18) or is_holiday:
print("Condition met: (student_age between 18-24) OR today is a holiday.")
else:
print("Condition not met.")
# Use 'not' to invert a condition
if not (speed > limit):
print("Driver is NOT speeding.")
else:
print("Driver IS speeding.")
print("\n" + "=" * 70)
print("4) PRECEDENCE AND PARENTHESES")
print("=" * 70)
# Precedence: not > and > or
print("not False and False or True ->", not False and False or True)
print("((not False) and False) or True ->", ((not False) and False) or True)
# Parentheses for clarity (recommended)
x = 10
y = 5
z = 5
print("(x > y) and (y == z) ->", (x > y) and (y == z)) # True and True -> True
print("(x < y) or (y != z) ->", (x < y) or (y != z)) # False or False -> False
print("\n" + "=" * 70)
print("5) SHORT-CIRCUIT IDEA (no custom functions)")
print("=" * 70)
# In 'and', if the left side is False, Python skips evaluating the right side.
print("Demonstrate: False and print('RIGHT SIDE') -> the RIGHT SIDE print should NOT appear:")
print("Result:", False and print("RIGHT SIDE")) # right side is skipped; prints only "Result: False"
# In 'or', if the left side is True, Python skips evaluating the right side.
print("\nDemonstrate: True or print('RIGHT SIDE') -> the RIGHT SIDE print should NOT appear:")
print("Result:", True or print("RIGHT SIDE")) # right side is skipped; prints only "Result: True"
print("\n" + "=" * 70)
print("6) MINI EXERCISES (try by editing values above or in a REPL)")
print("=" * 70)
print("- Make 'email' valid and see the message change.")
print("- Change 'speed' and 'limit' to make 'not (speed > limit)' become True.")
print("- Write a condition that is True only if 0 <= n < 100 and n is even.")
print("- Compare two names ignoring case and leading/trailing spaces:")
print(" For example: nameA.strip().lower() == nameB.strip().lower()")