Problem 165425 · easy · Phase 01 Prerequisites & Setup

Count the Even Numbers in a Range

loops · range · counting · modulo

Write a function count_evens(a, b) that returns how many even integers lie between a and b, including both ends. Remember that 0 is even and negative numbers can be even too.

Examples

Input:  a = 1, b = 10
Output: 5
Explanation: 2, 4, 6, 8, 10

Input:  a = -3, b = 3
Output: 3
Explanation: -2, 0, 2

Constraints

  • -10**5 <= a <= b <= 10**5

Goals

  • Loop over an inclusive range of integers
  • Count how many items satisfy a condition
Starting Python…