Fall 2024 - P4
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.2 None and null
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.
# 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
%%js
// 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
<IPython.core.display.Javascript object>