given a set of integers and a value for which you need to find the nearest one.
def nearest_number(numbers, target): numbers.sort() diff = target - numbers[0] for i in range(1, len(numbers)): if target - numbers[i] < diff: diff = target - numbers[i] return numbers[0] + diff nearest_number([1, 2, 3, 4, 5], 3)