Ravindra BagaleCourses & study guides मराठी Track your progress

Python Variables and Data Types Explained for Beginners (int, float, str, bool)

Data Science

P2.5 What Is None in Python

Why. Real data has missing values: a phone number not given, a delivery not yet done. Python uses None for "no value yet".

What. None is a special value that means nothing is there. It is not 0 and not an empty text. In pandas, missing values show as NaN or None.

How.

Check for a missing value

  1. Store None when a value is unknown.
  2. Test it with is None.
  3. Decide what to do: skip it or fill a default.

Worked example A — A missing delivery time.

Step 1 — What we do. A courier in Lagos has not delivered yet, so the delivery time is unknown.

Step 2 — Type and run this code.

delivered_at = None
print(delivered_at)
print(delivered_at is None)

Step 3 — Output you should see.

None
True

Step 4 — What it means. is None gives True, so we know the value is missing. Use is None, not == None.

Worked example B — Fill a default when missing.

Step 1 — What we do. If the customer city is missing, we use "Unknown".

Step 2 — Type and run this code.

city = None
if city is None:
    city = "Unknown"
print(city)

Step 3 — Output you should see.

Unknown

Step 4 — What it means. This is the same idea as fillna in pandas, just for one value. (We learn if properly in the if/else chapter.)

Ravindra Bagale's Tip

0 means "zero", None means "we do not know". Never put 0 for a missing amount — it will pull your average down.

Samajla ka? If yes, take the quiz for this chapter. If not, run every example again and change one value each time.