and, or, not
Combine bools into bigger conditions:
a and bis True only if both are Truea or bis True if at least one is Truenot aflips True and False
is_weekend = True has_money = False go_out = is_weekend and has_money # False
Parentheses group a choice: (ticket or pass_holder) and not closed requires either credential and an open door.
See it run
lamp_on = True battery_dead = True works = lamp_on and not battery_dead dim = lamp_on or battery_dead print(works, dim)
Two bools, combined two different ways. Watch how and and or treat the same inputs.
Practice and, or, not
Exercise 1
With temp = 25, sunny = True, raining = False:
good_day= temp above 20 and sunny and not rainingstay_in= raining or temp below 0
Exercise 2
A museum admits visitors with a ticket or a pass, but never while closed. With ticket = False, pass_holder = True, closed = False, compute boolean admit using or, and and not.
Exercise 3
A download needs Wi-Fi or mobile permission, enough storage, and no pause. Use wifi = False, mobile_allowed = True, free_mb = 500, size_mb = 500, paused = False. Compute boolean can_download; an exact storage fit is allowed.
Free lesson: use the editor to practice it.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.