None, Conversion, and Missing Values
Unit ID: M02-U06 Estimated active time: 22-30 minutes
Different values can mean different kinds of absence
Compare:
instructor_name = None
learner_count = 0
room_name = ""
status_text = "None"
These values are not interchangeable.
Noneis a special value often used to represent no value or not yet supplied.0is a numeric value.""is a string with no characters."None"is ordinary text containing four characters.
Inspect them:
print(type(instructor_name))
print(type(learner_count))
print(type(room_name))
print(type(status_text))
Check for None explicitly
Use is None:
print(instructor_name is None)
The result is True.
We will use such checks inside conditions in Module 4. For now, recognise the distinction.
Convert only when the source is suitable
Text input often needs conversion before arithmetic:
hours_text = "32"
hours_number = int(hours_text)
print(hours_number + 1)
Output:
33
Use float() for suitable decimal text:
rating_text = "4.5"
rating_number = float(rating_text)
print(rating_number)
Convert a value to display text with str():
module_number = 2
module_label = "Module " + str(module_number)
print(module_label)
An f-string is often clearer for final output.
Conversion can fail
int("thirty-two")
Python raises ValueError because the text is not a valid integer representation.
Whitespace around numeric text is accepted by int() and float(), but cleaning it explicitly makes the transformation visible:
raw_hours = " 32 "
clean_hours = raw_hours.strip()
hours = int(clean_hours)
A boolean conversion trap
Do not convert the text "False" with bool() and expect False:
print(bool("False"))
The result is True because any non-empty string is truthy. Text-to-boolean conversion needs an explicit accepted-value rule, which we will build after learning conditions.
For this module, compare cleaned text directly:
raw_value = " false "
is_available = raw_value.strip().lower() == "true"
print(is_available)
Practice
Create and inspect:
certificate_name = Nonecompleted_units = 0learner_note = ""planned_hours_text = " 32 "
Convert the planned hours to an integer and add two extension hours. Explain why the other three values should not be treated as the same kind of missing information.
Takeaway
None, zero, empty text, and the text "None" have different types and meanings. Convert only when the source value matches the required representation, and never rely on bool() to interpret human-entered boolean text. Next, we will diagnose common type mistakes systematically.
