Python None and Type Conversion (int, float, str)
None: No Value Yet
Sometimes you need a variable, but you don't have its value yet. Python has a special value for that: None.
phone_number = None
print(phone_number)
print(type(phone_number))None <class 'NoneType'>
Think of None as an empty box with a label on it. The box exists, but there's nothing inside yet.
Fill it later
winner = None
print("Winner:", winner)
winner = "Sunny"
print("Winner:", winner)Winner: None Winner: Sunny
Before the game ends, there's no winner, so None says exactly that.
None is not 0 and not ""
| Value | Means |
|---|---|
0 | the number zero, a real amount |
"" | an empty string, real text with no characters |
None | no value at all |
A score of 0 means the player scored nothing. A score of None means we don't know the score yet. That difference matters.
How to write it
- Capital
N, no quotes:None. "None"in quotes is just a four-letter word, a string.
print(type(None))
print(type("None"))<class 'NoneType'> <class 'str'>
Common beginner doubt: "Why does print() show None sometimes?" You'll see in the Functions chapter that a function which doesn't give back a value gives back None. Keep that in mind.
None means "no value yet". It's not 0, and it's not an empty string.