Home ยป How to Count Even and Odd Numbers in a List using python

How to Count Even and Odd Numbers in a List using python

Java SE 11 Programmer II [1Z0-816] Practice Tests
Oracle Java Certification
Spring Framework Basics Video Course
Java SE 11 Developer (Upgrade) [1Z0-817]
1 Year Subscription
Java SE 11 Programmer I [1Z0-815] Practice Tests

In this article we show how to count odd and even elements in a list

We will show 2 methods of doing this

Table of Contents

Example 1

Method 1 uses a for loop

# Program to Count Even and Odd Numbers in a List
mylist = []
even_numbers = 0
odd_numbers = 0

# Get user input
Number = int(input("Please enter the Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of Element %d : " %i))
    mylist.append(value)

# loop through and count even and odd numbers
for j in range(Number):
    if(mylist[j] % 2 == 0):
        even_numbers = even_numbers + 1
    else:
        odd_numbers = odd_numbers + 1
        
#Print the counts
print("\nTotal Number of Even Numbers in this List =  ", even_numbers)
print("Total Number of Odd Numbers in this List = ", odd_numbers)

Lets try this example out

>>> %Run listoddeven1.py
Please enter the Number of List Elements: 7
Please enter the Value of Element 1 : 1
Please enter the Value of Element 2 : 2
Please enter the Value of Element 3 : 3
Please enter the Value of Element 4 : 4
Please enter the Value of Element 5 : 5
Please enter the Value of Element 6 : 6
Please enter the Value of Element 7 : 7

Total Number of Even Numbers in this List =   3
Total Number of Odd Numbers in this List =  4

Example 2

Method 2 uses a while loop

# Program to Count Even and Odd Numbers in a List
mylist = []
even_numbers = 0
odd_numbers = 0
j = 0

# Get user input
Number = int(input("Please enter the Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of Element %d : " %i))
    mylist.append(value)

# loop through and count even and odd numbers
while(j < Number):
    if(mylist[j] % 2 == 0):
        even_numbers = even_numbers + 1
    else:
        odd_numbers = odd_numbers + 1
    j = j + 1

#Print the counts
print("\nTotal Number of Even Numbers in this List =  ", even_numbers)
print("Total Number of Odd Numbers in this List = ", odd_numbers)

Lets see a test run

>>> %Run listoddeven2.py
Please enter the Number of List Elements: 7
Please enter the Value of Element 1 : 1
Please enter the Value of Element 2 : 2
Please enter the Value of Element 3 : 3
Please enter the Value of Element 4 : 4
Please enter the Value of Element 5 : 5
Please enter the Value of Element 6 : 6
Please enter the Value of Element 7 : 7

Total Number of Even Numbers in this List =   3
Total Number of Odd Numbers in this List =  4

Link

This is in our github repository

https://github.com/programmershelp/maxpython/tree/main/code%20example/List%20examples

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More