Stop 02 · Variables, Data Types & OperatorsTopics · 3/8

Booleans & None

Part of the Python Roadmap

In short

Every decision a program makes comes down to a yes or a no, and sometimes the honest answer is "we don't know yet". Python has True and False for the first two and None for the third.

bool has two values, True and False, and is the result of every comparison. None means 'no value yet' and is what a function returns when it has no return statement. Check for it with is None.

The jaadu flicks a big wall light switch while glancing at an empty chair with a small reserved card at a dinner table.

Think of it like this

A light switch and an empty chair

A switch is either on (True) or off (False), nothing in between. None is different: it is the empty chair at a dinner table, reserved for a guest who hasn't arrived. The chair isn't "false" or "zero", it simply has nobody in it yet.

Watch it happen

None vs False vs 0

None

  • Means "no value" or "not set yet"
  • Its type is NoneType, and there is only one None
  • Returned by functions that don't return anything
  • Check with: if x is None

False and 0

  • Real values: an answer of no, or a count of zero
  • False is a bool, 0 is an int
  • A discount of 0 is a real, valid discount
  • Check with: if x == 0 or if not flag

None says "missing". False and 0 say "known, and it's no or nothing". Keep them separate in your head.

The big ideas

What you need to know

Exactly two booleans

True and False, with capital letters. Every comparison like 5 > 3 or name == "Asha" produces one of them.

bool is a kind of int

True behaves like 1 and False like 0, so True + True is 2. Handy for counting with sum(), confusing if you're not expecting it.

None is a placeholder

Use None when a value is unknown or optional, like a middle name or a coupon code nobody has entered yet.

Use is None, not == None

There is only one None object in Python, so the identity check is None is the correct, fastest and recommended style.

See it in code

Walk through the code

python
1is_member = True2cart_total = 12003print(cart_total > 1000)   # True4print(type(is_member))     # <class 'bool'>5print(True + True)         # 26 7coupon = None              # nothing entered yet8print(coupon is None)      # True9print(type(coupon))        # <class 'NoneType'>10 11result = print("Hi!")      # print shows Hi! but returns nothing12print(result)              # None

1/4A boolean can be stored directly, or produced by a comparison like cart_total > 1000.

Booleans come from comparisons, and None marks something that has no value.

Save yourself the pain

Common mistakes

Oops

Writing true or false in lowercase.

Do this instead

Python's booleans are capitalised: True and False. Lowercase true is just an undefined name and raises a NameError.

Oops

Using if discount == None.

Do this instead

Write if discount is None. It is the PEP 8 recommended style and can't be fooled by objects with unusual == behaviour.

Oops

Treating None and 0 as the same thing.

Do this instead

A discount of 0 is a real answer. None means the discount hasn't been decided. Mixing them hides bugs.

Your mission

Yes, no or unknown

~15 min

Quick check

What is the recommended way to check whether a variable called coupon has no value?

Go deeper

  1. 1Print the result of a few comparisons and check their type.
  2. 2Write a function with no return and print what it gives back.
  3. 3Always write x is None rather than x == None.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge