Count the positive elements in a list

PYTHON program is:

#
#    Count the positive elements, the zero elements,
#    and the negative elements in a list.
#    A function is constructed that returns three values.
#        return a, b, c
#        x, y, z = Count_list(List) is x,y,z=a,b,c
#
Count_positive = 0
Count_negative = 0
Count_zero = 0
List_elements_A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List_elements_B = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
List_elements_C = [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
List_elements_D = [1, 2, 3, 0, 0, 0, 0, -1, -2, -3]
def Count_list(List):
    C_pos = 0
    C_neg = 0
    C_zer = 0
    No_elem = len(List)
    for i in range(No_elem):
        if(List[i] > 0):
            C_pos = C_pos +1
        if(List[i] < 0):
            C_neg = C_neg +1
        if(List[i] == 0):
            C_zer = C_zer +1
    return C_pos, C_neg, C_zer
print ('********** List_elements_A **********')
Count_positive, Count_negative, Count_zero = Count_list(List_elements_A)
print ('Number positive elements are:', Count_positive)
print ('Number negative elements are:', Count_negative)
print ('Number zero elements are:', Count_zero)
print ('********** List_elements_B **********')
Count_positive, Count_negative, Count_zero = Count_list(List_elements_B)
print ('Number positive elements are:', Count_positive)
print ('Number negative elements are:', Count_negative)
print ('Number zero elements are:', Count_zero)
print ('********** List_elements_C **********')
Count_positive, Count_negative, Count_zero = Count_list(List_elements_C)
print ('Number positive elements are:', Count_positive)
print ('Number negative elements are:', Count_negative)
print ('Number zero elements are:', Count_zero)
print ('********** List_elements_D **********')
Count_positive, Count_negative, Count_zero = Count_list(List_elements_D)
print ('Number positive elements are:', Count_positive)
print ('Number negative elements are:', Count_negative)
print ('Number zero elements are:', Count_zero)


Results are:

Python 3.10.2 (v3.10.2:a58ebcc701, Jan 13 2022, 14:50:16) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
============== RESTART: /Users/ionivan/Documents/CountElements.py ==============
********** List_elements_A **********
Number positive elements are: 10
Number negative elements are: 0
Number zero elements are: 0
********** List_elements_B **********
Number positive elements are: 0
Number negative elements are: 0
Number zero elements are: 10
********** List_elements_C **********
Number positive elements are: 0
Number negative elements are: 10
Number zero elements are: 0
********** List_elements_D **********
Number positive elements are: 3
Number negative elements are: 3
Number zero elements are: 4

(March 21, 2022)

Comments

Popular posts from this blog

Pointers in PYTHON?

Generates M prime numbers