Write a program to create a list 1. Count even and odd numbers 2. Print Reverse list 3. Find the most frequent value in list 4. filter the list . using conditional statements.
Code:
def count_even_odd(numbers):
even_count = 0
odd_count = 0
for num in numbers:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
return even_count, odd_count
def print_reverse_list(numbers):
reversed_list = numbers[::-1]
print("Reversed List:", reversed_list)
def find_most_frequent(numbers):
frequency = {}
for num in numbers:
if num in frequency:
frequency[num] += 1
else:
frequency[num] = 1
most_frequent = max(frequency, key=frequency.get)
return most_frequent
def filter_list(numbers, condition):
filtered_list = [num for num in numbers if condition(num)]
return filtered_list
# Main function
def main():
# Create a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
# Task 1: Count even and odd numbers
even_count, odd_count = count_even_odd(numbers)
print("Even Count:", even_count)
print("Odd Count:", odd_count)
# Task 2: Print Reverse list
print_reverse_list(numbers)
# Task 3: Find the most frequent value in the list
most_frequent = find_most_frequent(numbers)
print("Most Frequent Value:", most_frequent)
# Task 4: Filter the list using conditional statements
filtered_even_numbers = filter_list(numbers, lambda x: x % 2 == 0)
print("Filtered Even Numbers:", filtered_even_numbers)
if __name__ == "__main__":
main()
0 Comments