Python code to Find the Frequency of Odd & Even Numbers in the given Lists.
Python code implementation without user-defined functions & classes
Code:
#Python code to Find the Frequency of Odd & Even Numbers in the given List
a = [1, 3, 4, 5, 101, 234, 191]
print("List elements are: ",a)
frequency_even = 0
frequency_odd = 0
for i in a:
if i % 2 == 0:
frequency_even = frequency_even + 1
else:
frequency_odd = frequency_odd + 1
print("Frequency of even number in the list is: ",frequency_even)
print("Frequency of odd number in the list is: ",frequency_odd)
Output:
List elements are: [1, 3, 4, 5, 101, 234, 191] Frequency of even number in the list is: 2 Frequency of odd number in the list is: 5
Python code implementation using function
Code:
#Python code to Find the Frequency of Odd & Even Numbers in the given List using Function
a = [1, 3, 4, 5, 101, 234, 191]
def frequency_odd_even_list_func(a_list):
print("List elements are: ",a_list)
frequency_even = 0
frequency_odd = 0
for i in a_list:
if i % 2 == 0:
frequency_even = frequency_even + 1
else:
frequency_odd = frequency_odd + 1
print("Frequency of even number in the list is: ", frequency_even)
print("Frequency of odd number in the list is: ", frequency_odd)
frequency_odd_even_list_func(a)
Output:
List elements are: [1, 3, 4, 5, 101, 234, 191] Frequency of even number in the list is: 2 Frequency of odd number in the list is: 5
Python code implementation using Classes
Code:
#Python code to Find the Frequency of Odd & Even Numbers in the given List using class
a = [1, 3, 4, 5, 101, 234, 191]
class Frequency_odd_even_list_class(object):
def frequency_odd_even_list_func(self,a_list):
self.a_list = a_list
print("List elements are: ", self.a_list)
frequency_even = 0
frequency_odd = 0
for i in self.a_list:
if i % 2 == 0:
frequency_even = frequency_even + 1
else:
frequency_odd = frequency_odd + 1
print("Frequency of even number in the list is: ", frequency_even)
print("Frequency of odd number in the list is: ", frequency_odd)
#Create Object
Object_1 = Frequency_odd_even_list_class()
#Call Function Using Created Object
Object_1.frequency_odd_even_list_func(a)
Output:
List elements are: [1, 3, 4, 5, 101, 234, 191] Frequency of even number in the list is: 2 Frequency of odd number in the list is: 5
Enjoy Python Code By Pythonbaba 🙂