def constrain(value, min_value, max_value):
    """
    Constrains the `value` to the specified range `[min_value, max_value]`

    Parameters
    ----------
    `value` : The value to be constrained

    `min_value` : The lower limit of range (inclusive)

    `max_value` : The upper limit of range (inclusive)

    Returns
    -------
    `min_value` : if `value` is less than `min_value`

    `max_value` : if `value` is greater than `max_value`

    `value` : otherwise

    Examples
    --------
    >>> constrain(10, 20, 40)
    20
    >>> constrain(30, 20, 40)
    30
    >>> constrain(50, 20, 40)
    40
    """

    return min(max(value, min_value), max_value)