Overview
The provided code defines a simple Python function check_even_odd
that determines whether a given integer is even or odd. It includes a docstring for documentation, uses conditional statements to perform the check, and demonstrates its functionality with example usage.
Code Breakdown
Function Definition
def check_even_odd(number):
- Function Name:
check_even_odd
- Parameter:
number
(expected to be an integer)
- This function will return a string indicating whether the input number is even or odd.
Docstring
"""
Check if a number is even or odd.
Parameters:
number (int): The number to be checked.
Returns:
str: A message indicating whether the number is even or odd.
"""
- The docstring provides a clear explanation of the function's purpose, its parameters, and its return value. This is a good practice for code documentation, making it easier for others (or yourself in the future) to understand the function's intent.
Conditional Logic
- This line checks if the number is even by using the modulus operator
%
. If the remainder when number
is divided by 2 is 0
, the number is even.
return f"The number {number} is even."
- If the condition is true, the function returns a formatted string indicating that the number is even.
else:
return f"The number {number} is odd."
- If the condition is false (meaning the number is odd), the function returns a string indicating that the number is odd.
Example Usage
print(check_even_odd(10)) # Output: The number 10 is even.
print(check_even_odd(7)) # Output: The number 7 is odd.
- These lines demonstrate how to call the
check_even_odd
function with different integers and print the results. The output confirms the function's correctness.
Key Concepts and Best Practices
- Modulus Operator: The use of
%
is a common way to determine evenness or oddness in programming.
- String Formatting: The use of f-strings (formatted string literals) for constructing the return message is a modern and readable way to include variable values in strings.
- Documentation: Including a docstring is a best practice that enhances code readability and maintainability.
- Conditional Statements: The use of
if-else
statements is a fundamental programming concept for decision-making.
Potential Issues and Improvements
-
Input Validation: The function currently does not handle cases where the input is not an integer (e.g., strings or floats). Adding input validation could improve robustness:
if not isinstance(number, int):
return "Input must be an integer."
-
Type Hinting: Adding type hints to the function signature can improve code clarity:
def check_even_odd(number: int) -> str:
Alternative Approaches
This approach is less verbose but may be less clear to those unfamiliar with lambda functions.
Conclusion
The check_even_odd
function is a straightforward implementation for determining the evenness or oddness of an integer. It effectively uses conditional logic and string formatting while adhering to good documentation practices. With minor improvements, such as input validation and type hinting, it could be made even more robust and user-friendly.