Fall 2024 - P4
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.2 Lesson Period 4 - None Data Abstraction
None - 3.2.9
None in Python
The None keyword is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.
Purpose
- Represents the absence of a value or a null value in Python.
- Used to indicate that a variable has no data assigned to it.
- Can serve as a placeholder in data structures (like lists or dictionaries) to signify empty entries.
- Useful in conditional statements to check if a variable has been initialized or assigned a meaningful value.
- Helps distinguish between “no value” and other falsy values (like
0
,False
, or an empty string).
# Assigning None to a variable
result = None
# Checking if a variable is None
if result is None:
print("The result has no value.")
else:
print("The result has a value:", result)
# None is commonly used as a default argument in functions
def my_function(value=None):
if value is None:
print("No value provided.")
else:
print("Value provided:", value)
The result has no value.
Javascript Version
// Assigning null to a variable
let result = null;
// Checking if a variable is null
if (result === null) {
console.log("The result has no value.");
} else {
console.log("The result has a value:", result);
}
// null is often used as a default value in function arguments
function myFunction(value = null) {
if (value === null) {
console.log("No value provided.");
} else {
console.log("Value provided:", value);
}
}
// Calling the function with and without a value
myFunction(); // No value provided.
myFunction(42); // Value provided: 42