For the record, you named the function "
is_even", so you should expect only information whether "
is_even" is even and returns true, or "
is_even" is not even and returns false.
trying to get the following code to tell me if a number is odd or even
To make the code more unambiguously readable IMO it should be looks like e.g.:
Python:
def is_even(num):
return num % 2 == 0
def is_odd(num):
return not num % 2 == 0
for i in range(6):
if is_even(i):
print(f'{i} is even')
for i in range(6):
if is_odd(i):
print(f'{i} is odd')
why? ...
Function naming conventions
Using proper names for functions in programming is important because:
- Readability: Clear names make code easier to understand.
- Maintenance: Easier to update and debug.
- Collaboration: Helps team members understand and work with the code.
- Reusability: Good names make libraries and APIs more intuitive to use.
- Documentation: Improves both automated and manual documentation.
- Avoiding Confusion: Reduces ambiguity and errors.
Example:
- def calc(a, b): vs. def calculate_sum(a, b):
The second name is clearer about the function's purpose.
different scenario
Python:
def is_even_odd(num):
return num % 2 == 0
for i in range(6):
print('is', i, 'even', str(is_even_odd(i)))
or some very similar what
@menator01 written
Python:
def is_even_odd(num):
return 'even' if num % 2 == 0 else 'odd'
for i in range(6):
print(i, 'is', is_even_odd(i))