World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
kindly assist me with the 10th question from Functions Que : Create a Function which will print all even and odd numbers over a specified range
hint- you need to check about range function with stepping value that we have discussed in the session
Certainly! Here is an example of how you could create a function to print all even and odd numbers within a specified range:
def print_even_odd(start, end): for num in range(start, end+1): if num % 2 == 0: print(num, "is even") else: print(num, "is odd") # Example usage print_even_odd(1, 10)
This function takes in two arguments: start
and end
, which specify the range of numbers to consider. The function then uses a loop to iterate over the numbers in that range and uses the modulo operator (%
) to determine whether each number is even or odd. If the number is even, it prints "is even"; if the number is odd, it prints "is odd".
I hope this helps! Let me know if you have any questions.