Skip to content

sleep_even

Is even?

is_even

is_even(x: int) -> bool

Check if a number is even.

Parameters:

Name Type Description Default
x int

The number to check.

required

Returns:

Type Description
bool

Whether the number is even.

Examples:

>>> is_even(2)
True
>>> is_even(3)
False
Source code in src/sleep_even/__init__.py
def is_even(x: int, /) -> bool:
    """Check if a number is even.

    Args:
        x: The number to check.

    Returns:
        Whether the number is even.

    Examples:
        >>> is_even(2)
        True
        >>> is_even(3)
        False
    """
    even = True

    async def switcher() -> None:
        nonlocal even
        await asyncio.sleep(0.5)
        while True:
            even = not even
            await asyncio.sleep(1)

    async def watcher() -> None:
        task = asyncio.create_task(switcher())
        await asyncio.sleep(x)
        task.cancel()

    asyncio.run(watcher())
    return even