```
def clamp(n, minn, maxn):
return max(min(maxn, n), minn)
```

or functionally equivalent:

```
clamp = lambda n, minn, maxn: max(min(maxn, n), minn)
```

now, you use:

```
n = clamp(n, 7, 42)
```

or make it perfectly clear:

```
n = minn if n < minn else maxn if n > maxn else n
```

even clearer:

```
def clamp(n, minn, maxn):
if n < minn:
return minn
elif n > maxn:
return maxn
else:
return n
```

### Related Contents:

- Getting the index of the returned max or min item using max()/min() on a list
- How to find the min/max value of a common key in a list of dicts?
- Find min, max, and average of a list
- Get the row(s) which have the max value in groups using groupby
- Getting key with maximum value in dictionary?
- How do I get indices of N maximum values in a NumPy array?
- Find the column name which has the maximum value for each row
- Finding max value in the second column of a nested list?
- How to find all positions of the maximum value in a list?
- Get the key corresponding to the minimum value within a dictionary
- How to get the index of a maximum element in a NumPy array along one axis
- Python: Maximum recursion depth exceeded
- Find maximum value of a column and return the corresponding row values using Pandas
- What is the maximum float in Python?
- Find the item with maximum occurrences in a list [duplicate]
- numpy max vs amax vs maximum
- Pandas max value index
- return max value from pandas dataframe as a whole, not based on column or rows
- 5 maximum values in a python dictionary
- Find the greatest (largest, maximum) number in a list of numbers
- Which maximum does Python pick in the case of a tie?
- Tuple pairs, finding minimum using python
- Using Python’s max to return two equally large values
- Maximum and minimum value of C types integers from Python
- Finding smallest float in file then printing that and line above it
- How to make numpy.argmax return all occurrences of the maximum?
- How do I find the maximum (larger, greater) of 2 numbers?
- Check if all values in list are greater than a certain number
- Best way to structure a tkinter application?
- Creating dataframe from a dictionary where entries have different lengths
- Python and ClearCase setview
- TensorFlow Only running on 1/32 of the Training data provided [duplicate]
- How to split a dos path into its components in Python
- In Python, how do I index a list with another list?
- Making an asynchronous task in Flask
- How to copy a class?
- Spark iteration time increasing exponentially when using join
- CSVWriter not saving data to file the moment I write it
- What is the difference between Series.replace and Series.str.replace?
- How to pass dictionary items as function arguments in python? [duplicate]
- Use vector2 in pygame. Collide with the window frame and restrict the ball to the rectangular area
- Is there shorthand for returning a default value if None in Python? [duplicate]
- How are Pipfile and Pipfile.lock used?
- OpenCV: “[ WARN:0] terminating async callback” when attempting to take a picture
- How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?
- how to to terminate process using python’s multiprocessing
- What does ‘u’ mean in a list?
- Pycharm: run only part of my Python file
- Different std in pandas vs numpy
- What does = (equal) do in f-strings inside the expression curly brackets?