## Understanding Python Return Values

In Python programming, return values play a crucial role in the functionality of functions. This section explains different types of return values and how they are represented in Python.

### Types of Return Values

** None**: In Python,

`None`

represents the absence of a value. It is often used as a default return value when a function does not explicitly return something or when a variable has not been assigned a value. In some cases, it can be compared to the `null`

concept in other programming languages. Here’s an example of using `None`

as a return value:

def my_function(x): if x > 0: return x * 2 else: return None result = my_function(-5) print(result) # Output: None

**NaN**: `NaN`

stands for “Not a Number” and is a special floating-point value. It represents the result of an undefined or unrepresentable mathematical operation, such as the square root of a negative number or division by zero. In Python, you can use the `math`

or `numpy`

libraries to work with `NaN`

values.

Here’s an example:

import math def invalid_operation(x): return x / 0 if x > 0 else math.sqrt(x) result = invalid_operation(-1) print(result) # Output: nan

**Boolean**: `bool`

is a fundamental data type in Python, representing the two truth values: `True`

and `False`

. Functions can return boolean values to represent success or failure, or other binary conditions.

For example:

def is_even(x): return x % 2 == 0 result = is_even(4) print(result) # Output: True

## Python Functions and Return Types

Python functions can return various types of results or even no results. This section discusses different ways a Python function can return nothing, specifically: Return None, Return Null, and Return NaN.

### Return None

In Python, a function returns `None`

implicitly if there is no explicit `return`

statement. However, you can also use the `return`

statement followed by the keyword `None`

to indicate that a function should return no value. This method is useful when you want to signal that a function sometimes returns a meaningful value, but in some cases, it does not have one.

Here’s a simple example:

def add_numbers(a, b): if a is None or b is None: return None return a + b result1 = add_numbers(3, 5) result2 = add_numbers(None, 2) print(result1) # Output: 8 print(result2) # Output: None

### Return Null

Technically, Python does not have a built-in `null`

data type like some other programming languages do. The closest equivalent in Python is `None`

, which represents the absence of a value.

In the context of Python, when we mention returning “null” from a function, we’re usually referring to returning `None`

. You can follow the same steps outlined in the Return None sub-section when you want to return a “null” value in Python.

### Return NaN

`NaN`

stands for “Not a Number,” and it is a special type of floating-point value.

In Python, you can represent `NaN`

using the `float`

class’s `nan`

constant or by importing the `nan`

constant from the `math`

module. This value is used to represent undefined or unrepresentable values in floating-point calculations.

Here’s an example of returning `NaN`

from a function:

import math def divide_numbers(a, b): if b == 0: return math.nan return a / b result1 = divide_numbers(10, 2) result2 = divide_numbers(3, 0) print(result1) # Output: 5.0 print(result2) # Output: nan

## Dealing with Missing Data

When working with data in Python, missing values can be represented by `None`

or `NaN`

(Not a Number). Missing data can create issues when performing various operations, such as calculations or applying machine learning algorithms. In this section, we will discuss how to handle missing values in pandas DataFrames and Series.

### Handling Missing Values in DataFrames

Pandas is a widely used library for data manipulation, and it provides several methods to manage missing data in DataFrames. One of the most common operations is to check for missing values using the `isnull()`

method.

This method returns a DataFrame of the same shape, containing `True`

where a value is missing and `False`

otherwise:

import pandas as pd data = { "A": [1, 2, None, 4], "B": [5, None, None, 8], "C": [9, 10, 11, 12] } df = pd.DataFrame(data) print(df.isnull())

Another common operation is to fill in missing values using the `fillna()`

method. This method can take various arguments like a constant value, forward filling method (propagating the previous value in the column), or backward filling method (propagating the next value in the column).

Here’s an example of using `fillna()`

:

df_filled = df.fillna(method="ffill") print(df_filled)

You can also drop rows or columns that contain missing values using the `dropna()`

method. To drop rows containing at least one missing value, use `axis=0`

, and to drop columns containing at least one missing value, use `axis=1`

:

df_dropped_rows = df.dropna(axis=0) df_dropped_cols = df.dropna(axis=1) print(df_dropped_rows) print(df_dropped_cols)

### Handling Missing Values in Series

Pandas Series are one-dimensional arrays with labels, and they can also have missing values. The methods to handle missing data in Series are similar to those used in DataFrames. You can use `isnull()`

, `fillna()`

, and `dropna()`

in the same way as previously shown with DataFrames.

Here’s an example of checking for missing values, filling them, and dropping them in a Series:

import pandas as pd data = [1, 2, None, 4, 5, None] series = pd.Series(data) print(series.isnull())

Filling missing values in a Series:

series_filled = series.fillna(method="ffill") print(series_filled)

Dropping missing values in a Series:

series_dropped = series.dropna() print(series_dropped)

## Working with Null and NaN Values

This section will guide you through checking for null and NaN values and replacing them using Python and popular libraries like Pandas and NumPy.

### Checking for Null and NaN Values

In Python, a null value is represented by the `None`

keyword. Comparing a variable with `None`

directly can be done using the `is`

keyword.

For example:

