L1·Foundations

⚖️Comparison, Logic & Identity

==, is, in, and/or/not, chained comparisons, precedence.

operatorsisinprecedence

§ 1Comparison

== != < <= > >= compare values. Python allows chaining: 0 < x < 10 means 0 < x and x < 10.

§ 2== vs is

== asks 'same value?'. is asks 'same object in memory?'. Use is only for None, True, False.

1a = [1, 2]​
2b = [1, 2]​
3print(a == b) # True​
4print(a is b) # False​

§ 3Membership & logic

in checks containment in strings, lists, dicts (keys) and sets. and/or short-circuit and return one of the operands, not always a bool.

1name = "" or "Guest"​
2print(name) # Guest​

§ 4Precedence

From high to low: ** → unary - → * / // % → + - → comparisons → not → and → or. When unsure, add parentheses.

Worked example

1age = 25​
2print(18 <= age < 60)​
3print("py" in "python")​
4x = None​
5print(x is None)​
6print(2 + 3 * 4 ** 2)​

Try it live · Python runs in your browser

Loading playground…

Checkpoint quiz

Question 1 / 3
0 or 'x' evaluates to:

Lesson test

Take an 8-question test on “Comparison, Logic & Identity” — concepts, output prediction, bug spotting and interview questions.