Lesson 56 +10 XP

Conditions with Operators

Conditions with Operators

Real conditions combine comparisons, logical operators, and nested checks.

and

Both must be true:

a = 200
b = 33
c = 500
if a > b and c > a:
    print("Both conditions are True")

or

At least one must be true:

if a > b or a > c:
    print("At least one condition is True")

not

Negate a condition:

if not a > b:
    print("a is NOT greater than b")

Nested if

An if inside an if:

x = 41
if x > 10:
    print("Above ten,")
    if x > 20:
        print("and also above 20!")
    else:
        print("but not above 20.")

pass

An if block can't be empty. Use pass as a placeholder:

a = 33
b = 200
if b > a:
    pass  # do nothing for now

TL;DR

  • and, or, not combine and flip conditions.
  • Nested ifs check inside a condition.
  • pass fills an empty block without errors.