AND, OR, NOT: Combining Conditions

Real decisions need more than one condition. Are you 18 or older and do you have a ticket? Three logical operators combine Booleans:

AND: true only when both sides are true

OR: true when at least one side is true

NOT: flips a Boolean to its opposite

Logical operators

PYTHON

age >= 18 and has_ticket
age >= 18 or is_member
not is_banned

JAVASCRIPT

age >= 18 && hasTicket
age >= 18 || isMember
!isBanned
Compare → Identical logic, different symbols. Python spells the words out: and, or, not, which reads almost like English. JavaScript uses symbols: && for and, || for or, ! for not. Once you know the pairing, you can read either instantly.
EXAMPLE
loading editor…
YOUR TURN
Your turn → AND needs both; OR needs one; NOT flips. Add a has_id variable (hasId in JavaScript), then a line that prints True only when the person is 18 or older, has a ticket, and has ID.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

✦ The truth tables, in one breath

AND: only true + true gives true. Everything else is false. OR: only false + false gives false. Everything else is true. NOT: swaps. That's all there is to memorize, the rest is practice.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Drop volunteer_hours to 30 and rerun. One false condition sinks the whole AND chain. That's the point of AND.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

◈ Graded Challenge

A studio lets someone in when they are 16 or older AND not suspended. Using the given variables, create a variable named can_enter holding the result of that check.

Name it exactly: can_enter

1 visible + 1 hidden
loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back