Python
Programming
Coding Best Practices
Conditional Statements
NoneType

if A vs if A is not None

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

if A: and if A is not None: answer different questions in Python. The first checks truthiness. The second checks only whether the value is the singleton None. Using the wrong one is a common cause of bugs when 0, "", or [] are valid inputs.

Truthiness and if A:

Python treats many values as false in conditionals:

  • 'None'
  • 'False'
  • '0'
  • '0.0'
  • '""'
  • '[]'
  • '{}'

Example:

python
1def report(value):
2    if value:
3        print("truthy")
4    else:
5        print("falsy")
6
7
8report(None)
9report(0)
10report("")
11report([1, 2])

This is the right test when your real intention is "does this value contain useful content."

if A is not None: Means Something Narrower

None is a special marker for "no value." A None check ignores general truthiness and asks only whether the variable is missing.

python
1def report(value):
2    if value is not None:
3        print("not None")
4    else:
5        print("None")
6
7
8report(None)
9report(0)
10report("")
11report([])

Here, 0, "", and [] all pass the check because they are real values, even though they are falsy.

The Classic Bug

Consider an optional numeric argument:

python
1def set_limit(limit=None):
2    if limit:
3        print(f"custom limit: {limit}")
4    else:
5        print("default limit")
6
7
8set_limit(0)

This prints "default limit", which is wrong if 0 is a meaningful explicit value.

Correct version:

python
1def set_limit(limit=None):
2    if limit is not None:
3        print(f"custom limit: {limit}")
4    else:
5        print("default limit")
6
7
8set_limit(0)

Now the semantics are correct.

When if A: Is Better

Use if A: when emptiness itself should count as false.

python
1items = []
2
3if items:
4    print("process items")
5else:
6    print("nothing to process")

That is a good fit because an empty list really means there is nothing to do.

Strings are another common case:

python
1name = ""
2
3if name:
4    print("name provided")
5else:
6    print("name missing or empty")

If blank input should be treated the same as missing input, if name: is the cleaner choice.

Why is not None Beats != None

When checking for None, use identity comparison:

python
if value is not None:
    print("value exists")

This is better than value != None because None is a singleton and identity expresses the real intent directly. It also avoids odd behavior from custom equality implementations.

Custom Classes Can Change the Result of if A:

Objects can define __bool__ or __len__, which means even custom instances can be falsy.

python
1class Box:
2    def __init__(self, items):
3        self.items = items
4
5    def __bool__(self):
6        return bool(self.items)
7
8
9print(bool(Box([])))
10print(bool(Box([1])))

That is another reason the two conditions are not style variants. One respects truthiness rules. The other checks only for None.

A Practical Rule

Ask yourself:

  • Should all falsy values be rejected.
  • Or should only the missing marker None be rejected.

If the first answer is correct, use if A:.

If the second answer is correct, use if A is not None:.

That decision is about data meaning, not about which syntax looks nicer.

Common Pitfalls

  • Using if A: when 0 or "" are valid values.
  • Using if A is not None: when empty containers should also count as missing.
  • Comparing to None with == instead of is.
  • Forgetting that custom objects can override truthiness.
  • Choosing the shorter form without thinking about semantics.

Summary

  • 'if A: checks truthiness.'
  • 'if A is not None: checks only for None.'
  • Use if A: when empty and zero-like values should count as false.
  • Use if A is not None: when falsy values can still be valid input.
  • The correct condition depends on what the value means in your program.

Course illustration
Course illustration

All Rights Reserved.