x = None if x is None: print("x is null")

NaN (Not a Number) values are primarily used in the context of numerical arrays or data manipulation libraries like NumPy. To check for NaN values, use NumPy’s `isnan()`

function:

import numpy as np x = np.nan if np.isnan(x): print("x is NaN")

In Pandas, `isnull()`

and `notnull()`

functions are available for checking null and NaN values in Series or DataFrame objects:

import pandas as pd data = {'Column1': [1, None, 3, np.nan], 'Column2': [4, 5, np.nan, 7]} df = pd.DataFrame(data) null_mask = df.isnull() not_null_mask = df.notnull()

### Replacing Null and NaN Values

In Python, you may use a conditional expression to replace null values with a sentinel value or another placeholder:

x = None y = x if x is not None else 0

NumPy provides the `array()`

function to replace NaN values in an ndarray with another value:

import numpy as np array = np.array([1, 2, np.nan, 4]) masked_array = np.where(np.isnan(array), 0, array)

In Pandas, the `fillna()`

function is used to replace null and NaN values in a DataFrame:

import pandas as pd data = {'Column1': [1, None, 3, np.nan], 'Column2': [4, 5, np.nan, 7]} df = pd.DataFrame(data) filled_df = df.fillna(value=0)

You can also use the `replace()`

function to replace specific NaN or null values with a desired value:

import pandas as pd import numpy as np data = {'Column1': [1, None, 3, np.nan], 'Column2': [4, 5, np.nan, 7]} df = pd.DataFrame(data) replaced_df = df.replace(np.nan, 0)

## Handling Complex Calculations

When working with complex calculations in Python, especially involving real and complex numbers, vectors, and matrices, it is essential to understand the peculiarities of certain values such as `Inf`

, `-Inf`

, and `NaN`

. This will help you handle edge cases, comparisons, and more efficiently.

### Inf, -Inf, and NaN in Calculations

`Inf`

represents positive infinity, while `-Inf`

represents negative infinity. These values can be the result of particular mathematical operations, such as dividing a number by zero. On the other hand, `NaN`

stands for “Not a Number” and usually results from undefined calculations.

Let’s consider some code examples and how Python reacts to `Inf`

, `-Inf`

, and `NaN`

:

import numpy as np # Create a vector and operation resulting in Inf, -Inf, and NaN a = np.array([1, -1]) b = np.array([0, 0]) result = a / b print(result) # Output: [inf, -inf] # Create a matrix and operation resulting in NaN c = np.array([[0.0, 0.0], [0.0, 0.0]]) result = np.sqrt(c - c) print(result) # Output: [[nan, nan], [nan, nan]]

For handling these peculiar values in your calculations, you can use the functions `isinf()`

, `isnan()`

, and `type()`

:

print(np.isinf(result)) # Output: [ True, True, False, False] print(np.isnan(result)) # Output: [False, False, True, True] print(type(result[0])) # Output: <class 'numpy.float64'>

Sometimes, you may want to check if *every* or *any* element of an array meets a specific condition, such as being `NaN`

. In that case, you can use the `np.all()`

and `np.any()`

functions:

print(np.any(np.isnan(c))) # Output: False print(np.all(np.isnan(c))) # Output: False

## Frequently Asked Questions

### How to return an empty value from a function in Python?

To return an empty value or “nothing” from a Python function, you can use the `return`

statement followed by the keyword `None`

. For example:

def my_function(): if some_condition: return "Something" else: return None

You can also use an empty `return`

statement, which is equivalent to returning `None`

:

def my_function(): if some_condition: return "Something" else: return

### When should I use return None in Python?

You should use `return None`

when you want to explicitly indicate that a function does not return a value or when it encounters a specific condition that leads to the absence of a return value. Returning `None`

can be useful for indicating that a function has not found a match, outputted an error, or completed a specific task.

### What is the difference between ‘return’ and ‘return None’ in Python?

There is no practical difference between using `return`

and `return None`

in Python. Both statements will result in the function returning the `None`

value. However, using `return None`

can be considered more explicit and self-explanatory, making the code easier to understand.

### How can I check if a function returns None?

You can check if a function returns `None`

by using an `if`

statement to compare the function’s output to the `None`

keyword, like this:

result = my_function() if result is None: print("The function returned None") else: print("The function returned:", result)

### How to handle functions that return an object or None?

When dealing with functions that return either an object or `None`

, it is essential to check the output before using it further, to prevent potential errors. You can use an `if`

statement to check if the output is `None`

and then handle the case accordingly:

result = my_function() if result is not None: # Use the result object do_something(result) else: # Handle the None case handle_none_case()

### How to replace NaN with None in Python?

To replace a `NaN`

(Not a Number) value with `None`

in Python, you can use the `math.isnan()`

function to check for `NaN`

values and replace them accordingly:

import math def replace_nan_with_none(value): if math.isnan(value): return None else: return value

You can apply this function to the elements of a list, array, or any other iterable using list comprehension or a loop:

values_with_nan = [1, 2, float('nan'), 4, 5] values_without_nan = [replace_nan_with_none(v) for v in values_with_nan]

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.