Skip to content

Instantly share code, notes, and snippets.

@baybatu
Created June 26, 2016 06:27
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save baybatu/269296fe1d530f0defff7b6454222bc0 to your computer and use it in GitHub Desktop.
Save baybatu/269296fe1d530f0defff7b6454222bc0 to your computer and use it in GitHub Desktop.
Limiting input number between minimum and maximum values range in Python
def limit(num, minimum=1, maximum=255):
"""Limits input 'num' between minimum and maximum values.
Default minimum value is 1 and maximum value is 255."""
return max(min(num, maximum), minimum)
@bryomuch
Copy link

bryomuch commented Feb 9, 2018

wonderful ,just worderful, this saved me a lot of time

@testman42
Copy link

Nice, exactly what I was looking for. Thank you very much for saving this to Github 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment